---
name: "sem_open"
section: "3p"
platform: "linux"
package: "man-pages"
version: "6.19-1"
release: "Arch Linux 2026-11-01"
platforms: ["linux"]
url: "https://new.carta.tech/sem_open/3p"
---

# sem\_open(3p)

initialize and open a named semaphore

## PROLOG

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.

## NAME

sem\_open — initialize and open a named semaphore

## SYNOPSIS

```
#include <semaphore.h>
```

sem\_t \*sem\_open(const char \**name*, int *oflag*, ...);

## DESCRIPTION

The *sem\_open*() function shall establish a connection between a named semaphore and a process. 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.

The *oflag* argument controls whether the semaphore is created or merely accessed by the call to *sem\_open*(). 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. 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 type **mode\_t**, and *value*, which is of type **unsigned**. 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*.

- O\_EXCL

  If O\_EXCL and O\_CREAT are 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 executing *sem\_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 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.

References to copies of the semaphore produce undefined results.

## 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.

## 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\_CREAT and O\_EXCL are set and the named semaphore already exists.

- **EINTR**

  The *sem\_open*() operation was interrupted by a signal.

- **EINVAL**

  The *sem\_open*() operation is not supported for the given name, or O\_CREAT was 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\_CREAT is 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.

## APPLICATION USAGE

None.

## 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.

## FUTURE DIRECTIONS

A future version might require the *sem\_open*() and *sem\_unlink*() functions to have semantics similar to normal file system operations.

## 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 .
