---
name: "sem_wait"
section: "3"
platform: "freebsd"
package: "base"
version: "16.0-CURRENT-20261027-61c70dc16a79"
release: "FreeBSD 16.0-CURRENT (2026-10-27)"
platforms: ["linux", "freebsd"]
url: "https://new.carta.tech/sem_trywait"
---

# sem\_wait(3)

decrement (lock) a semaphore

Part of [sem\_trywait(3)](/sem_trywait/3.md).

## NAME

**sem\_wait**, **sem\_trywait** – decrement (lock) a semaphore

## LIBRARY

Standard C Library (libc, -lc)

## SYNOPSIS

**#include \<semaphore.h\>**

*int*\

**sem\_wait**(*sem\_t \*sem*);

*int*\

**sem\_trywait**(*sem\_t \*sem*);

## DESCRIPTION

The **sem\_wait**() function decrements (locks) the semaphore pointed to by *sem*, but blocks if the value of *sem* is zero, until the value is non-zero and the value can be decremented.

The **sem\_trywait**() function decrements (locks) the semaphore pointed to by *sem* only if the value is non-zero. Otherwise, the semaphore is not decremented and an error is returned.

## RETURN VALUES

The **sem\_wait()** function returns the value 0 if successful; otherwise the value -1 is returned and the global variable errno is set to indicate the error.

## ERRORS

The **sem\_wait**() and **sem\_trywait**() functions will fail if:

- \[EINVAL\]

  The *sem* argument points to an invalid semaphore.

Additionally, **sem\_wait**() will fail if:

- \[EINTR\]

  A signal interrupted this function.

Additionally, **sem\_trywait**() will fail if:

- \[EAGAIN\]

  The semaphore value was zero, and thus could not be decremented.

## SEE ALSO

[sem\_getvalue(3)](/sem_getvalue/3.md), [sem\_post(3)](/sem_post/3.md), [sem\_timedwait(3)](/sem_timedwait/3.md)

## STANDARDS

The **sem\_wait**() and **sem\_trywait**() functions conform to ISO/IEC 9945-1:1996 (“POSIX.1”).
