Re: What if process crashes while holding a locked semaphore?
Re: What if process crashes while holding a locked semaphore?
- Subject: Re: What if process crashes while holding a locked semaphore?
- From: Terry Lambert <email@hidden>
- Date: Sat, 28 Mar 2009 18:13:15 -0700
On Mar 28, 2009, at 5:50 PM, Jerry Krinock wrote:
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).
These are POSIX semaphores and not specific to Mac OS X. They are
resource tracked (meaning they will be cleaned up when a process
crashes, assuming you are using them correctly.
-- Terry
_______________________________________________
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