LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   POSIX shared memory confusion (https://www.linuxquestions.org/questions/programming-9/posix-shared-memory-confusion-327093/)

MeMooMeM 05-25-2005 04:32 PM

POSIX shared memory confusion
 
Hi,

I am in deep trouble, please help! :(

Here's the story:

I have a initialization code, which creates shared memory objects (say SHR_1 and SHR_2) and fills in with initial values. Then it exits, without unlinking these objects for future use.

After then, other processes open these previously created shared sections to read/modify data in them.

I successfully open/map one of these sections (say, SHR_1), and successfully read the data in it. Then I open/map another one (say, SHR_2), and this operation destroys the data in SHR_1, which I have succesfully read before!! The problem occurs after using (mmap) for SHR_2, but not (sem_open). I checked thousand of times to be sure that I didn't make silly mistakes like using the same file descriptor for both etc... But I think I make another silly mistake, that I can't see.

Is this possible? How can mapping a completely seperate shared section interfere with the data inside another?

Thanks a lot in advance!! :)

Here's my code section:

THIS WORKS:
--------------

(1) if ((SHM_PID_fd = shm_open(SHM_PID_name, O_RDWR, mode)) == -1) oops ("shm_open, SHM_PID:", 1);

(2) if ((SHM_PID = (int *)mmap (0, SHM_PID_size, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, SHM_PID_fd, (long) 0)) == (int *) -1) oops ("mmap, SHM_PID:", 1);

(3) for (i=0; i<StackDepth; ++i) printf ("PID:%d, SHM_PID[%d]=%d\n", PID, i, SHM_PID[i]); // I CAN READ THE VALUES CORRECTLY!


BUT THIS DOESN'T WORK:
---------------------------

(1) if ((SHM_PID_fd = shm_open(SHM_PID_name, O_RDWR, mode)) == -1) oops ("shm_open, SHM_PID:", 1);

(2) if ((SHM_PID = (int *)mmap (0, SHM_PID_size, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, SHM_PID_fd, (long) 0)) == (int *) -1) oops ("mmap, SHM_PID:", 1);

(3) if ((SHM_BRR_fd = shm_open(SHM_BRR_name, O_RDWR, mode)) == -1) oops ("shm_open, SHM_BRR:", 1);

(4) if ((SHM_BRR = (int *)mmap (0, sizeof(int), PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, SHM_BRR_fd, (long) 0)) == (int *) -1) oops ("mmap, SHM_BRR:", 1);

(5) for (i=0; i<StackDepth; ++i) printf ("PID:%d, SHM_PID[%d]=%d\n", PID, i, SHM_PID[i]); // GIVES WRONG VALUES FOR SHM_PID !!!

Please notice that the first two step are identical, and it is the (4)th step that causes the problem at (5)th step.

Again, thanks a lot!

MeMo


All times are GMT -5. The time now is 12:23 AM.