How to save int and array in shared memory in C?

I am trying to write a program in which child processes interact with each other on Linux.

All these processes are created from the same program, and they share the code.

I need them to have access to two integer variables, as well as an integer array.

I have no idea how shared memory works, and every resource I searched for doesn’t change anything, but confuse me.

Any help would be greatly appreciated!

Edit: Here is an example of some code that I have written so far to split one int, but it is probably wrong.

int segmentId; int sharedInt; const int shareSize = sizeof(int); /* Allocate shared memory segment */ segmentId = shmget(IPC_PRIVATE, shareSize, S_IRUSR | S_IWUSR); /* attach the shared memory segment */ sharedInt = (int) shmat(segmentId, NULL, 0); /* Rest of code will go here */ /* detach shared memory segment */ shmdt(sharedInt); /* remove shared memory segment */ shmctl(segmentId, IPC_RMID, NULL); 
+6
c shared-memory
source share
4 answers

You will need to increase the size of your shared memory. How big array do you need? No matter how valuable it is, you will need to select it before creating a shared memory segment - dynamic memory will not work here.

When connected to shared memory, you get a pointer to the starting address. It will be well aligned for any purpose. Thus, you can create pointers to your two variables and an array along these lines (cut out part of the skeleton from your sample code) - pay attention to the use of pointers to access shared memory:

 enum { ARRAY_SIZE = 1024 * 1024 }; int segmentId; int *sharedInt1; int *sharedInt2; int *sharedArry; const int shareSize = sizeof(int) * (2 + ARRAY_SIZE); /* Allocate shared memory segment */ segmentId = shmget(IPC_PRIVATE, shareSize, S_IRUSR | S_IWUSR); /* attach the shared memory segment */ sharedInt1 = (int *) shmat(segmentId, NULL, 0); sharedInt2 = sharedInt1 + 1; sharedArry = sharedInt1 + 2; /* Rest of code will go here */ ...fork your child processes... ...the children can use the three pointers to shared memory... ...worry about synchronization... ...you may need to use semaphores too - but they *are* complex... ...Note that pthreads and mutexes are no help with independent processes... /* detach shared memory segment */ shmdt(sharedInt1); /* remove shared memory segment */ shmctl(segmentId, IPC_RMID, NULL); 
+6
source share

This guide looks useful: http://www.cs.cf.ac.uk/Dave/C/node27.html . It includes several sample programs.

There are also personal Linux pages online .

0
source share

Shared memory is just a memory segment allocated by one process with a unique identifier, and the other process also makes allocation with the same identifier, and the memory size is the size of the structure using, so you will have a structure with 2 integers and whole array.

Now they have a pointer to the same memory, so the records of one will be overwritten, however, and the other has immediate access to it.

0
source share

From your comment, it seems that you are using IPC_PRIVATE , and it definitely looks wrong (the "private" types suggesting this is not for sharing, no?). Try something like:

 #include <sys/ipc.h> #include <sys/shm.h> ... int segid = shmget((key_t)0x0BADDOOD, shareSize, IPC_CREAT); if (segid < 0) { /* insert error processing here! */ } int *p = (int*) shmat(segid, 0, 0); if (!p) { /* insert error processing here! */ } 
0
source share

All Articles