Re: More PTHREAD_PROCESS_SHARED(Storing pointers into shared memory?)
| Newsgroups | gmane.linux.ngpt.devel |
|---|---|
| Message-ID | <[email protected]> |
I have written code that demonstrates the problem. This scenario involves two programs(proga, progb) executing the same code that manipulates some shared data protected with a PTHREAD_PROCESS_SHARED mutex. Both programs use the same initialization code to map its shared memory and, if necessary, initialize the mutex. The only difference between the two is that progb has another library linked in (libbreak). This library doesn't do anything but allocate some memory, but takes up address space causing the internal NGPT shared memory to be mapped at a different address than proga. The source to everything is in the attached tarball.
After initialization, each program acquires the mutex, prints out the shared data, then updates it to be the programs name (argv[0]). Before each step it asks the user to press enter (this way you can get the other program started).
If you run two instances of proga, or two instances of progb, everything works as expected. Running proga with progb causes the locks not to work (seemingly ignored).
I found it useful to add this code to the bottom of pth_initialize_shared():
fprintf(stderr, "NGPT: shared area pointer: %p\n", pth_shared_area);
You can see then, that the memory is mapped to a different address in progb then proga.
to test:
untar ngptproblem.tar.gz
./configure --with-ngptdir=/your/ngpt/path
make
./proga
(on another tty)
./progb (or proga if you want to see it work)
The first <enter> locks the mutex, prints out the data, then updates it. The second <enter> unlocks the mutex. So hit enter on proga, then hit enter on progb. The lock wont be honored and the critical section executes(because the pointer in the shared mutex is wrong from progb's perspective).
Here is the output from proga:
NGPT: shared area pointer: 0x40195000
internal mutex pointer: 0x40198200
press a key to obtain the lock and update data
locking mutex...
locked
shared data is ''
press a key to unlock the mutex
unlocking mutex...
unlocked
And here from progb:
NGPT: shared area pointer: 0x40197000
Initializing library
press a key to obtain the lock and update data
locking mutex...
locked
shared data is './proga'
press a key to unlock the mutex
unlocking mutex...
unlocked
Thanks for taking a look,
Chris McFarlen
Z:\ngptproblem\ngptproblem-1.0.0.tar.gz
(application/x-gzip-compressed, 137.4 KB) - not displayed