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โvalueexceedsSEM_VALUE_MAX. she said too much ๐ENOSYSโpsharedis nonzero, but the system does not support process-shared semaphores (peepsem_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
- Copyright 2006, Michael Kerrisk <mtk.manpages@gmail.com>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms