I have created a shared memory block used between two processes via
shmget(). Each of these two processes can sends information to the
other in a serial fashion: Process #1, then Process #2, then Process
#1, etc. I was trying to determine a good protocol for notification so
that each process can know when the other has completed placing a
message.
At first pass, it seemed reasonable to do the following:
1. When it's a given process' turn, the process will call shmat() to
get the raw pointer
2. The process updates the memory with its information
3. The process gives up the pointer by calling shmdt()
4. The process continually calls shmctl() with IPC_STAT and checks the
shmid_ds struct's shm_dtime field to see if it has been updated (that
is, another process has called shmdt()).
Unfortunately, it appears that on Mac OS X, each call to shmctl() to
updates the both the shm_atime and shm_dtime fields! It's almost as if
a call to shmctl() performs shmat() and shmdt() under the hood. All I
wish to do is to check on the status of the memory block to see if it
has been updated, but there does not seem to be a way to check it
without affecting the very variables I wish to view!
Any advice?
Jonathan Hoyle
Eastman Kodak
Anton Petrusevich - 29 Aug 2005 20:25 GMT
> Any advice?
Use some kind of "synchronization object". To synchronize between
_processes_ I use flock() on some file in the file system.

Signature
Anton Petrusevich
Maxim Yegorushkin - 30 Aug 2005 11:02 GMT
> I have created a shared memory block used between two processes via
> shmget(). Each of these two processes can sends information to the
[quoted text clipped - 21 lines]
>
> Any advice?
Why don't you try using Unix datagram sockets? It would be much easier
for you as all syncronization is done by glibc/kernel for you.
If you do need to use shared memory, place a header at the beginning of
it containing a mutex, a condition variable and an integer counter.
To write the memory:
1) lock the mutex
2) write the memory
3) increment the counter, store the new value in a process local
variable
4) unlock the mutex
5) notify other processes using the condition variable
To wait for the write notification:
1) lock the mutex
2) while(local_counter == shared_counter) condition.wait();
3) local_counter = shared_counter
4) read the memory
5) unlock the mutex
Anton Petrusevich - 30 Aug 2005 13:49 GMT
> If you do need to use shared memory, place a header at the beginning of
> it containing a mutex, a condition variable and an integer counter.
Can you show me a working code for different processes, not threads? Last
time I tried that that didn't work. I am not sure why, I used my own
locking mechanism then.

Signature
Anton Petrusevich