DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 

sema_wait(SYNCH)


sema_wait -- acquire a semaphore

Synopsis

   cc [options] -Kthread file
   

#include <synch.h>

int sema_wait(sema_t *sema);

Description

sema_wait acquires the semaphore pointed to by sema.

If the semaphore is available (that is, if the semaphore value is greater than zero), sema_wait decrements the semaphore value and returns to the caller.

If the semaphore is unavailable (that is, the semaphore value is zero or less), sema_wait decrements the semaphore value and suspends execution of the calling thread until the semaphore becomes available to the caller.

If a thread waiting on a semaphore is interrupted by a signal, sema_wait returns EINTR.

Parameters


sema
pointer to the semaphore to acquire

sema parameter

sema must previously have been initialized, either by sema_init or statically (see sema_init(SYNCH)).

Usage

See the description of semaphores under USAGE on semaphore(SYNCH).

In general, sema_wait is used to block wait for an event, or when a critical section is long. Semaphores are asynchronous-safe, and can be used to communicate between signal handlers and base level code.

Return values

sema_wait returns zero for success and an error number for failure.

Errors

If the following condition is detected, sema_wait returns the corresponding value:

EINVAL
invalid argument specified

EINTR
semaphor waiting was interrupted by a UNIX system signal

References

Intro(SYNCH), semaphore(SYNCH), sema_destroy(SYNCH), sema_init(SYNCH), sema_post(SYNCH), sema_trywait(SYNCH)
© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 - 01 June 2005