Thread: Problems with POSIX semaphores.

  1. #1
    Registered User
    Join Date
    Jun 2007

    Unhappy Problems with POSIX semaphores.

    Hello guys.
    I am having a hard time with the behaviour of POSIX semaphores.
    Please, take a look at the following code: (Error checking omitted for the sake of simplicity.)

    #include <fcntl.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <semaphore.h>
    int main( void )
      sem_t *sem;
      sem = sem_open( "/MySemaphore", O_CREAT, 0666, 1 );
        sem_wait( sem );
        printf( "Semaphore locked!\n" );
        sleep( 5 );
        sem_post( sem );
        printf( "Semaphore unlocked!\n\n" );
        sleep( 1 );
      return 0;
    As you can see, this code creates a POSIX named semaphore (or connects to one if the semaphore has already been created by another process), and start to continuously lock it for 5 seconds, and then unlock it. The desired effect is that two or three instances of this program running concurrently will alternate the lock.

    The problem is:
    If one instance of the program dies (or is explicitly killed) while it has the lock, IT DOES NOT UNDO THE LOCK, and the other program instances will wait for the lock forever. This effectvely makes the semaphore useless, since it is locked forever and any new instances of the program will already start deadlocked. This behaviour does not happen with System V semaphores, which undo the lock automatically. Is this a limitation of POSIX semaphores, or am I doing something wrong?

    Hope you can help, this has been bugging me for days.
    Thank you a lot.

    (To compile this you have to link against libpthread: 'gcc code.c -lpthread')

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    You might be able to use atexit() to register a function which unlocks the semaphore, or you might have to trap SIGINT and unlock it from there. Those are my only suggestions. And I don't know how you could deal with SIGKILL -- I don't think you can. That's what SIGKILL is for.

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell

    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Apr 2007
    You could use sem_trywait to ensure the others don't deadlock, but as far as the SIGKILL goes....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Semaphores, need advice on implementation.
    By Swerve in forum C++ Programming
    Replies: 2
    Last Post: 01-13-2009, 01:54 AM
  2. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  3. semaphores
    By Dr Spud in forum C Programming
    Replies: 7
    Last Post: 09-22-2007, 12:45 PM
  4. Problem with POSIX semaphores
    By kaseo88 in forum C Programming
    Replies: 7
    Last Post: 06-29-2007, 12:31 PM
  5. Semaphores Problems
    By mhelal in forum Linux Programming
    Replies: 2
    Last Post: 05-06-2007, 10:36 PM