semaphore

Details

define SEM_FILE_HDR_SIGNATURE

define SEM_FILE_HDR_NOT_SIGNATURE

int sem_init(

sem_t * sem,
int pshared,
unsigned int value
)

Initializes sem as an unnamed semaphore with pshared and 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

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

int sem_destroy(

sem_t * sem
)

This function destroys sem–an unnamed semaphore.

Returns

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

  • EINVAL: sem is NULL

int sem_getvalue(

sem_t * sem,
int * sval
)

This 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_t * sem_open(

const char * name,
int oflag,

)

This function opens or creates a named semaphore.

Parameters

  • name The name of the message queue

  • oflag The flags to use when opening (O_CREAT, O_EXCL, O_RDWR)

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

int sem_close(

sem_t * sem
)

This 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

int sem_post(

sem_t * sem
)

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


int sem_timedwait(

sem_t * sem,
const struct timespec * abs_timeout
)

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.

Parameters

  • sem A pointer to the semaphore

  • abs_timeout Absolute timeout value

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

int sem_trywait(

sem_t * sem
)

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

const char * name
)

This 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

int sem_wait(

sem_t * sem
)

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.

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

X

Thanks for Coming!

Subscribe to news and updates