p Each operation (specified in .Va sem_op ) is applied to semaphore number .Va sem_num in the set of semaphores specified by .Fa semid . The value of .Va sem_op determines the action taken in the following way: l -bullet t .Va sem_op is less than 0. The current process is blocked until the value of the semaphore is greater than or equal to the absolute value of .Va sem_op . The absolute value of .Va sem_op is then subtracted from the value of the semaphore, and the calling process continues. Negative values of .Va sem_op are thus used to enter critical regions. t .Va sem_op is greater than 0. Its value is added to the value of the specified semaphore. This is used to leave critical regions. t .Va sem_op is equal to 0. The calling process is blocked until the value of the specified semaphore reaches 0. .El
p The behaviour of each operation is influenced by the flags set in .Va sem_flg in the following way: l -tag -width IPC_NOWAITX t Dv IPC_NOWAIT In the case where the calling process would normally block, waiting for a semaphore to reach a certain value, .Dv IPC_NOWAIT makes the call return immediately, returning a value of -1 and setting .Va errno to .Er EAGAIN . t SEM_UNDO Keep track of the changes that this call makes to the value of a semaphore, so that they can be undone when the calling process terminates. This is useful to prevent other processes waiting on a semaphore to block forever, should the process that has the semaphore locked terminate in a critical section. .El
p .Fn semtimedop is similar to .Fn semop , but it also allows specifying a timeout. When the semaphore is not available, the thread typically sleeps until the semaphore is available. .Fn semtimedop allows specifying a maximum amount of time in .Fa timeout argument that a thread should sleep while waiting for the semaphore to be available. If the specified time limit has been reached, .Fn semtimedop fails with .Er EAGAIN (and none of the operations in .Fa sops are performed). If .Fa timeout is .Dv NULL , .Fn semtimedop behaves exactly like .Fn semop . .Sh RETURN VALUES Upon successful completion both .Fn semop and .Fn semtimedop return a value of 0. Otherwise, -1 is returned and the global variable .Va errno is set to indicate the error. .Sh ERRORS .Fn semop will fail if: l -tag -width Er t Bq Er EINVAL There is no semaphore associated with .Fa semid . t Bq Er EIDRM The semaphore set was removed while the process was waiting for one of its semaphores to reach a certain value. t Bq Er EACCES The calling process has no permission to access the specified semaphore set. t Bq Er E2BIG The value of .Fa nsops is too big. The maximum is defined as .Dv MAX_SOPS in n sys/sem.h . t Bq Er EFBIG .Va sem_num in one of the sem_buf structures is less than 0, or greater than the actual number of semaphores in the set specified by .Fa semid . t Bq Er ENOSPC .Dv SEM_UNDO was requested, and there is not enough space left in the kernel to store the undo information. t Bq Er EAGAIN The requested operation can not immediately be performed, and .Dv IPC_NOWAIT was set in .Va sem_flg . t Bq Er EFAULT .Fa sops points to an illegal address. t Bq Er EINTR While blocked in this system call, the thread caught a signal. .El
p In addition, .Fn semtimedop will fail if: l -tag -width Er t Bq Er EAGAIN An operation could not proceed immediately and either .Dv IPC_NOWAIT was specified in .Va sem_flg or the time limit specified in .Fa timeout expired. t Bq Er EFAULT An address specified in the .Fa timeout argument isn't accessible. .El .Sh EXAMPLES The following example shows how to perform a semaphore operation with a timeout: d -literal -offset indent /* Performs a semaphore operation with a 5 sec timeout*/ struct sembuf sops[1]; /* Semaphore operation structure */ struct timespec timeout; /* Timeout structure */ /* Create semaphore set with 1 semaphore */ int semid = semget(key, 1, 0666 | IPC_CREAT); /* Initialize semaphore to 0 */ if (semctl(semid, 0, SETVAL, 0) == -1) { warn("semctl SETVAL"); exit(EXIT_FAILURE); } sops[0].sem_num = 0; /* Operation on semaphore 0 */ sops[0].sem_op = -1; /* Decrement semaphore by 1 */ sops[0].sem_flg = 0; /* No flags */ timeout.tv_sec = 5; /* 5 seconds */ timeout.tv_nsec = 0; /* 0 nanoseconds */ if (semtimedop(semid, sops, 1, &timeout) == -1) { warn("semtimedop"); /* Print error message */ } .Ed .Sh SEE ALSO .Xr semctl 2 , .Xr semget 2 .Sh STANDARDS The .Nm system call conforms to .St -xsh5 . .Sh HISTORY Semaphores appeared in the first release of .At V .