sem_open(3)
β¨ sem_open(3) β¨
okay bestie, so you've found yourself in the Library Functions Manual section, and you're about to meet sem_open(3) β the girl who creates and opens named semaphores. she's lowkey the main character of the POSIX semaphore friend group. let's get into it π«Ά
π NAME π
sem_open β initialize and open a named semaphore
π LIBRARY π
POSIX threads library (libpthread, -lpthread)
π SYNOPSIS π
#include <fcntl.h> /* For O_* constants */
#include <sys/stat.h> /* For mode constants */
#include <semaphore.h>
sem_t *sem_open(const char *name, int oflag, ...
/* mode_t mode, unsigned int value */ );
πΈ DESCRIPTION πΈ
sem_open() is giving two girlboss jobs in one: it creates a brand-new POSIX semaphore, or it opens one that already exists. the semaphore is identified by name. and if you wanna know all the juicy rules about how name gets constructed? that's a whole other episode β go read sem_overview(7), iykyk.
the oflag argument is the vibe-setter: it's the flags that control how the call behaves. (you get the definitions of the flag values by including <fcntl.h>, periodt.) so here's the tea:
- If
O_CREATis specified inoflag, then the semaphore gets created if it doesn't already exist. β¨ - The owner (user ID) of the semaphore is set to the effective user ID of the calling process.
- The group ownership (group ID) is set to the effective group ID of the calling process.
- If both
O_CREATandO_EXCLare specified inoflag, then an error gets returned if a semaphore with the givennamealready exists. no dupes allowed bestie, we don't do that here π
now, if O_CREAT is specified in oflag, plot twist: you gotta supply two additional arguments. yes ma'am, two of them:
modeβ specifies the permissions to be placed on the new semaphore, exactly as foropen(2). (you can grab the symbolic definitions for the permissions bits by including<sys/stat.h>.) the permissions settings are masked against the processumask. and listen β both read and write permission should be granted to each class of user that will access the semaphore. everybody eats, that's the rule πvalueβ specifies the initial value for the new semaphore.
π and here's the part that gets people: ifO_CREATis specified, and a semaphore with the givennamealready exists, thenmodeandvalueare ignored. completely. she said "i already know her" and moved on.
π RETURN VALUE π
- On success,
sem_open()returns the address of the new semaphore β you hold onto this address and use it when you call other semaphore-related functions. it's like her number, keep it π - On error,
sem_open()returnsSEM_FAILED, witherrnoset to indicate the error.
π ERRORS π
EACCESβ the semaphore exists, but the caller does not have permission to open it. πEEXISTβ bothO_CREATandO_EXCLwere specified inoflag, but a semaphore with this name already exists.EINVALβvaluewas greater thanSEM_VALUE_MAX.EINVALβnameconsists of just"/", followed by no other characters.EMFILEβ the per-process limit on the number of open file descriptors has been reached.ENAMETOOLONGβnamewas too long.ENFILEβ the system-wide limit on the total number of open files has been reached.ENOENTβ theO_CREATflag was not specified inoflagand no semaphore with this name exists; or,O_CREATwas specified, butnamewasn't well formed.ENOMEMβ insufficient memory. π
π ATTRIBUTES π
for an explanation of the terms used in this section, see attributes(7).
- Interface:
sem_open() - Attribute: Thread safety
- Value:
MT-Safe
so yeah, she's MT-Safe β multithread-safe, iconic behaviour, no cap π₯
β STANDARDS β
POSIX.1-2008.
π¦ HISTORY π¦
POSIX.1-2001.
π§ SEE ALSO π§
sem_close(3), sem_getvalue(3), sem_post(3), sem_unlink(3), sem_wait(3), sem_overview(7)
and that's the whole page bestie, you ate that π
sign-off time: this little love letter to semaphores comes from the Linux man-pages crew, version 6.20, dated 2026-02-08. go forth and semaphore 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