Currently, let’s say I create a shared_memory segment using mulitprocessing.shared_memory.SharedMemory in Process 1 and open the same in Process 2.
Then, I try to write some data to the shared memory segment using both the processes, so for me to prevent any race condition (data corruption), either these operations must be atomic, or I should be able to lock / unlock shared memory segment, which I cannot at the moment.

I earlier posted a solution to this problem, which received positive response, but there weren’t many responses to it, despite the fact this problem makes shared_memory practically unusable if there are simultaneous writes.
So, the purpose of this post is to have discussion about the solution of the same.

Some Solutions:

1. Support for shared semaphores across unrelated processes to lock/unlock shared memory segment.
    --> More Details

2. Let the first bit in the shared memory segment be the synchronisation bit used for locking/unlocking.
    --> A process can only lock the shared memory segment if this bit is unset, and will set this bit after acquiring lock, and similarly unset this bit        after unlocking. Although set/unset operations must be atomic. Therefore, the following tools can be used:
type __sync_add_and_fetch (type *ptr, type value, ...)
type __sync_sub_and_fetch (type *ptr, type value, ...)
type __sync_or_and_fetch (type *ptr, type value, ...)

Documentation of these can be found at below links:

Any other ideas/solutions are very welcome.