pthread_mutex_init v. sem_init v. sem_get


 
Thread Tools Search this Thread
Top Forums Programming pthread_mutex_init v. sem_init v. sem_get
# 1  
Old 06-13-2006
pthread_mutex_init v. sem_init v. sem_get

Apparently there are three similar features in the linux API. Can someone explain the difference between the data structures that are initialized by these functions:

(1) pthread_mutex_init
(2) sem_init
(3) sem_get

Thanks,
Siegfried
# 2  
Old 06-14-2006
A mutex is a mutal-exclusion device specifically designed to let one and only one thread have access to a certain code path at a time. If another thread tries to lock the mutex when it's already locked, that thread will be suspended until the first thread unlocks it.

Mutexes keep track of which thread "owns" them. An unlocked mutex is owned by nobody, once a thread locks it, it belongs to that thread; when they unlock it, it belongs to nobody again. Only the thread that locked the mutex is allowed to unlock it, and if it's owner tries to lock it again without unlocking it, the second lock will fail. Don't depend on this owner-checking, because some types of 'fast' mutexes dispense with it for efficiency.

here is a good example of how this kind of mutual exclusion is used -- it uses a slightly different thread API but the concept is exactly the same.

A semaphore is different from a mutex. Semaphores can store a range of values from zero to the maximum integer size, and don't track owners at all.

How a semaphore works is, when you "wait" on a sem, it attempts to vecrement the integer value. It will not let the count become negative -- if the integer value is zero, it will instead suspend the calling thread. The thread will stay suspended until something else "posts" on the semaphore, incrementing it and thus freeing the first waiting thread to2decrement it.

So if you had five threads waiting on one semaphore, you could let them go one at a time by posting to the sem 5 times.

One last difference between mutexes and semaphores is, semaphores aren't just thread-safe, they're even signal-safe. If for some reason you have to use thread synchronization inside signal handlers, they have to be semaphores. Otherwise you'll cause deadlocks.

I can't find sem_get anywhere in my system's manual pages. I'm not familiar with it.

Last edited by Corona688; 06-14-2006 at 02:28 PM..
# 3  
Old 06-14-2006
Corona --
mutex= mutual exclusion sempahore Smilie

I don't know sem_get either just (for unamed sempahores)-
sem_destroy(2), sem_post(2), sem_trywait(2), sem_wait(2), sem_init(2)

sem_open(2) and sem_getvalue(2) for named semaphores.

Semaphores also work across processes as well as threads. Processes access them via name and sem_open.
# 4  
Old 06-14-2006
semaphore.h v. sys/sem.h

semaphore.h defines sem_init and sys/sem.h defines semget.

Both functions seem to work on windows/cygwin and SuSE. I'm studying the sample code from the Wrox Press book "Beginning Linux Programming" 3rdEdition which gives examples that work on both SuSE and windows/cygwin. See http://www.wrox.com/WileyCDA/WroxTit...764544977.html .

Apparently, sem_init does not work across process boundaries for linux (according to my SuSE manpage) and sem_get does (for linux).

sem_init seems to be documented at http://www.opengroup.org/pubs/online.../sem_init.html.

semget seems to be documented at http://www.opengroup.org/pubs/online...sh/semget.html

Why are their two APIs for semaphores, especially when they both work across process boundaries (for nonlinux implementations).

Can someone please explain to me what the last argument (named "value") for sem_init is for? Is this the same in concept as the second argument (named nsems) in semget?

In native windows land (not cygwin) I can lock a resource with a semaphore just like I can with a unix or windows mutex. In native windows land a mutex is just a binary semaphore meaning that only a single thread can have access to the resource at one time. In Windows land, one uses a semaphore instead of a mutex when a maximum of "n" threads may acess a single resource simultaneously (as might be used for throttling access to a network resource). Is this true of sem_init and semget for linux and cygwin/windows?

Sometimes, however, I don't want to lock a resource. For example, let's suppose I want to reinvent apache httpd with synchronous sockets and a thread pool implemented with an array of pthread_t. Every thread is initialized with a threadproc and all the threads (initially) sit around blocked waiting for a green light. Now the main thread (not part of the thread pool) blocks on the listen and accept functions. When a remote socket client connects (such as a web browser), the server's main thread returns from the accept function and gives a green light to one of the threads sitting around idle in the thread pool.

How would I implement the green light? In windows I would use an "event" object (which is like a mutex that automatically unlocks it self immediately after being locked) and not a semaphore or a mutex. What should I use in linux?

thanks,
Siegfried
# 5  
Old 06-14-2006
Events are analogous to System V message queues. And events do not unlock themselves, they are messages, from the message pump - IMO. -- and Charles Petzold as well.

Suggestion: try this url
http://www.advancedlinuxprogramming.com/downloads.html
Read chapter 5 on Interprocess Communication.
# 6  
Old 06-15-2006
Quote:
Originally Posted by jim mcnamara
Semaphores also work across processes as well as threads. Processes access them via name and sem_open.
I'm not certain if they work across processes on Linux except for the IPC ones, and I know they don't work across processes on MacOS X. Which makes the 'named' part of 'named semaphores' pretty pointless, doesn't it?
Login or Register to Ask a Question

Previous Thread | Next Thread

1 More Discussions You Might Find Interesting

1. Programming

Undefined: sem_init, sem_post, sem_wait

Hi friends, I am using semaphores in my program, but when I compile the program, it gives the following error $ gcc sem.c -o sem -lpthread Undefined first referenced symbol in file sem_init ... (1 Reply)
Discussion started by: gabam
1 Replies
Login or Register to Ask a Question