|HP-UX Reference > S
sem_open(2)HP-UX 11i Version 3: February 2007
sem_open — create/open a named POSIX semaphore
sem_t * sem_open(const char *name, int oflag, mode_t mode, unsigned int value);
sem_open() is used to open or create a named semaphore. A successful call to sem_open() will create a descriptor for the semaphore specified by name. The pointer to the semaphore returned by sem_open() can be used to access the semaphore associated with name in subsequent operations. The name argument points to a string referring to a semaphore. It should begin with a "/" and shall conform to pathname rules except that no path component should be "." or "..".
The oflag argument specifies whether a semaphore is to be created or not. The following bits in it may be set:
The mode and value arguments are provided to supply the permissions and the initial value information necessary for creating a new semaphore.
To use this function, link in the realtime library by specifying -lrt on the compiler or linker command line.
The following call to sem_open() will create a new named semaphore if one does not exist, which depends on the flags specified in oflag, has the permissions specified in mode and has an initial value of value.
sem_open(name, oflag, mode, value);
If the semaphore was created and initialized, sem_open() returns a pointer to a sem_t structure containing the index of the new descriptor.
If the semaphore could not be created/initialized, the call returns -1 and sets errno to indicate the error. If the named semaphore is already opened by the calling process, a descriptor and a sem_t structure for the named semaphore already exists for the calling process. A new descriptor is not created and a pointer to the existing sem_t structure is returned for this call.
sem_open() fails and does not perform the requested operation if any of the following conditions are encountered:
sem_close(2), sem_post(2), sem_wait(2), sem_unlink(2), privileges(5), <semaphore.h>.