sem_init(3)

โœจ sem_init(3) โœจ โ€” from the Library Functions Manual, bestie ๐Ÿ’…

okay so picture this: you've got a semaphore but it's โœจunnamedโœจ and it hasn't been born yet. this page is literally the birth announcement. let's get into it ๐Ÿซถ


๐ŸŽ€ NAME ๐ŸŽ€

sem_init โ€” initialize an unnamed semaphore


๐Ÿ“š LIBRARY ๐Ÿ“š

POSIX threads library โ€” link it with libpthread, -lpthread ๐ŸŒธ


๐Ÿ‘‘ SYNOPSIS ๐Ÿ‘‘

#include <semaphore.h>

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

๐Ÿ’– DESCRIPTION ๐Ÿ’–

sem_init() initializes the unnamed semaphore sitting at the address that sem points to. and the value argument? that's the starting value for the semaphore, no cap. ๐Ÿ”ฅ

the pshared argument is the vibe check that tells you whether this semaphore is gonna be shared between the threads of one process, or between processes. main character decisions here โœจ

if pshared has the value 0, then the semaphore is shared between the threads of a process, and it should live at some address that's visible to all those threads โ€” think a global variable, or a variable allocated dynamically on the heap. ๐Ÿงš

if pshared is nonzero, then the semaphore is shared between processes, and it should be located in a region of shared memory โ€” see shm_open(3), mmap(2), and shmget(2). ๐Ÿฆ‹ (and since a child created by fork(2) inherits its parent's memory mappings, it can also access the semaphore, periodt.) any process that can access the shared memory region can operate on the semaphore using sem_post(3), sem_wait(3), and so on. ๐Ÿ’œ

โš ๏ธ big scary one: initializing a semaphore that has already been initialized results in undefined behavior. not a vibe. do not do it. ๐Ÿ˜ญ

๐ŸŒˆ RETURN VALUE ๐ŸŒˆ

sem_init() returns 0 on success. on error, -1 is returned, and errno is set to indicate the error. iconic behavior honestly ๐Ÿ‘‘


๐Ÿ’€ ERRORS ๐Ÿ’€

  • EINVAL โ€” value exceeds SEM_VALUE_MAX. she said too much ๐Ÿ“
  • ENOSYS โ€” pshared is nonzero, but the system does not support process-shared semaphores (peep sem_overview(7)). the system simply cannot serve this look ๐Ÿ™ˆ

โญ ATTRIBUTES โญ

for an explanation of the terms used in this section, see attributes(7).

  • Interface: sem_init()
  • Attribute: Thread safety
  • Value: MT-Safe

so yeah โ€” sem_init() is MT-Safe, she's thread safe, we love that for her ๐Ÿ’…


๐ŸŒธ STANDARDS ๐ŸŒธ

POSIX.1-2008. ๐ŸŽ€


๐Ÿฆ‹ HISTORY ๐Ÿฆ‹

POSIX.1-2001.

and here's the tea โ˜• โ€” bizarrely, POSIX.1-2001 does not specify the value that should be returned by a successful call to sem_init(). POSIX.1-2008 rectifies this, specifying the zero return on success. glow-up era โœจ


๐Ÿ”ฅ EXAMPLES ๐Ÿ”ฅ

see shm_open(3) and sem_wait(3).


๐Ÿ’œ SEE ALSO ๐Ÿ’œ

sem_destroy(3), sem_post(3), sem_wait(3), sem_overview(7)


okay that's the whole page, you ate that ๐Ÿ’… this was brought to you by Linux man-pages 6.20, dated 2026-02-08, and yes that date is serving future energy. bye bestie, go init responsibly ๐Ÿซถโœจ

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: Linux-man-pages-copyleft
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms