Skip to main content

Semaphore

Functions#

Name
intsem_init(sem_t * sem, int pshared, unsigned int value)
intsem_destroy(sem_t * sem)
intsem_getvalue(sem_t sem, int sval)
sem_t *sem_open(const char * name, int oflag, ... )
intsem_close(sem_t * sem)
intsem_post(sem_t * sem)
intsem_timedwait(sem_t sem, const struct timespec abs_timeout)
intsem_trywait(sem_t * sem)
intsem_unlink(const char * name)
intsem_wait(sem_t * sem)

Defines#

Name
SEM_FILE_HDR_SIGNATURE
SEM_FILE_HDR_NOT_SIGNATURE

Functions Documentation#

sem_init#

int sem_init(    sem_t * sem,    int pshared,    unsigned int value)

Parameters:

  • sem A pointer to the semaphore to initialize
  • pshared Non-zero to share the semaphore between processes
  • value The initial value of the semaphore

Return: Zero on success or -1 with errno (see Errno) set to:

  • EINVAL: sem is NULL

Initializes sem as an unnamed semaphore with pshared and value.

The semaphore value will be decremented on sem_wait() and incremented on sem_post().

sem_destroy#

int sem_destroy(    sem_t * sem)

Return: Zero on success or -1 with errno (see Errno) set to:

  • EINVAL: sem is NULL

This function destroys sem–an unnamed semaphore.

sem_getvalue#

int sem_getvalue(    sem_t * sem,    int * sval)

Return: Zero on success or -1 with errno (see Errno) set to:

  • EINVAL: sem is NULL

This function gets the value of the semaphore. If the semaphore is locked, the value is zero.

sem_open#

sem_t * sem_open(    const char * name,    int oflag,    ... )

Parameters:

  • name The name of the message queue
  • oflag The flags to use when opening (O_CREAT, O_EXCL, O_RDWR)

Return: Zero on success or SEM_FAILED with errno (see Errno) set to:

  • ENAMETOOLONG: name length is greater than NAME_MAX
  • EEXIST: OCREAT and O_EXCL are set in _oflag but the semaphore already exists
  • ENOENT: OCREAT is not set in _oflag and the semaphore does not exist
  • ENOMEM: not enough memory for the semaphore

This 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){

sem_close#

int sem_close(    sem_t * sem)

Return: Zero on success or SEM_FAILED with errno (see Errno) set to:

  • EINVAL: sem is NULL

This function closes a semaphore. The semaphore must be deleted using sem_unlink() in order to free the system resources associated with the semaphore.

sem_post#

int sem_post(    sem_t * sem)

Return: Zero on success or SEM_FAILED with errno (see Errno) set to:

  • EINVAL: sem is NULL
  • EACCES: process cannot access semaphore

Unlocks (increments) the value of the 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#

int sem_timedwait(    sem_t * sem,    const struct timespec * abs_timeout)

Parameters:

  • sem A pointer to the semaphore
  • abs_timeout Absolute timeout value

Return: 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

Locks (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.

sem_trywait#

int sem_trywait(    sem_t * sem)

Return: 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.

This function locks (decrements) the semaphore if it can be locked immediately.

sem_unlink#

int sem_unlink(    const char * name)

Return: 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

This function unlinks a named semaphore. It also releases any system resources associated with the semaphore.

sem_wait#

int sem_wait(    sem_t * sem)

Return: 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

This 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().

Macros Documentation#

SEM_FILE_HDR_SIGNATURE#

#define SEM_FILE_HDR_SIGNATURE 

SEM_FILE_HDR_NOT_SIGNATURE#

#define SEM_FILE_HDR_NOT_SIGNATURE 

Updated on 18 September 2021 at 21:44:10 MDT