POSIX semaphores ( a.k.a. sem_init(), sem_wait(), sem_post() ) on LINUX

POSIX semaphores ( a.k.a. sem_init(), sem_wait(), sem_post() ) on LINUX

Post by Shailesh Phansalk » Sun, 28 Jul 2002 11:45:32



Hello

Are the posix implementation of semaphores ( known as POSIX semaphores
) recursive on LINUX ?

In other words, if one thread calls sem_wait(); and then calls it
again, would it hang on itself ?

I am seeing such a symptom on my Red Hat LINUX 7.3/2.4.18-4smp.

Is there any way to make these recursive on LINUX ?

Thanks
- Shailesh

 
 
 

POSIX semaphores ( a.k.a. sem_init(), sem_wait(), sem_post() ) on LINUX

Post by Danny Dia » Mon, 29 Jul 2002 04:20:47



> Are the posix implementation of semaphores ( known as POSIX semaphores
> ) recursive on LINUX ?
> In other words, if one thread calls sem_wait(); and then calls it
> again, would it hang on itself ?

A semaphore is merely a counting/waiting mechanism.  If you use
sem_post() and increment the internal count, you should be able to
call sem_wait() that many times since each sem_wait() call merely
decrements the value.  

Are you calling sem_post() the same number of times as sem_wait()?

Quote:> Is there any way to make these recursive on LINUX ?

Semaphores by definition are not recursive like mutexes.  There
MUST be and equal number of sem_post() before sem_wait() or else
sem_wait() will block until a sem_post() is called.

-Danny

--
Danny Diaz         | "There are only 10 kinds of people in the

Civ3 *        |  who don't."  -- Unknown
Confidence is what you had before you understood the situation.