table of contents
other versions
- wheezy 3.44-1
- jessie 3.74-1
- jessie-backports 4.10-2~bpo8+1
- testing 4.10-2
- unstable 4.10-2
SEMOP(2) | Linux Programmer's Manual | SEMOP(2) |
NAME¶
semop, semtimedop - semaphore operationsSYNOPSIS¶
#include <sys/types.h> #include <sys/ipc.h> #include <sys/sem.h>int semop(int semid, struct sembuf *sops, unsigned nsops);int semtimedop(int semid, struct sembuf *sops, unsigned nsops, struct timespec *timeout);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
DESCRIPTION¶
Each semaphore in a semaphore set has the following associated values:unsigned short semval; /* semaphore value */ unsigned short semzcnt; /* # waiting for zero */ unsigned short semncnt; /* # waiting for increase */ pid_t sempid; /* ID of process that did last op */semop() performs operations on selected semaphores in the set indicated by semid. Each of the nsops elements in the array pointed to by sops specifies an operation to be performed on a single semaphore. The elements of this structure are of type struct sembuf, containing the following members:
unsigned short sem_num; /* semaphore number */ short sem_op; /* semaphore operation */ short sem_flg; /* operation flags */Flags recognized in sem_flg are IPC_NOWAIT and SEM_UNDO. If an operation specifies SEM_UNDO, it will be automatically undone when the process terminates. The set of operations contained in sops is performed in array order, and atomically, that is, the operations are performed either as a complete unit, or not at all. The behavior of the system call if not all operations can be performed immediately depends on the presence of the IPC_NOWAIT flag in the individual sem_flg fields, as noted below.
- •
- semval becomes 0, at which time the value of semzcnt is decremented.
- •
- The semaphore set is removed: semop() fails, with errno set to EIDRM.
- •
- The calling thread catches a signal: the value of semzcnt is decremented and semop() fails, with errno set to EINTR.
- •
- The time limit specified by timeout in a semtimedop() call expires: semop() fails, with errno set to EAGAIN.
- •
- semval becomes greater than or equal to the absolute value of sem_op, at which time the value of semncnt is decremented, the absolute value of sem_op is subtracted from semval and, if SEM_UNDO is specified for this operation, the system updates the undo count (semadj) for this semaphore.
- •
- The semaphore set is removed from the system: semop() fails, with errno set to EIDRM.
- •
- The calling thread catches a signal: the value of semncnt is decremented and semop() fails, with errno set to EINTR.
- •
- The time limit specified by timeout in a semtimedop() call expires: the system call fails, with errno set to EAGAIN.
RETURN VALUE¶
If successful semop() and semtimedop() return 0; otherwise they return -1 with errno indicating the error.ERRORS¶
On failure, errno is set to one of the following:- E2BIG
- The argument nsops is greater than SEMOPM, the maximum number of operations allowed per system call.
- EACCES
- The calling process does not have the permissions required to perform the specified semaphore operations, and does not have the CAP_IPC_OWNER capability.
- EAGAIN
- An operation could not proceed immediately and either IPC_NOWAIT was specified in sem_flg or the time limit specified in timeout expired.
- EFAULT
- An address specified in either the sops or the timeout argument isn't accessible.
- EFBIG
- For some operation the value of sem_num is less than 0 or greater than or equal to the number of semaphores in the set.
- EIDRM
- The semaphore set was removed.
- EINTR
- While blocked in this system call, the thread caught a signal; see signal(7).
- EINVAL
- The semaphore set doesn't exist, or semid is less than zero, or nsops has a nonpositive value.
- ENOMEM
- The sem_flg of some operation specified SEM_UNDO and the system does not have enough memory to allocate the undo structure.
- ERANGE
- For some operation sem_op+semval is greater than SEMVMX, the implementation dependent maximum value for semval.
VERSIONS¶
semtimedop() first appeared in Linux 2.5.52, and was subsequently backported into kernel 2.4.22. Glibc support for semtimedop() first appeared in version 2.3.3.CONFORMING TO¶
SVr4, POSIX.1-2001.NOTES¶
The inclusion of <sys/types.h> and <sys/ipc.h> isn't required on Linux or by any version of POSIX. However, some old implementations required the inclusion of these header files, and the SVID also documented their inclusion. Applications intended to be portable to such old systems may need to include these header files.- SEMOPM
- Maximum number of operations allowed for one semop() call (32) (on Linux, this limit can be read and modified via the third field of /proc/sys/kernel/sem).
- SEMVMX
- Maximum allowable value for semval: implementation dependent (32767).
BUGS¶
When a process terminates, its set of associated semadj structures is used to undo the effect of all of the semaphore operations it performed with the SEM_UNDO flag. This raises a difficulty: if one (or more) of these semaphore adjustments would result in an attempt to decrease a semaphore's value below zero, what should an implementation do? One possible approach would be to block until all the semaphore adjustments could be performed. This is however undesirable since it could force process termination to block for arbitrarily long periods. Another possibility is that such semaphore adjustments could be ignored altogether (somewhat analogously to failing when IPC_NOWAIT is specified for a semaphore operation). Linux adopts a third approach: decreasing the semaphore value as far as possible (i.e., to zero) and allowing process termination to proceed immediately.EXAMPLE¶
The following code segment uses semop() to atomically wait for the value of semaphore 0 to become zero, and then increment the semaphore value by one.struct sembuf sops[2]; int semid; /* Code to set semid omitted */ sops[0].sem_num = 0; /* Operate on semaphore 0 */ sops[0].sem_op = 0; /* Wait for value to equal 0 */ sops[0].sem_flg = 0; sops[1].sem_num = 0; /* Operate on semaphore 0 */ sops[1].sem_op = 1; /* Increment value by one */ sops[1].sem_flg = 0; if (semop(semid, sops, 2) == -1) { perror("semop"); exit(EXIT_FAILURE); }
SEE ALSO¶
clone(2), semctl(2), semget(2), sigaction(2), capabilities(7), sem_overview(7), svipc(7), time(7)COLOPHON¶
This page is part of release 3.44 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/.2012-08-27 | Linux |