Varaquilex
Varaquilex

Reputation: 3463

Shared pointer to an array in shared memory, pointer doesn't seem shared?

I have an array in shared memory. I want to use a pointer for iterating through this array, which also is meant to be shared. Here is what I tried:

    /* initialize color sequence in shared memory */
    shmkey = ftok("/dev/null",3);   /* executable name and a random number */
    shmid = shmget(shmkey, sizeof(char), 0700 | IPC_CREAT);
    if(shmid < 0){      /* shared memory error check */
        perror("shmget\n"); 
        exit(1);
    }
    queue = (char*) shmat(shmid, NULL, 0);  /* shared memory part of colorSequence */
    printf("queue allocated.\n");

    /* initialize color sequence pointer in shared memory */
    shmkey = ftok("/dev/null//",61);    /* executable name and a random number */
    shmid = shmget(shmkey, sizeof(char), 0700 | IPC_CREAT);
    if(shmid < 0){      /* shared memory error check */
        perror("shmget\n"); 
        exit(1);
    }
    p = (char*) shmat(shmid, NULL, 0);  /* pointer to queue in shared memory */
    printf("queue pointer allocated.\n");
    p = &queue[0];

Now I fork the child and try to change the value p, but a change made in one process doesn't affect the other.

if(fork() == 0){   /* child process */
    sem_wait(&sem);
    printf("   Child printing *p=%c, p=%p\n",*p,p);
    p++;
    printf("   Child printing after++ p=%c, p=%p\n",*p,p);
    sem_post(&sem);
    exit(0);
}
else{   /* parent process */
    sem_wait(&sem);
    printf("Parent printing *p=%c, p=%p\n",*p,p);
    p+=2;
    printf("Parnet printing after++ p=%c, p=%p\n",*p,p);
    p = NULL;   //even though this, program doesn't fail
    sem_post(&sem);
}

Yet, the output is (Contents of queue is as follows: R B G . . .) :

Parent printing           *p=R, p=0x7f5c77837000
Parnet printing after++   p=B, p=0x7f5c77837002
   Child printing         *p=R, p=0x7f5c77837000
   Child printing after++ p=G, p=0x7f5c77837001

I couldn't figure out why I get these results even though the pointer is supposed to be shared. Can you help me fix this? Thanks.

EDIT

When I try to change the value p points to in parent process, the effects are visible when printing from child process. However incrementing pointer doesn't work.

Upvotes: 0

Views: 1266

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409266

You actually have two problems, but one is mitigated by the operating system.

The first problem is that you say your shared memory is of size 1 (sizeof(char)). This is mitigated by the OS as it rounds it up to page size.

The second problem is this:

p = (char*) shmat(shmid, NULL, 0);  /* pointer to queue in shared memory */

p = &queue[0];

Here you get p from shared memory, the you overwrite this pointer with another pointer.

However, this is a moot problem fro two reasons: The first is the one I commented about. The second is that even if you don't overwrite the pointer p it will still be "local" to your process so doing e.g. p++ will not change the copy in shared memory as the actual pointer isn't shared. If you truly wanted to have a pointer in shared memory you would have p be a pointer to a pointer, would have to do (*p)++ to increase both locally and in the shared memory.

Upvotes: 2

Related Questions