cc [ flag ... ] file ... -lposix4 [ library ... ]
#include <semaphore.h>
sem_t *sem_open(const char *name, int oflag, /* unsigned long mode, unsigned int value */ ... );
typedef struct {
...
} sem_t; /*opaque POSIX.4 semaphore*/
name points to a string naming a semaphore object. The name argument should conform to the construction rules for a pathname. If a process 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(3R) 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 processes executing sem_open(). After the semaphore named name has been created by sem_open() with the O_CREAT flag, other processes can connect to this semaphore by calling sem_open() with the same value of name, and nobits set in oflag.
Using the O_CREAT flag requires a third and a fourth argument: mode and value. The semaphore is created with an initial count of value. value must be less than or equal to {SEM_VALUE_MAX}. The semaphore’s user ID acquires the effective user ID of the process; the semaphore’s group ID is set to a system default group ID or to the effective group ID of the process. The semaphore’s permission bits is set to the value of mode, modified by clearing all bits set in the file creation mask of the process (see umask(2) ).
O_CREAT was set in oflag and value is greater than {SEM_VALUE_MAX}.