Posix Semaphoe in Linux

Posix Semaphoe in Linux

macro defined
_POSIX_SEMAPHORES
Header file
#include <semaphore.h>

Types of semaphore in Posix
1.
unnamed semaphore
use in related process, parnet-chiled, multithreaded process. 2.
named semaphore
can use in unrelated process and related process.

APIs for semaphores in Posix

unnamed semaphore
sem_init — Initialize a POSIX unnamed semaphore.
int sem_init(sem_t *sem_location, int pshared,unsigned int value);
/* sem_location: memory address where semaphore initlized, phsared : is this shared by multiple process 0=no sharing, 1=sharing. value : initial value of semaphore but never negative.number of process can lock this semaphore. */ sem_destroy — Deinitialize a POSIX unnamed semaphore.
int sem_destroy(sem_t *sem_location);

named semaphore
sem_open — Create/access a POSIX named semaphore.
sem_t * sem_open(char *sem_name, int oflags,mode_t mode, unsigned int value);
sem_close—Terminate access to a POSIX named semaphore. // close opend semaphore instance.
int sem_close(sem_t *sem);
sem_unlink—Destroy a POSIX named semaphore. // remove semaphore, only opned semaphore will continue work untill they closed.
int sem_unlink(char *semname);

Operation on semaphore
sem_getvalue—Get the value of a POSIX semaphore (named or unnamed). // get semaphore value // if positive unblocked, zero blocked.
int sem_getvalue(sem_t *sem, int *value);
sem_wait, sem_trywait—Wait on a POSIX semaphore (named or unnamed). // lock semaphore.
int sem_wait(sem_t *sem);
// blocking
int sem_trywait(sem_t *sem);
// non blocking sem_post—Post (signal) a POSIX semaphore (named or unnamed). // unlock semaphore.
int sem_post(sem_t *sem);

Difference Between Semaphores and Mutex
noticeable differences between semaphore and Mutex. 1. A semaphore can be a Mutex but a Mutex can never be semaphore. This simply means that a binary semaphore can be used as Mutex, but a Mutex can never exhibit the functionality of semaphore. 2. Both semaphores and Mutex (at least the on latest kernel) are non-recursive in nature. 3. No one owns semaphores, whereas Mutex are owned and the owner is held responsible for them. This is an important distinction from a debugging perspective. 4. In case the of Mutex, the thread that owns the Mutex is responsible for freeing it. However, in the case of semaphores, this condition is not required. Any other thread can signal to free the semaphore by using the sem_post() function. 5. A Mutex, by definition, is used to serialize access to a section of re-entrant code that cannot be executed concurrently by more than one thread. A semaphore, by definition, restricts the number of simultaneous users of a shared resource up to a maximum number 6. Another difference that would matter to developers is that semaphores are system-wide and remain in the form of files on the filesystem, unless otherwise cleaned up. Mutex are process-wide and get cleaned up automatically when a process exits. 7. The nature of semaphores makes it possible to use them in synchronizing related and unrelated process, as well as between threads. Mutex can be used only in synchronizing between threads and at most between related processes (the pthread implementation of the latest kernel comes with a feature that allows Mutex to be used between related process). 8. According to the kernel documentation, Mutex are lighter when compared to semaphores. What this means is that a program with semaphore usage has a higher memory footprint when compared to a program having Mutex. 9. From a usage perspective, Mutex has simpler semantics when compared to semaphores.
referenced : linuxdevcenter.com


No comments:

Post a Comment

would you like it. :)