views:

590

answers:

1

I've got a queue resource that is shared across multiple producers and multiple consumers. All are independent processes; no one process "owns" the queue.

By nature of the implementation access to the queue must be controlled and only one process must be allowed to push or pop at any given moment.

I figured using a POSIX named semaphore would be the right solution, however a few of the details are bothering me. (This is a Linux-only implementation, btw.)

  1. When (if ever) should I do a sem_unlink? Is there any reason to actually remove the queue?

  2. I'm concerned about a process dying while holding the queue semaphore locked. Is there any good way around this? I can do a timed wait when trying to get the lock, but if the timeout expires I've now got a race condition.

  3. Is there a better solution for a simple binary lock like this? Perhaps a lockfile using fcntl and/or exclusive opens?

Thanks.

+1  A: 

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

I can imagine using semaphores when I need the more complex semantics they support (they do more than support the mutex usage you have in mind) but if I do use them I need some way to perform housekeeping in the event of untimely death. I observe that Lotus Notes on Windows has a "ZapNotes" houskeeper that tidies in what I assume are similar "shouldn't happen" scenarios.

djna
These are service-based apps, so a "KillNotes" (which I've used on a number of occasions!) wouldn't work here, but thanks for the suggestion.
Joe
The suggestion was actually to use file locks. The necessity of housekeeping kluges such KillNotes makes me shy away from using sempahores.
djna
Sorry, I understood your intent, just wasn't clear on my response.
Joe