What if process crashes while holding a locked semaphore?
What if process crashes while holding a locked semaphore?
- Subject: What if process crashes while holding a locked semaphore?
- From: Jerry Krinock <email@hidden>
- Date: Sat, 28 Mar 2009 17:50:26 -0700
Users of my application could install several launchd tasks, starting
multiple instances of my app's short-lived background agent at or near
the same time. But the design requires that only one instance be
running at a time. After considering various cheesy/kludgy solutions,
I decided that this calls for an exclusive system semaphore.
So I wrote a Cocoa wrapper for sem_open(), sem_close() and
sem_unlink(), and have this little demo tool which attempts to acquire
an exclusive semaphore using sem_open() before beginning some work,
retries with backoff and timeout, and when it gets the semaphore,
works for some random seconds, then calls sem_unlink() and sem_close()
to free the semaphore for other processes. The random wait, repeat.
Launch three of these and listen to them compete. Very cute, thanks
to NSSound.
Now all this will work fine unless one of my agents crashes in the
middle of its work. Then the semaphore will not be available to other
agents until, I presume, the user logs out and back in. So it looks
like I need to have another little agent that might be triggered, say,
once every 5 minutes, to check that my semaphore has not been
unavailable for an inordinately long time and, if so, sem_unlink() it.
Is there a better way to do this? I haven't been able to find any in-
depth articles on Mac OS X semaphores (although I see that Amit
Singh's book "Mac OS X Internals" has a sub-chapter on it).
Thank you,
Jerry Krinock
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-kernel mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden