sem_open - initialize/open a named semaphore
#include <semaphore.h>
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}.
If successful, sem_open() returns the address of the semaphore, otherwise it returns SEM_FAILED and sets errno to indicate the error condition.
sem_close(), sem_post(), sem_unlink(), sem_wait()