简体   繁体   中英

Creating and Accessing a struct in shared memory

If I have some struct

struct processData{
    int *a;
    int *b;
}

And I set up my shared memory ID as

int shmid = shmget(1234, sizeof(processData), IPC_CREAT | 0666);

Where 1234 is my key

Would I set up my actual shared struct like this..

processData* pData =(processData *) shmat(shmid, NULL, 0);

And then shouldn't I be able to change things around in seperate processes like:

pData -> a = SOME_NUMBER;

When I do this, all of my processes aren't interacting with the same piece of memory and I'm clueless why.

The members *a and *b in your structure processData are integer pointer type. Attaching a shared memory to your process does not make them point to a memory location inside the shared memory. Secondly the shared memory that you are creating using the shmget() function in the first place has only enough space to hold a processData data type. You will need some additional memory space to hold two int data types which *a and *b will point to and off course these memory spaces should also be shared so that other processes can access it.

So the solution to your problem will be something like this.

//Create the shared memory to hold the structure.
shmid=shmget(KEY1,sizeof(struct processData),IPC_CREAT|0666);
//Attach it to the process.
pData=shmat(shmid,0,0);

//Create a shared memory to hold an integer which will be pointed by *a.
shmid=shmget(KEY2,sizeof(int),IPC_CREAT|0666);
//Attach the shared memory to the location pointed by *a.
pData->a=shmat(shmid,0,0);

//Create a shared memory to hold an integer which will be pointed by *b.
shmid=shmget(KEY3,sizeof(int),IPC_CREAT|0666);
//Attach the shared memory to the location pointed by *b.
pData->b=shmat(shmid,0,0);

This is the way you should create and attach the shared memories to all the processes that are going to access the shared memory.

(Note: You do not need to create 3 separate shared memories using 3 separate key values. It can also be done by creating a single chunk of shared memory of sufficient size using 1 key and then seeking the pointers to the their respective positions inside the shared memory. It is a little complicated that is why I have given a simple example for better understanding.)

Now coming to the next problem. Since you are using pointers in your structure you cannot assign values to them like this, pData -> a = SOME_NUMBER; . Because, pData -> a is a pointer, to assign some value to it you need to deference it. So it will be done like this.

*(pData -> a) = SOME_NUMBER;

Similarly to read the value in other processes will also need to derefence it there.

SOME_NUMBER = *(pData -> a);

When in your local process you copy the pointer of a int into your struct with:

pData -> a = some-pointer;

you are actually copying the logical memory address (which is valid in your current process) into the shared memory.

When from another process you read such value, it will be a logical memory address occupied by something else (possibly also out of current addressable space).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM