.\" $NetBSD: semop.2,v 1.18 2024/10/03 16:58:17 christos Exp $ .\" .\" Copyright (c) 1995 Frank van der Linden .\" All rights reserved. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" 3. All advertising materials mentioning features or use of this software .\" must display the following acknowledgement: .\" This product includes software developed for the NetBSD Project .\" by Frank van der Linden .\" 4. The name of the author may not be used to endorse or promote products .\" derived from this software without specific prior written permission .\" .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR .\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES .\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. .\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, .\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT .\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, .\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY .\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF .\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. .\" .Dd October 3, 2024 .Dt SEMOP 2 .Os .Sh NAME .Nm semop, semtimedop .Nd semaphore operations .Sh LIBRARY .Lb libc .Sh SYNOPSIS .In sys/sem.h .Ft int .Fn semop "int semid" "struct sembuf *sops" "size_t nsops" .Ft int .Fn semtimedop "int semid" "struct sembuf *sops" "size_t nsops" "struct timespec *timeout" .Sh DESCRIPTION .Fn semop provides a number of atomic operations on a set of semaphores. The semaphore set is specified by .Fa semid , .Fa sops is an array of semaphore operations, and .Fa nsops is the number of operations in this array. The .Va sembuf structures in the array contain the following members: .Bd -literal unsigned short sem_num; /* semaphore # */ short sem_op; /* semaphore operation */ short sem_flg; /* operation flags */ .Ed .Pp 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: .Bl -bullet .It .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. .It .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. .It .Va sem_op is equal to 0. The calling process is blocked until the value of the specified semaphore reaches 0. .El .Pp The behaviour of each operation is influenced by the flags set in .Va sem_flg in the following way: .Bl -tag -width IPC_NOWAITX .It 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 . .It 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 .Pp .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: .Bl -tag -width Er .It Bq Er EINVAL There is no semaphore associated with .Fa semid . .It Bq Er EIDRM The semaphore set was removed while the process was waiting for one of its semaphores to reach a certain value. .It Bq Er EACCES The calling process has no permission to access the specified semaphore set. .It Bq Er E2BIG The value of .Fa nsops is too big. The maximum is defined as .Dv MAX_SOPS in .In sys/sem.h . .It 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 . .It Bq Er ENOSPC .Dv SEM_UNDO was requested, and there is not enough space left in the kernel to store the undo information. .It Bq Er EAGAIN The requested operation can not immediately be performed, and .Dv IPC_NOWAIT was set in .Va sem_flg . .It Bq Er EFAULT .Fa sops points to an illegal address. .It Bq Er EINTR While blocked in this system call, the thread caught a signal. .El .Pp In addition, .Fn semtimedop will fail if: .Bl -tag -width Er .It 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. .It 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: .Bd -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 .