HI
Can anyone please get me the code for shmget() function implementation in shared memory as soon as possible.........thank you
3 9681
Please be more specific. Are you wanting to see the actual library implementation of the "shmget" function, or are you wanting an example of how to write code that utilizes shared memory? Shared memory is not exactly a cake walk. In particular you usually need to utilze semaphores to synchronize access to the shared memory segment.
Please be more specific. Are you wanting to see the actual library implementation of the "shmget" function, or are you wanting an example of how to write code that utilizes shared memory? Shared memory is not exactly a cake walk. In particular you usually need to utilze semaphores to synchronize access to the shared memory segment.
Can you please get me the library implementation and also an example of how to write code to utilize shared memory.......
If you want to see how shared memory is implemented then go look at the Linux kernel source code. That is an excercise I leave up to you. However, I will warn you that it is not pretty, and probably not going to make sense unless you have a high level understanding of C programming and the implementation of the Linux kernel.
As far as using shared memory goes here is an incredibly simplistic client/server example using shared memory. Mind you in production code you want to use semaphores or a record locking mechanism for syncronization and concurrency which raise the code complexity by several magnitudes.
Server.c -
#include <sys/types.h>
-
#include <sys/ipc.h>
-
#include <sys/shm.h>
-
#include <stdio.h>
-
-
#define SHMSZ 27
-
-
main()
-
{
-
char c;
-
int shmid;
-
key_t key;
-
char *shm, *s;
-
-
/*
-
* We'll name our shared memory segment
-
* "5678".
-
*/
-
key = 5678;
-
-
/*
-
* Create the segment.
-
*/
-
if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) {
-
perror("shmget");
-
exit(1);
-
}
-
-
/*
-
* Now we attach the segment to our data space.
-
*/
-
if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) {
-
perror("shmat");
-
exit(1);
-
}
-
-
/*
-
* Now put some things into the memory for the
-
* other process to read.
-
*/
-
s = shm;
-
-
for (c = 'a'; c <= 'z'; c++)
-
*s++ = c;
-
*s = NULL;
-
-
/*
-
* Finally, we wait until the other process
-
* changes the first character of our memory
-
* to '*', indicating that it has read what
-
* we put there.
-
*/
-
while (*shm != '*')
-
sleep(1);
-
-
exit(0);
-
}
-
Client.c -
/*
-
* shm-client - client program to demonstrate shared memory.
-
*/
-
#include <sys/types.h>
-
#include <sys/ipc.h>
-
#include <sys/shm.h>
-
#include <stdio.h>
-
-
#define SHMSZ 27
-
-
main()
-
{
-
int shmid;
-
key_t key;
-
char *shm, *s;
-
-
/*
-
* We need to get the segment named
-
* "5678", created by the server.
-
*/
-
key = 5678;
-
-
/*
-
* Locate the segment.
-
*/
-
if ((shmid = shmget(key, SHMSZ, 0666)) < 0) {
-
perror("shmget");
-
exit(1);
-
}
-
-
/*
-
* Now we attach the segment to our data space.
-
*/
-
if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) {
-
perror("shmat");
-
exit(1);
-
}
-
-
/*
-
* Now read what the server put in the memory.
-
*/
-
for (s = shm; *s != NULL; s++)
-
putchar(*s);
-
putchar('\n');
-
-
/*
-
* Finally, change the first character of the
-
* segment to '*', indicating we have read
-
* the segment.
-
*/
-
*shm = '*';
-
-
exit(0);
-
}
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Michael Schuler |
last post by:
The use of STL in shared memory poses a real problem since
(non-smart) pointers are not allowed there.
Is there any solution for containers in shared memory using
smart pointers? Where can I...
|
by: Jim |
last post by:
Hello,
I have a broken server that we are going to be moving off to a new server
with a new version of DB2 but here is what I have right now:
RedHat 7.0 (2.2.24smp)
DB2 v6.1.0.40
I am...
|
by: Alexander Cohen |
last post by:
Hi, sometimes ill get this error:
FATAL: could not create shared memory segment: Cannot allocate memory
DETAIL: Failed system call was shmget(key=2, size=4153344, 03600).
HINT: This error...
|
by: zeny |
last post by:
Hi everybody!
In the file "server.c" i created a shared memory segment of the size of a 1024 byte string and then i attached a string to the segment, just like this:
int key = 12345;
char...
|
by: zeny |
last post by:
Hey everyone! I´ve been tying to create a shared memory segment with the size of a structure, as follows:
typedef struct{
int id;
char message;
}data;
In the line where i create the shared...
|
by: cavemandave |
last post by:
hi people i have been teaching myself C for the last few months and have recently moved onto ICP shared memory. I have with aid developed some simple code that prints out some chars i then wanted to...
|
by: Ron Eggler |
last post by:
Hi,
I'm running an application on an embedded PC 104 platform on a Linux OS.
I need some shared memory and I'm trying to create it with:
int prg_shm = ::shmget(0x1999,sizeof(struct...
|
by: hugo.arregui |
last post by:
Hi!
I have two struts like that:
struct {
int num;
int num2;
struct b arrayOfB;
} a;
|
by: Tosh2pointO |
last post by:
Hello, I'm trying to make a program that will multiply 2 matrices using shmget() and fork(). For example, I would need to multiply a 64 x 64 matrix using 4 processes or 16 processes, and the...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
| |