I am trying to create a shared memory which will be used by multiple processes. these processes communicate with each other using MPI calls (MPI_Send, MPI_Recv).

I need a mechanism to control the access of this shared memory I added a question yesterday to see if MPI provides any facility to do that. Shared memory access control mechanism for processes created by MPI , but it seems that there is no such provision by MPI.

So I have to choose between named semaphore or flock.

For named semaphore if any of the process dies abruptly without calling sem_cloe(), than that semaphore always remains and can be seen by ll /dev/shm/. This results in deadlock sometimes(if I run the same code again!), for this reason I am currently thinking of using flock.

Just wanted to confirm if flock is best suited for this type of operation ?

Are there any disadvantages of using flock?

Is there anything else apart from named semaphore and flock that can be used here ?

I am working on C under linux.

Edit
Report