LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-25-2005, 05:32 PM   #1
MeMooMeM
LQ Newbie
 
Registered: May 2005
Posts: 8

Rep: Reputation: 0
Unhappy 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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
is shared memory expandable in memory size? Thinking Programming 4 08-16-2005 10:57 AM
Posix file based process shared semaphores dragonman Programming 9 05-25-2005 05:44 PM
POSIX Thread Termination confusion sleepymish Programming 2 12-05-2004 09:16 PM
About Shared Memory...? aegis_shiva Programming 1 08-25-2004 06:05 AM
shared memory socket9001 Programming 4 02-06-2004 03:08 PM


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

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration