UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » Processes
  • » automatically remove semaphore on process crash?

#1 2007-11-24 02:07 PM

thinking
Member
Registered: 2005-09-15
Posts: 103

Re: automatically remove semaphore on process crash?

[email protected]

i currently developed a simple c++ synchronization class which uses semaphores for locking
my previous need was synchronization within one process, so i used a simple pthread mutex
related to semaphores i have one question:
as far as i know, a pthread mutex is implemented as a semaphore (don't know if posix or sysv) AND the mutex and thus it's semaphore is removed on process crash (e.g. segfault)
vom richard stevens book and my current expirience i know that semaphores are not automatically removed by the kernel
so the question is: why is a mutex cleaned up and my semaphores not?
my only guess is that a mutex uses IPC_PRIVATE but i didn't find documentation which answers this specific question (and man pages are sometimes hard to understand ^^)

thx for your time

Offline

#2 2007-11-24 05:10 PM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: automatically remove semaphore on process crash?

I believe the reason is that a semaphore is typically used by multiple processes, and you often want the semaphore to remain in place even after the process that created it is gone, so that other processes can continue to use it.  If it were automatically destoyed when the originating process went away, that would likely cause problems for the other processes that were using it at the time (or would want to use it later).

Offline

#3 2007-11-24 05:39 PM

thinking
Member
Registered: 2005-09-15
Posts: 103

Re: automatically remove semaphore on process crash?

sorry, my question was a bit misleading :)
i understand the reason why it is done, but i just wanted to know how?
how is a mutext technically different than a semaphore?
IPC_CREATE on a sysv semaphore can't be right, cause it doesn't get deleted after program exit

i just had a look at libpthread and it seems that it doesn't use semaphores
i tought it would, maybe an old implementation did
it seems that pthread_mutex_lock uses a glibc function: __arch_compare_and_exchange_val_32_acq
which uses assembler for an atomic exchange of memory to lock the mutex

#define __arch_compare_and_exchange_val_32_acq(mem, newval, oldval) \
  ({ __typeof (*mem) ret;						      \
     __asm __volatile (LOCK_PREFIX "cmpxchgl %2, %1"			      \
		       : "=a" (ret), "=m" (*mem)			      \
		       : "r" (newval), "m" (*mem), "0" (oldval));	      \
     ret; })

seems i have to use pthread_mutex for my case

but thx jfriesne

Offline

#4 2007-12-04 01:03 AM

mlampkin
Administrator
From: Sol 3
Registered: 2002-06-12
Posts: 911
Website

Re: automatically remove semaphore on process crash?

Actually Stevens wasn't incorrect taken in the context of when that information was written...

The truth is way back in the dark ages when the first copies of those texts were originally written a lot of processors did NOT have integrated full lock / look / compare instructions e.g. the cmpxchg operator on Intel processors you noticed in the source code... that meant they were quite often implemented in the same way / as an overlay to existing semaphore implementations...

As the processors evolved and the majority got cmpxchg or similar commands integrate... the mutex and semaphore commands began diverging... and now on most - if not all - systems / libraries for those tasks are completely separate beasts...


Michael


"The only difference between me and a madman is that I'm not mad."

Salvador Dali (1904-1989)

Offline

#5 2007-12-04 04:25 PM

darthsidious
Member
Registered: 2007-11-18
Posts: 14

Re: automatically remove semaphore on process crash?

Offline

#6 2007-12-06 01:18 PM

thinking
Member
Registered: 2005-09-15
Posts: 103

Re: automatically remove semaphore on process crash?

@mlampkin
good to know ^^
thx for the info

@darthsidious
as i currently read (in stevens book) posix semaphores are not removed if the process dies (no matter how)
for posix semaphores you have sem_close and sem_unlink and it seems to work like a file
the kernel has a reference count which equals how often sem_open has been called for that semaphore
if everyone called sem_close (which is also called automatically if the process dies) the semaphore is not removed even if the reference count is 0
it is only removed on sem_unlink AND reference count is 0
just like a simple file

a sysV semaphore doesn't have such sem_close if the process dies but you have SEM_UNDO which can bring the semaphore in a consistent state which means another process could do the cleanup

for my problem i finally used sysV semaphores to synchronize unrelated processes and it works perfectly
i couldn't (didn't want) use posix semaphores cause i also have different locktypes (shared and exclusive) for which i used counting semaphores and not posix

Offline

  • Index
  • » Processes
  • » automatically remove semaphore on process crash?

Board footer

Powered by FluxBB