## NAME

sem_open - initialize/open a named semaphore

## SYNOPSIS

#include <semaphore.h>

sem_t *sem_open(const char *name , int oflag ,
  /* unsigned long mode , unsigned int value */ ... );
typedef struct {
  ...
} sem_t; /*opaque POSIX semaphore*/

## DESCRIPTION

sem_open() establishes a connection to a semaphore, name, returning the address of the semaphore to the calling thread for subsequent calls to sem_wait(), sem_trywait(), sem_post(), and sem_close(). The semaphore remains usable until the semaphore is closed.

name points to a string naming a semaphore object. The name argument should conform to the construction rules for a pathname. If a thread makes multiple successful calls to sem_open() with the same value for name, the same semaphore address will be returned for each such successful call, provided that there have been no calls to sem_unlink() for this semaphore.

oflag determines whether the semaphore is created or merely accessed by the call to sem_open(). The three valid values for oflag are 0, O_CREAT, or the bitwise inclusive OR of O_CREAT and O_EXCL. Setting the oflag bits to O_CREAT will create the semaphore if it does not already exist. Setting both O_CREAT and O_EXCL will fail if the semaphore already exists. The check for the existence of the semaphore and the creation of the semaphore if it does not exist is atomic with respect to other threads executing sem_open(). After the semaphore named name has been created by sem_open() with the O_CREAT flag, other threads can connect to this semaphore by calling sem_open() with the same value of name, oflag set to zero.

Using the O_CREAT flag requires a third and a fourth argument: mode which is not used and can be set to 0 and value. The semaphore is created with an initial count of value. value must be less than or equal to {SEM_VALUE_MAX}.

## RETURN VALUES

If successful, sem_open() returns the address of the semaphore, otherwise it returns SEM_FAILED and sets errno to indicate the error condition.

## ERRORS

EACCES
  The named semaphore exists and the O_RDWR permissions 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.
ENOENT
  O_CREAT is not set and the named semaphore does not exist.
ENOSPC
  There is insufficient space for the creation of the new named semaphore.
ENOSYS
  sem_open() is not supported by this implementation.

## SEE ALSO

sem_close(), sem_post(), sem_unlink(), sem_wait()

#### Home page (Kernel)

## < Copyright Rowebots Research Inc. and Multiprocessor Toolsmiths Inc. 1987-2018 >