DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH
 

sem_wait(S)


sem_wait, sem_trywait -- lock a semaphore

Syntax

cc . . . -lsuds
#include <semaphore.h>

int sem_wait (sem_t *sem);

int sem_trywait (sem_t *sem);

Description

sem_wait- lock a semaphore

sem_trywait- conditionally lock a semaphore

The sem_wait function locks the semaphore referenced by sem by performing the semaphore lock operation on that semaphore. If the semaphore value is currently zero, then the calling process does not return from the call to sem_wait until it either locks the semaphore or the call is interrupted by a signal. The sem_trywait function locks the semaphore referenced by sem only if the semaphore is currently not locked; that is, if the semaphore value is currently positive. Otherwise, it does not lock the semaphore.

Upon successful return, the state of the semaphore is locked and shall remain locked until the sem_post(S) function is executed and returns successfully.

The sem_wait function is interruptible by the delivery of a signal.

Return values

The sem_wait and sem_trywait functions return zero if the calling process successfully performed the semaphore lock operation on the semaphore designated by sem. If the call was unsuccessful, the state of the semaphore is unchanged, and the function returns a value of -l and sets errno to indicate the error.

Diagnostics

If any of the following conditions occur, the sem_wait and sem_trywait functions return -1 and set errno to the corresponding value:

[EAGAIN]
The semaphore was already locked, so it cannot be immediately locked by the sem_trywait operation (sem_trywait only).

[EINVAL]
The sem argument does not refer to a valid semaphore.

[EINTR]
A signal interrupted this function.
For each of the following conditions, if the condition is detected, the sem_wait and sem_trywait functions return -1 and set errno to the corresponding value:

[EDEADLK]
A deadlock condition was detected.

See also

semaphore(FP), sem_post(S)

Standards conformance

Text reprinted and/or adapted from IEEE Std 1003.1b-1993, IEEE Standard for Information Technology, POSIX Part 1: System Application Program Interface (API) Amendment 1: Realtime Extensions [C Language], copyright © 1993 by the Institute of Electrical and Electronics Engineers, Inc. The IEEE takes no responsibility for and will assume no liability for damages resulting from the reader's misinterpretation of said information resulting from the placement and context in this publication. Information is reproduced with the permission of the IEEE.
© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003