Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
HP-UX Reference > S


HP-UX 11i Version 3: February 2007

Technical documentation

» Feedback
Content starts here

 » Table of Contents

 » Index


sem_open — create/open a named POSIX semaphore


#include <sys/semaphore.h>

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:


If this flag is set, a new semaphore is created if it does not already exist. If this flag is not set, the semaphore should already exist.


If this flag is set, the call fails if the semaphore already exists. This flag is valid only when O_CREAT is also set; otherwise, it is ignored.

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:


The named semaphore exists and the process does not have the permissions to open the semaphore as described by oflag, or the named semaphore does not exist and the process does not have the permission to open it.


The flags O_CREAT and O_EXCL are set in oflag and the named semaphore exists.


A signal interrupted the sem_open() operation.


The argument value is greater than {_POSIX_SEM_VALUE_MAX} and the O_CREAT flag was specified in oflag.


The name argument does not begin with "/" or contains "." or ".." as a pathname component.


Too many semaphore descriptors are currently in use by this process.


The name string is longer than {PATH_MAX}.


There are too many semaphores in the system.


The flag O_CREAT is not set in oflag and the named semaphore does not exist.


There are insufficient resources for the creation of a new named semaphore.


sem_open(): POSIX

Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© 1983-2007 Hewlett-Packard Development Company, L.P.