sema_wait(3synch)


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(3synch)).

Usage

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

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(3synch), semaphore(3synch), sema_destroy(3synch), sema_init(3synch), sema_post(3synch), sema_trywait(3synch)
© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 25 April 2004