Multiprocess synchronization is a better choice than semaphores?

I have a queue resource that is used by several producers and several consumers. All these are independent processes; no process owns a queue.

By the nature of the implementation, access to the queue should be controlled, and only one process should be allowed to click or pop up at any time.

I thought that using a semaphore called POSIX would be the right solution, however some details bother me. (This is an implementation only for Linux, by the way.)

  • When (if ever) should I do sem_unlink? Is there any reason to delete the queue?

  • I am concerned about the process that dies by keeping the semaphore in line. Is there a good way around this? I can execute the timeout while trying to get a lock, but if the timeout expires, I now have a race condition.

  • Is there a better solution for a simple binary lock? Perhaps a lock file using fcntl and / or exclusive opens?

+4
source share
1 answer

File locks have the advantage of unlocking in the event of an unexpected process death. I think they best suit your scenario.

I can imagine, using semaphores, when I need more complex semantics that they support (they do more than support using the mutex that you have in mind), but if I use them, I need to somehow do the job work in In the event of untimely death, I notice that Lotus Notes on Windows has a ZapNotes housewife, which I think is similar to the “shouldn't happen” scenario.

+3
source

All Articles