PROLOG |NAME |SYNOPSIS |DESCRIPTION |RETURN VALUE |ERRORS |EXAMPLES |APPLICATION USAGE |RATIONALE |FUTURE DIRECTIONS |SEE ALSO |COPYRIGHT | |
SEM_OPEN(3P) POSIX Programmer's ManualSEM_OPEN(3P)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.
sem_open — initialize and open a named semaphore
#include <semaphore.h> sem_t *sem_open(const char *name, intoflag, ...);
Thesem_open() function shall establish a connection between a named semaphore and a process. Following a call tosem_open() with semaphore namename, the process may reference the semaphore associated withname using the address returned from the call. This semaphore may be used in subsequent calls tosem_wait(),sem_timedwait(),sem_trywait(),sem_post(), andsem_close(). The semaphore remains usable by this process until the semaphore is closed by a successful call tosem_close(),_exit(), or one of theexec functions. Theoflag argument controls whether the semaphore is created or merely accessed by the call tosem_open(). The following flag bits may be set inoflag: O_CREAT This flag is used to create a semaphore if it does not already exist. If O_CREAT is set and the semaphore already exists, then O_CREAT has no effect, except as noted under O_EXCL. Otherwise,sem_open() creates a named semaphore. The O_CREAT flag requires a third and a fourth argument:mode, which is of typemode_t, andvalue, which is of typeunsigned. The semaphore is created with an initial value ofvalue. 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 thename 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 themode argument except those set in the file mode creation mask of the process. When bits inmode other than the file permission bits are specified, the effect is unspecified. After the semaphore namedname has been created bysem_open() with the O_CREAT flag, other processes can connect to the semaphore by callingsem_open() with the same value ofname. O_EXCL If O_EXCL and O_CREAT are set,sem_open() fails if the semaphorename 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() with O_EXCL and O_CREAT set. If O_EXCL is set and O_CREAT is not set, the effect is undefined. If flags other than O_CREAT and O_EXCL are specified in theoflag parameter, the effect is unspecified. Thename 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. Thename argument conforms to the construction rules for a pathname, except that the interpretation of <slash> characters other than the leading <slash> character inname is implementation-defined, and that the length limits for thename argument are implementation- defined and need not be the same as the pathname limits {PATH_MAX} and {NAME_MAX}. Ifname begins with the <slash> character, then processes callingsem_open() with the same value ofname shall refer to the same semaphore object, as long as that name has not been removed. Ifname does not begin with the <slash> character, the effect is implementation-defined. If a process makes multiple successful calls tosem_open() with the same value forname, the same semaphore address shall be returned for each such successful call, provided that there have been no calls tosem_unlink() for this semaphore, and at least one previous successfulsem_open() call for this semaphore has not been matched with asem_close() call. References to copies of the semaphore produce undefined results.Upon successful completion, thesem_open() function shall return the address of the semaphore. Otherwise, it shall return a value of SEM_FAILED and seterrno to indicate the error. The symbol SEM_FAILED is defined in the<semaphore.h> header. No successful return fromsem_open() shall return the value SEM_FAILED.
If any of the following conditions occur, thesem_open() function shall return SEM_FAILED and seterrno to the corresponding value:EACCESThe named semaphore exists and the permissions specified byoflag are denied, or the named semaphore does not exist and permission to create the named semaphore is denied.EEXISTO_CREAT and O_EXCL are set and the named semaphore already exists.EINTRThesem_open() operation was interrupted by a signal.EINVALThesem_open() operation is not supported for the given name, or O_CREAT was specified inoflag andvalue was greater than {SEM_VALUE_MAX}.EMFILEToo many semaphore descriptors or file descriptors are currently in use by this process.ENFILEToo many semaphores are currently open in the system.ENOENTO_CREAT is not set and the named semaphore does not exist.ENOMEMThere is insufficient memory for the creation of the new named semaphore.ENOSPCThere is insufficient space on a storage device for the creation of the new named semaphore. If any of the following conditions occur, thesem_open() function may return SEM_FAILED and seterrno to the corresponding value:ENAMETOOLONG The length of thename 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.None.
None.
Early drafts required an error return value of -1 with the typesem_t *for thesem_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.
A future version might require thesem_open() andsem_unlink() functions to have semantics similar to normal file system operations.
semctl(3p),semget(3p),semop(3p),sem_close(3p),sem_post(3p),sem_timedwait(3p),sem_trywait(3p),sem_unlink(3p) The Base Definitions volume of POSIX.1‐2017,semaphore.h(0p)
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 athttp://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, seehttps://www.kernel.org/doc/man-pages/reporting_bugs.html .IEEE/The Open Group 2017SEM_OPEN(3P)Pages that refer to this page:semaphore.h(0p), sem_close(3p), semctl(3p), sem_destroy(3p), semget(3p), semop(3p), sem_unlink(3p), sigaction(3p), umask(3p)
HTML rendering created 2025-09-06 byMichael Kerrisk, author ofThe Linux Programming Interface. For details of in-depthLinux/UNIX system programming training courses that I teach, lookhere. Hosting byjambit GmbH. | ![]() |