SEM_OPEN(3P)
π sem_open β POSIX Programmer's Manual, but make it a group chat π
okay bestie, we're doing sem_open(3P) today, from the POSIX Programmer's Manual, and honestly? it's giving β¨synchronizationβ¨. no cap.
πΈ PROLOG πΈ
so like, real talk first: this manual page is part of the POSIX Programmer's Manual. the Linux implementation of this interface may differ (consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux at all. π yes, that's the whole disclaimer, and yes, it matters.
π NAME π
sem_open β initialize and open a named semaphore
β¨ SYNOPSIS β¨
#include <semaphore.h>
sem_t *sem_open(const char *name, int oflag, ...);
the ... is doing main character energy, don't worry about it yet, we get there π
π¦ DESCRIPTION π¦
so sem_open() shall establish a connection between a named semaphore and a process. that's it, that's the vibe: you're matching your process with a semaphore. following a call to sem_open() with semaphore name name, the process may reference the semaphore associated with name using the address returned from the call. this semaphore may be used in subsequent calls to sem_wait(), sem_timedwait(), sem_trywait(), sem_post(), and sem_close().
the semaphore remains usable by this process until the semaphore is closed by a successful call to sem_close(), _exit(), or one of the exec functions. π«Ά like, it stays with you until you leave the party, one way or another.
the oflag argument controls whether the semaphore is created or merely accessed by the call to sem_open(). and the following flag bits may be set in oflag:
O_CREATβ this flag is used to create a semaphore if it does not already exist. ifO_CREATis set and the semaphore already exists, thenO_CREAThas no effect, except as noted underO_EXCL. otherwise,sem_open()creates a named semaphore. theO_CREATflag requires a third and a fourth argument: mode, which is of typemode_t, and value, which is of typeunsigned. the semaphore is created with an initial value of value. valid initial values for semaphores are less than or equal to{SEM_VALUE_MAX}.
the user ID of the semaphore shall be set to the effective user ID of the process. the group ID of the semaphore shall be set to the effective group ID of the process; however, if the name argument is visible in the file system, the group ID may be set to the group ID of the containing directory. the permission bits of the semaphore are set to the value of the mode argument except those set in the file mode creation mask of the process. when bits in mode other than the file permission bits are specified, the effect is unspecified. π
after the semaphore named name has been created by sem_open() with the O_CREAT flag, other processes can connect to the semaphore by calling sem_open() with the same value of name. friendship bracelet behavior fr fr. π
O_EXCLβ ifO_EXCLandO_CREATare set,sem_open()fails if the semaphore name exists. the check for the existence of the semaphore and the creation of the semaphore if it does not exist are atomic with respect to other processes executingsem_open()withO_EXCLandO_CREATset. ifO_EXCLis set andO_CREATis not set, the effect is undefined. π
if flags other than O_CREAT and O_EXCL are specified in the oflag parameter, the effect is unspecified.
the name argument points to a string naming a semaphore object. it is unspecified whether the name appears in the file system and is visible to functions that take pathnames as arguments. the name argument conforms to the construction rules for a pathname, except that the interpretation of <slash> characters other than the leading <slash> character in name is implementation-defined, and that the length limits for the name argument are implementation-defined and need not be the same as the pathname limits {PATH_MAX} and {NAME_MAX}.
if name begins with the <slash> character, then processes calling sem_open() with the same value of name shall refer to the same semaphore object, as long as that name has not been removed. if name does not begin with the <slash> character, the effect is implementation-defined. π
if a process makes multiple successful calls to sem_open() with the same value for name, the same semaphore address shall be returned for each such successful call, provided that there have been no calls to sem_unlink() for this semaphore, and at least one previous successful sem_open() call for this semaphore has not been matched with a sem_close() call. consistency? iconic. π
references to copies of the semaphore produce undefined results. do not do this. not even a little. π
π RETURN VALUE π
upon successful completion, the sem_open() function shall return the address of the semaphore. otherwise, it shall return a value of SEM_FAILED and set errno to indicate the error. the symbol SEM_FAILED is defined in the <semaphore.h> header. no successful return from sem_open() shall return the value SEM_FAILED. periodt.
π ERRORS π
if any of the following conditions occur, the sem_open() function shall return SEM_FAILED and set errno to the corresponding value:
EACCESβ the named semaphore exists and the permissions specified by oflag are denied, or the named semaphore does not exist and permission to create the named semaphore is denied.EEXISTβO_CREATandO_EXCLare set and the named semaphore already exists.EINTRβ thesem_open()operation was interrupted by a signal. rude. πEINVALβ thesem_open()operation is not supported for the given name, orO_CREATwas specified in oflag and value was greater than{SEM_VALUE_MAX}.EMFILEβ too many semaphore descriptors or file descriptors are currently in use by this process.ENFILEβ too many semaphores are currently open in the system.ENOENTβO_CREATis not set and the named semaphore does not exist.ENOMEMβ there is insufficient memory for the creation of the new named semaphore.ENOSPCβ there is insufficient space on a storage device for the creation of the new named semaphore.
if any of the following conditions occur, the sem_open() function may return SEM_FAILED and set errno to the corresponding value:
ENAMETOOLONGβ the length of the name argument exceeds{_POSIX_PATH_MAX}on systems that do not support the XSI option or exceeds{_XOPEN_PATH_MAX}on XSI systems, or has a pathname component that is longer than{_POSIX_NAME_MAX}on systems that do not support the XSI option or longer than{_XOPEN_NAME_MAX}on XSI systems.
the following sections are informative. π«Ά
π EXAMPLES π
None.
(not me being sad about it. it literally says None. π)
π§ APPLICATION USAGE π§
None.
(also literally None. the vibes are minimal but the facts are the facts.)
π RATIONALE π
early drafts required an error return value of -1 with the type sem_t * for the sem_open() function, which is not guaranteed to be portable across implementations. the revised text provides the symbolic error code SEM_FAILED to eliminate the type conflict. glow-up arc complete. β¨
β FUTURE DIRECTIONS β
a future version might require the sem_open() and sem_unlink() functions to have semantics similar to normal file system operations. π foreshadowing.
π₯ SEE ALSO π₯
semctl(), semget(), semop(), sem_close(), sem_post(), sem_timedwait(), sem_trywait(), sem_unlink()
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. 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 .
and that's the whole page bestie β brought to you by IEEE/The Open Group, 2017, and it's SEM_OPEN(3P) so you know exactly which one we're talking about. 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