| Main index | Section 9 | 日本語 | Options |
#include <sys/types.h>
#include <sys/lock.h>
#include <sys/sema.h>
Semaphores should not be used where mutexes and condition variables will suffice. Semaphores are a more complex synchronization mechanism than mutexes and condition variables, and are not as efficient.
Semaphores are created with sema_init(), where sema is a pointer to space for a struct sema, value is the initial value of the semaphore, and description is a pointer to a null-terminated character string that describes the semaphore. Semaphores are destroyed with sema_destroy(). A semaphore is posted (incremented) with sema_post(). A semaphore is waited on (decremented) with sema_wait(), sema_timedwait(), or sema_trywait(). The timo argument to sema_timedwait() specifies the minimum time in ticks to wait before returning with failure. sema_value() is used to read the current value of the semaphore.
If decrementing the semaphore would result in its value being negative, sema_trywait() returns 0 to indicate failure. Otherwise, a non-zero value is returned to indicate success.
The sema_timedwait() function returns 0 if waiting on the semaphore succeeded; otherwise a non-zero error code is returned.
| [EWOULDBLOCK] | |
| Timeout expired. | |
| SEMA (9) | February 1, 2006 |
| Main index | Section 9 | 日本語 | Options |
Please direct any comments about this manual page service to Ben Bullock. Privacy policy.
| “ | "I liken starting one's computing career with Unix, say as an undergraduate, to being born in East Africa. It is intolerably hot, your body is covered with lice and flies, you are malnourished and you suffer from numerous curable diseases. But, as far as young East Africans can tell, this is simply the natural condition and they live within it. By the time they find out differently, it is too late. They already think that the writing of shell scripts is a natural act." | ” |
| — Ken Pier, Xerox PARC | ||