sem_init(3)

sem_init3

Initialize an unnamed semaphore

Usage

#include <semaphore.h>

int

sem_init(sem_t *sem, int pshared, unsigned int value);

Library

Standard C Library (libc, -lc)

Overview

The sem_init() function initializes the unnamed semaphore pointed to by sem to have the value value.

A non-zero value for pshared specifies a shared semaphore that can be used by multiple processes, the semaphore should be located in shared memory region (see mmap(2), shm_open(2), and shmget(2)), any process having read and write access to address sem can perform semaphore operations on sem.

Following a successful call to sem_init(), sem can be used as an argument in subsequent calls to sem_wait(3), sem_trywait(3), sem_post(3), and sem_destroy(3). The sem argument is no longer valid after a successful call to sem_destroy(3).

Return values

The sem_init() 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_init() function will fail if:

[EINVAL]

The value argument exceeds SEM_VALUE_MAX.

[ENOSPC]

Memory allocation error.

Releases

70 releases since FreeBSD 4.0-RELEASE; the text changed 41 times.

  1. 4.0
  2. 4.1–4.2
  3. 4.3
  4. 4.4
  5. 4.5–4.11
  6. 5.0
  7. 5.1–5.2.1
  8. 5.3–5.4
  9. 5.5
  10. 6.0–6.3
  11. 6.4
  12. 7.0
  13. 7.1
  14. 7.2
  15. 7.3
  16. 7.4
  17. 8.0
  18. 8.1
  19. 8.2
  20. 8.3
  21. 8.4
  22. 9.0
  23. 9.1
  24. 9.2
  25. 9.3
  26. 10.0
  27. 10.1
  28. 10.2
  29. 10.3
  30. 10.4
  31. 11.0
  32. 11.1
  33. 11.2
  34. 11.3
  35. 11.4
  36. 12.0
  37. 12.1
  38. 12.2
  39. 12.3
  40. 12.4
  41. 13.0–13.2
  42. 13.3–16.0

Standards

The sem_init() function conforms to ISO/IEC 9945-1:1996 (“POSIX.1”).

From base 16.0-CURRENT-20261027-61c70dc16a79 in FreeBSD 16.0-CURRENT (2026-10-27). Parsed by carta from the page's mdoc source.