Semaphore
Functions
#
sem_init(sem_t *sem, int pshared, unsigned int value)int sem_init (sem_t *sem, int pshared, unsigned int value)
Type: int
Parameters:
sem_t *
sem
int
pshared
unsigned int
value
#
DetailsInitializes sem as an unnamed semaphore with pshared and value. The semaphore value will be decremented on sem_wait() and incremented on sem_post().
Returns:
Zero on success or -1 with errno (see Errno) set to:
- EINVAL: sem is NULL
#
sem_destroy(sem_t *sem)int sem_destroy (sem_t *sem)
Type: int
Parameters:
sem_t *
sem
#
DetailsThis function destroys seman unnamed semaphore.
Returns:
Zero on success or -1 with errno (see Errno) set to:
- EINVAL: sem is NULL
#
sem_getvalue(sem_t sem, int sval)int sem_getvalue (sem_t *sem, int *sval)
Type: int
Parameters:
sem_t *
sem
int *
sval
#
DetailsThis function gets the value of the semaphore. If the semaphore is locked, the value is zero.
Returns:
Zero on success or -1 with errno (see Errno) set to:
- EINVAL: sem is NULL
#
sem_open(const char *name, int oflag,...)sem_t * sem_open (const char *name, int oflag,...)
Type: sem_t *
Parameters:
const char *
name
int
oflag
...
#
DetailsThis function opens or creates a named semaphore. When using O_CREAT, the third argument is the mode and the fourth is the initial value of the semaphore:
sem_t * sem_open(const char * name, int oflag, int mode, int value){
Returns:
Zero on success or SEM_FAILED with errno (see Errno) set to:
- ENAMETOOLONG: name length is greater than NAME_MAX
- EEXIST: O_CREAT and O_EXCL are set in oflag but the semaphore already exists
- ENOENT: O_CREAT is not set in oflag and the semaphore does not exist
- ENOMEM: not enough memory for the semaphore
#
sem_close(sem_t *sem)int sem_close (sem_t *sem)
Type: int
Parameters:
sem_t *
sem
#
DetailsThis function closes a semaphore. The semaphore must be deleted using sem_unlink() in order to free the system resources associated with the semaphore.
Returns:
Zero on success or SEM_FAILED with errno (see Errno) set to:
- EINVAL: sem is NULL
#
sem_post(sem_t *sem)int sem_post (sem_t *sem)
Type: int
Parameters:
sem_t *
sem
#
DetailsUnlocks (increments) the value of the semaphore.
Returns:
Zero on success or SEM_FAILED with errno (see Errno) set to:
- EINVAL: sem is NULL
- EACCES: process cannot access semaphore
If other threads or processes have called sem_wait() or sem_timedwait(), the semaphore will be available and one of the threads will lock the semaphore.
#
sem_timedwait(sem_t sem, const struct timespec abs_timeout)int sem_timedwait (sem_t *sem, const struct timespec *abs_timeout)
Type: int
Parameters:
sem_t *
sem
const struct timespec *
abs_timeout
#
DetailsLocks (decrements) the semaphore. If the semaphore cannot be locked (it is already zero), this function will block until the value of CLOCK_REALTIME exceeds the value of abs_timeout.
Returns:
Zero on success or SEM_FAILED with errno (see Errno) set to:
- EINVAL: sem is NULL
- EACCES: process cannot access semaphore
- ETIMEDOUT: timeout expired without locking the semaphore
#
sem_trywait(sem_t *sem)int sem_trywait (sem_t *sem)
Type: int
Parameters:
sem_t *
sem
#
DetailsThis function locks (decrements) the semaphore if it can be locked immediately.
Returns:
Zero on success or -1 with errno (see Errno) set to:
- EINVAL: sem is NULL
- EACCES: process cannot access semaphore
- EAGAIN: sem could not be immediately locked.
#
sem_unlink(const char *name)int sem_unlink (const char *name)
Type: int
Parameters:
const char *
name
#
DetailsThis function unlinks a named semaphore. It also releases any system resources associated with the semaphore.
Returns:
Zero on success or SEM_FAILED with errno (see Errno) set to:
- ENOENT: the semaphore with name could not be found
- ENAMETOOLONG: the length of name exceeds NAME_MAX
#
sem_wait(sem_t *sem)int sem_wait (sem_t *sem)
Type: int
Parameters:
sem_t *
sem
#
DetailsThis function locks (decrements) the semaphore. If the semaphore is already zero (and therefore cannot be locked), the calling thread blocks until the semaphore becomes available by another thread calling sem_post().
Returns:
Zero on success or SEM_FAILED with errno (see Errno) set to:
- EINVAL: sem is NULL
- EACCES: sem is not pshared and was created in another process