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: Jerry Krinock <email@hidden>
- Date: Sat, 28 Mar 2009 19:57:46 -0700
On 2009 Mar 28, at 18:13, Terry Lambert wrote:
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.
Thanks, Terry. I must be using them incorrectly then. My proof-of-
concept demo tool alternately opens, and then unlink+closes the
semaphore every few seconds.
If I crash the tool by clicking "Stop" in Xcode while it has the
semaphore "open", then re-launch, the new process cannot open the same-
named semaphore. But if I crash the tool after it has unlink+closed
the semaphore, then the new process ^can^ open the semaphore.
Here is my code, with error and retry recovery removed for
readability...
int main ()
{
sem_t* descriptor ;
// Try to acquire semaphore
descriptor = sem_open("MySemaphore", (O_CREAT|O_EXCL), S_IRWXU,
1) ;
if (descriptor != SEM_FAILED)
{
// Do work
...
// Work is done
sem_unlink("MySemaphore") ;
sem_close(descriptor) ;
}
return 0 ;
}
Is this incorrect? I've seen code published where someone unlinked
immediately after opening, but when I tried that, other processes
could acquire the semaphore while the work was going on, which is no
good.
_______________________________________________
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