SEM_OPEN(3) | Library Functions Manual | SEM_OPEN(3)

# NAME

`sem_open`, `sem_close`, `sem_unlink` — open and close named semaphores

# SYNOPSIS

POSIX Threads Library (libpthread, -lpthread)
`#include <semaphore.h>`

sem_t *
`sem_open`(const char *name, int oflag, ...);

int
`sem_close`(sem_t *sem);

int
`sem_unlink`(const char *name);

# DESCRIPTION

The `sem_open`() function opens and returns a named semaphore. The `sem_close`() function closes a previously opened named semaphore without removing it. The `sem_unlink`() function removes the named semaphore from the system without closing it.

# RETURN VALUES

On success, `sem_open`() returns a pointer to a semaphore. `sem_close`() and `sem_unlink`() return 0 on success. `sem_open`() returns SEM_FAILED and sets errno to indicate an error. `sem_close`() and `sem_unlink`() return -1 and set errno to indicate an error.

# ERRORS

It is an error to call `sem_close`() with an unnamed semaphore or to call sem_destroy(3) with a named semaphore.

`sem_open`() may fail if:

[`ENOSPC`]
  Insufficient memory is available.
[`EPERM`]
  An attempt was made to open a shared semaphore owned by another user.

`sem_unlink`() may fail for any of the reasons listed in unlink(2).

# SEE ALSO

sem_destroy(3), sem_getvalue(3), sem_init(3), sem_post(3), sem_wait(3)

# STANDARDS

`sem_open`(), `sem_close`(), and `sem_unlink`() appear in ISO/IEC 9945-1:1996 (“POSIX.1”). This implementation deviates from the standard by permitting less sharing.

June 7, 2025 | Debian