SEM_INIT(3P)
π SEM_INIT(3P) β the POSIX Programmer's Manual era π
okay bestie, welcome to the POSIX Programmer's Manual section of the lore π«Ά this is SEM_INIT(3P) and she's here to set up an unnamed semaphore for you. slay.
β οΈ PROLOG β οΈ
so before we even get into it β this manual page is part of the POSIX Programmer's Manual. the Linux implementation of this interface may differ (so like, lowkey go read the corresponding Linux manual page for the deets on Linux behavior), or honestly the interface might not even be implemented on Linux at all. π vibe check your platform first, fr fr.
β¨ NAME β¨
sem_init β initialize an unnamed semaphore
like, she's the birth of a semaphore. the origin story. the main character's first scene. π
π SYNOPSIS π
#include <semaphore.h>
int sem_init(sem_t *sem, int pshared, unsigned value);
three args, one little include, and you're off. π¦
π DESCRIPTION π
okay so the sem_init() function shall initialize the unnamed semaphore referred to by sem. and the value of the initialized semaphore? that's gonna be value. that's it. that's the whole vibe. β¨
following a successful call to sem_init(), the semaphore may be used in subsequent calls to sem_wait(), sem_timedwait(), sem_trywait(), sem_post(), and sem_destroy(). and this semaphore shall remain usable until the semaphore is destroyed β so like, she stays iconic until you say goodbye. no cap.
now the pshared tea β :
- if the
psharedargument has a non-zero value, then the semaphore is shared between processes; in this case, any process that can access the semaphoresemcan usesemfor performingsem_wait(),sem_timedwait(),sem_trywait(),sem_post(), andsem_destroy()operations. so like, cross-process besties. π - if the
psharedargument is zero, then the semaphore is shared between threads of the process; any thread in this process can usesemfor performingsem_wait(),sem_timedwait(),sem_trywait(),sem_post(), andsem_destroy()operations. so like, in-house besties only. π«Ά
also, see Section 2.9.9, Synchronization Object Copies and Alternative Mappings for further requirements. iykyk, go read it, she's important. π
π¨ okay this next bit is the serious one: attempting to initialize an already initialized semaphore results in undefined behavior. not "it'll probably be fine" behavior. not "just try it" behavior. undefined. behavior. do not do this. not me warning you twice. π
πΈ RETURN VALUE πΈ
upon successful completion, the sem_init() function shall initialize the semaphore in sem and return 0.
otherwise, it shall return -1 and set errno to indicate the error. π classic.
π₯ ERRORS π₯
the sem_init() function shall fail if:
EINVALβ thevalueargument exceeds{SEM_VALUE_MAX}. bestie you overshot the ceiling. πENOSPCβ a resource required to initialize the semaphore has been exhausted, or the limit on semaphores ({SEM_NSEMS_MAX}) has been reached. the semaphore budget said no more. πEPERMβ the process lacks appropriate privileges to initialize the semaphore. she said who authorized this. π
and then: the following sections are informative. so like, they're the bonus content, not the rules. π
π§ EXAMPLES π§
None.
(that's it. that's the section. she's mysterious. she's giving nothing and everything. β)
π APPLICATION USAGE π
None.
(again. nothing. rest era. π )
π RATIONALE π
None.
(they really said no thoughts, head empty for all three of these. iconic honestly. π)
β FUTURE DIRECTIONS β
None.
(the future is wide open and unspecified, bestie. π¦)
π SEE ALSO π
sem_destroy()sem_post()sem_timedwait()sem_trywait()
and also, from The Base Definitions volume of POSIX.1β2017: <semaphore.h> π
π COPYRIGHT π
portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group.
in the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. (she's the final boss, the one who decides who wins the argument, periodt.) the original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. to report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html . π
okay that's the whole page, we ate and left no crumbs. π«Ά this has been IEEE/The Open Group, 2017 β signing off, SEM_INIT(3P), mwah π
β¨
π¦ where this page comes from
- alpine 3.22: man-pages-posix 2017a-r0 Β· custom
- alpine 3.23: man-pages-posix 2017a-r0 Β· custom
- alpine 3.24: man-pages-posix 2017a-r0 Β· custom
- arch rolling: man-pages 6.19-1
- 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-posix 2017a
- opensuse-leap 16.0: man-pages-posix 2017a-160000.4.1 Β· SUSE-IEEE
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms