SEM_OPEN(3C) | Standard C Library Functions | SEM_OPEN(3C) |
sem_open - initialize/open a named semaphore
#include <semaphore.h> sem_t *sem_open(const char *name, int oflag,
/* unsigned long mode, unsigned int value */ ...);
The sem_open() function establishes a connection between a named semaphore and a process (or LWP or thread). Following a call to sem_open() with semaphore name name, the process may reference the semaphore associated with name using the address returned from the call. This semaphore may be used in subsequent calls to sem_wait(3C), sem_trywait(3C), sem_post(3C), and sem_close(3C). The semaphore remains usable by this process until the semaphore is closed by a successful call to sem_close(3C), _Exit(2), or one of the exec functions.
The oflag argument controls whether the semaphore is created or merely accessed by the call to sem_open(). The following flag bits may be set in oflag:
O_CREAT
The user ID of the semaphore is set to the effective user ID of the process; the group ID of the semaphore is set to a system default group ID or to the effective group ID of the process. The permission bits of the semaphore are set to the value of the mode argument except those set in the file mode creation mask of the process (see umask(2)). When bits in mode other than the file permission bits are specified, the effect is unspecified.
After the semaphore named name has been created by sem_open() with the O_CREAT flag, other processes can connect to the semaphore by calling sem_open() with the same value of name.
O_EXCL
If flags other than O_CREAT and O_EXCL are specified in the oflag parameter, the effect is unspecified.
The name argument points to a string naming a semaphore object. It is unspecified whether the name appears in the file system and is visible to functions that take pathnames as arguments. The name argument conforms to the construction rules for a pathname. The first character of name must be a slash (/) character and the remaining characters of name cannot include any slash characters. For maximum portability, name should include no more than 14 characters, but this limit is not enforced.
If a process makes multiple successful calls to sem_open() with the same value for name, the same semaphore address is returned for each such successful call, provided that there have been no calls to sem_unlink(3C) for this semaphore.
References to copies of the semaphore produce undefined results.
The sem_init(3C) function is used with unnamed semaphores.
Upon successful completion, the function returns the address of the semaphore. Otherwise, it will return a value of SEM_FAILED and set errno to indicate the error. The symbol SEM_FAILED is defined in the header <semaphore.h>. No successful return from sem_open() will return the value SEM_FAILED.
If any of the following conditions occur, the sem_open() function will return SEM_FAILED and set errno to the corresponding value:
EACCES
EEXIST
EINTR
EINVAL
EMFILE
ENAMETOOLONG
ENFILE
ENOENT
ENOSPC
ENOSYS
See attributes(7) for descriptions of the following attributes:
ATTRIBUTE TYPE | ATTRIBUTE VALUE |
Interface Stability | Committed |
MT-Level | MT-Safe |
Standard | See standards(7). |
exec(2), exit(2), umask(2), sem_close(3C), sem_init(3C), sem_post(3C), sem_unlink(3C), sem_wait(3C), sysconf(3C), attributes(7), standards(7)
July 9, 2009 | OmniOS |