semop(2) System Calls Manual semop(2) semop, semtimedop - System V C (libc, -lc) #include int semop(int semid, struct sembuf *sops, size_t nsops); int semtimedop(int semid, struct sembuf *sops, size_t nsops, const struct timespec *_Nullable timeout); glibc (. feature_test_macros(7)): semtimedop(): _GNU_SOURCE System V : unsigned short semval; /* semaphore value */ unsigned short semzcnt; /* # waiting for zero */ unsigned short semncnt; /* # waiting for increase */ pid_t sempid; /* PID of process that last modified the semaphore value */ semop() semid. nsops , sops , , . struct sembuf, : unsigned short sem_num; /* */ short sem_op; /* */ short sem_flg; /* */ sem_flg IPC_NOWAIT SEM_UNDO. SEM_UNDO, . sops , , . IPC_NOWAIT sem_flg , . sem_num- , 0. , sem_op. If sem_op is a positive integer, the operation adds this value to the semaphore value (semval). Furthermore, if SEM_UNDO is specified for this operation, the system subtracts the value sem_op from the semaphore adjustment (semadj) value for this semaphore. This operation can always proceed--it never forces a thread to wait. The calling process must have alter permission on the semaphore set. sem_op , . << >>: semval , . , sem_flg IPC_NOWAIT, semop() errno EAGAIN ( sops ). semzcnt ( , ) , : o semval 0, semzcnt . o : semop() , errno EIDRM. o : semncnt semop() , errno EINTR. sem_op , . semval sem_op, : sem_op semval, , SEM_UNDO, sem_op (semadj) . sem_op semval, sem_flg IPC_NOWAIT, semop() , errno EAGAIN ( sops ). semncnt ( , ) , : o semval sem_op: . o : semop() , errno EIDRM. o : semncnt semop() , errno EINTR. sempid , , sops, . sem_otime . semtimedop() semtimedop() semop(), , , , , timespec, timeout. , - . , semtimedop() , errno EAGAIN ( sops ). timeout NULL, semtimedop() semop(). , semtimedop() , EINTR, timeout . On success, semop() and semtimedop() return 0. On failure, they return -1, and set errno to indicate the error. E2BIG nsops SEMOPM, , . EACCES , , CAP_IPC_OWNER, IPC. EAGAIN , IPC_NOWAIT sem_flg, , timeout. EFAULT , sops timeout, . EFBIG sem_num . EIDRM . EINTR , , ; signal(7). EINVAL , semid , nsops . ENOMEM sem_flg SEM_UNDO, . ERANGE sem_op+semval SEMVMX, semval ( ). POSIX.1-2008. Linux 2.5.52 (backported into Linux 2.4.22), glibc 2.3.3. POSIX.1-2001, SVr4. sem_undo , fork(2), execve(2). semop() , SA_RESTART . A semaphore adjustment (semadj) value is a per-process, per-semaphore integer that is the negated sum of all operations performed on a semaphore specifying the SEM_UNDO flag. Each process has a list of semadj values--one value for each semaphore on which it has operated using SEM_UNDO. When a process terminates, each of its per-semaphore semadj values is added to the corresponding semaphore, thus undoing the effect of that process's operations on the semaphore (but see BUGS below). When a semaphore's value is directly set using the SETVAL or SETALL request to semctl(2), the corresponding semadj values in all processes are cleared. The clone(2) CLONE_SYSVSEM flag allows more than one process to share a semadj list; see clone(2) for details. semval, sempid, semzcnt semnct semctl(2). , semop(): SEMOPM , semop(). Linux 3.19, 3. Linux 3.19, 500. Linux /proc/sys/kernel/sem. : 1000, , semop(2) - sops. SEMVMX semval: (32767). (SEMAEM), , (SEMMNU) . semadj , SEM_UNDO. : ( ) , ? , . , . ( , , IPC_NOWAIT). Linux : , ( .. ) . In Linux 2.6.x, x <= 10, there is a bug that in some circumstances prevents a thread that is waiting for a semaphore value to become zero from being woken up when the value does actually become zero. This bug is fixed in Linux 2.6.11. semop() , 0 . 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); } A further example of the use of semop() can be found in shmop(2). clone(2), semctl(2), semget(2), sigaction(2), capabilities(7), sem_overview(7), sysvipc(7), time(7) () Alexander Golubev , Azamat Hackimov , Hotellook, Nikita , Spiros Georgaras , Vladislav , Yuri Kozlov , Kirill Rekhov ; GNU (GNU General Public License - GPL, 3 ) , - . - , , () () () <>. Linux 6.15 17 2025 . semop(2)