Initializes and opens a named semaphore.
Standard C Library (libc.a)
#include <semaphore.h>
sem_t * sem_open (const char *name, int oflag, mode_t mode, unsigned value)
The sem_open subroutine establishes a connection between a named semaphore and a process. Following a call to the sem_open subroutine with semaphore name name, the process may reference the semaphore using the address returned from the call. This semaphore may be used in subsequent calls to the sem_wait, sem_trywait, sem_post, and sem_close subroutines. The semaphore remains usable by this process until the semaphore is closed by a successful call to sem_close, _exit, or one of the exec subroutines.
The name parameter points to a string naming a semaphore object. The name has no representation in the file system. The name parameter conforms to the construction rules for a pathname. It might begin with a slash character, and it must contain at least one character. Processes calling sem_open() with the same value of name refers to the same semaphore object, as long as that name has not been removed.
If a process makes multiple successful calls to the sem_open subroutine with the same value of the name parameter, the same semaphore address is returned for each such successful call, provided that there have been no calls to the sem_unlink subroutine for this semaphore.
Item | Description |
---|---|
name | Points to a string naming a semaphore object. |
oflag | Controls whether the semaphore is created or merely accessed
by the call to the sem_open subroutine. The following flag
bits may be set in the oflag parameter:
|
mode | Specifies the value of the file permission bits. Used with O_CREAT to create a message queue. |
value | Specifies the initial value. Used with O_CREAT to create a message queue. |
Upon successful completion, the sem_open subroutine returns the address of the semaphore. Otherwise, it returns a value of SEM_FAILED and sets errno to indicate the error. The SEM_FAILED symbol is defined in the semaphore.h header file. No successful return from the sem_open subroutine returns the value SEM_FAILED.
Item | Description |
---|---|
EACCES | The named semaphore exists and the permissions specified by oflag are denied. |
EEXIST | The O_CREAT and O_EXCL flags are set and the named semaphore already exists. |
EFAULT | Invalid user address. |
EINVAL | The sem_open subroutine is not supported for the given name, or the O_CREAT flag was specified in the oflag parameter and value was greater than SEM_VALUE_MAX. |
EMFILE | Too many semaphore descriptors are currently in use by this process. |
ENAMETOOLONG | The length of the name parameter exceeds PATH_MAX, or a pathname component is longer than NAME_MAX. |
ENFILE | Too many semaphores are currently open in the system. |
ENOENT | The O_CREAT flag is not set and the named semaphore does not exist. |
ENOMEM | Insufficient memory for the required operation. |
ENOTSUP | This function is not supported with processes that have been checkpoint-restart'ed. |
ENOSPC | There is insufficient space for the creation of the new named semaphore. |