• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: What if process crashes while holding a locked semaphore?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: What if process crashes while holding a locked semaphore?


  • Subject: Re: What if process crashes while holding a locked semaphore?
  • From: David Gatwood <email@hidden>
  • Date: Mon, 30 Mar 2009 14:28:32 -0700

On Mar 28, 2009, Jerry Krinock wrote:

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.


If you insist on doing it this way, you could add a signal handler.  In a trap handler for SIGSEGV and SIGBUS, call sem_unlink and sem_close.  See man sigaction for info.

That said, this definitely isn't the right way to use a semaphore....  A semaphore is by nature cross-process, which means that the process quitting (or crashing) doesn't cause it to be destroyed.  As such, you can't really use access to a semaphore as a gating mechanism in any useful way.  The right way to do this is to treat the semaphore as a mutex, e.g.:

// Create a semaphore with an initial value of 1 if it does not exists or just open the existing semaphore if it does.
descriptor = sem_open("com.mycompany.myproduct.mysemaphore", (O_CREAT), S_IRWXU,  1) ;

if (descriptor)) {
//  Wait until the semaphore value is greater than zero, then decrement the semaphore value.
sem_wait(descriptor);

// Do work here.

sem_post(descriptor);
sem_close(descriptor);
}

If you do that, it should work as Terry describes.  You probably should not be calling sem_unlink at all.


David

 _______________________________________________
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

  • Follow-Ups:
    • Re: What if process crashes while holding a locked semaphore?
      • From: Jerry Krinock <email@hidden>
  • Prev by Date: Re: debuggging a recurring system freeze (syslog redir, ktrace?)
  • Next by Date: Re: debuggging a recurring system freeze (syslog redir, ktrace?)
  • Previous by thread: Re: What if process crashes while holding a locked semaphore?
  • Next by thread: Re: What if process crashes while holding a locked semaphore?
  • Index(es):
    • Date
    • Thread