Shared Memory & Read/Write Locks


I want to use a shared memory object to transfer data between multiple processes. However its set up in such a way where one process will write to the shared memory and other will read from it.

How do I ensure that read processes aren’t accessing it while the write process is writing. From my understanding locks/mutexes/semaphores only work between threads in the same process because they are simply a variable type, is this true?? If so, how can I recreate this functionality across processes, preferably using only kernel functions. I’m sure I’m not the first person to run across this issue.


Maybe pass a message to to processes to say when read/write is OK, or maybe make the first byte of the share memory block a marker (1 or 0) of whether the block is being written to? You could maybe make a write set the byte to 1, a read set the byte to 2, so both processes can check to see if the other is doing something?

Accoring to … ml#SYNCHRO you can use mutex between process, just store it in the share memory itself

Same apply for read/write locks

That looks good, except where do I store the rwl data structure, I wasn’t able to find where it specified something like that. I am hoping its not supposed to go in more shared memory, as that just seems messy to me.


Where else can it goes aside shared memory? These are low level primite that works on same data structure they have to be shared either in the “public” sharememory or in some private shared memory created just to stored the object (mutext/rwl/…) Your choice.

I like to make the share memory map to a C structure. That way all programs just use the C structure as reference for the data layout in the shared memory, adding a mutext/rwl/etc to that structure becomes trivial.

what is actually shared memory?

You can put a pthread_mutex into shared memory, you just have to set the attribute when it is created to let the kernel know that you are expecting it to work between processes. pthread_mutexattr_setpshared().

It’s an address range accessible to more then one program.