Movatterモバイル変換


[0]ホーム

URL:


man7.org > Linux >man-pages

Linux/UNIX system programming training


semop(2) — Linux manual page

NAME |LIBRARY |SYNOPSIS |DESCRIPTION |RETURN VALUE |ERRORS |STANDARDS |VERSIONS |NOTES |BUGS |EXAMPLES |SEE ALSO |COLOPHON

semop(2)                   System Calls Manualsemop(2)

NAME        top

       semop, semtimedop - System V semaphore operations

LIBRARY        top

       Standard C library (libc,-lc)

SYNOPSIS        top

#include <sys/sem.h>int semop(intsemid, struct sembuf *sops, size_tnsops);int semtimedop(intsemid, struct sembuf *sops, size_tnsops,const struct timespec *_Nullabletimeout);   Feature Test Macro Requirements for glibc (seefeature_test_macros(7)):semtimedop():           _GNU_SOURCE

DESCRIPTION        top

       Each semaphore in a System V 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;   /* PID of process that last                                        modified the semaphore value */semop() performs operations on selected semaphores in the set       indicated bysemid.  Each of thensops elements in the array       pointed to bysops is a structure that specifies an operation to       be performed on a single semaphore.  The elements of this       structure are of typestruct sembuf, containing the following       members:           unsigned short sem_num;  /* semaphore number */           short          sem_op;   /* semaphore operation */           short          sem_flg;  /* operation flags */       Flags recognized insem_flg areIPC_NOWAITandSEM_UNDO.  If an       operation specifiesSEM_UNDO, it will be automatically undone when       the process terminates.       The set of operations contained insops is performed inarrayorder, andatomically, 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 theIPC_NOWAITflag in the individualsem_flg fields, as noted below.       Each operation is performed on thesem_num-th semaphore of the       semaphore set, where the first semaphore of the set is numbered 0.       There are three types of operation, distinguished by the value ofsem_op.       Ifsem_op is a positive integer, the operation adds this value to       the semaphore value (semval).  Furthermore, ifSEM_UNDOis       specified for this operation, the system subtracts the valuesem_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.       Ifsem_op is zero, the process must have read permission on the       semaphore set.  This is a "wait-for-zero" operation: ifsemval is       zero, the operation can immediately proceed.  Otherwise, ifIPC_NOWAITis specified insem_flg,semop() fails witherrno set       toEAGAIN(and none of the operations insops is performed).       Otherwise,semzcnt (the count of threads waiting until this       semaphore's value becomes zero) is incremented by one and the       thread sleeps until one of the following occurs:       •semval becomes 0, at which time the value ofsemzcnt is          decremented.       •  The semaphore set is removed:semop() fails, witherrno set toEIDRM.       •  The calling thread catches a signal: the value ofsemzcnt is          decremented andsemop() fails, witherrno set toEINTR.       Ifsem_op is less than zero, the process must have alter       permission on the semaphore set.  Ifsemval is greater than or       equal to the absolute value ofsem_op, the operation can proceed       immediately: the absolute value ofsem_op is subtracted fromsemval, and, ifSEM_UNDOis specified for this operation, the       system adds the absolute value ofsem_op to the semaphore       adjustment (semadj) value for this semaphore.  If the absolute       value ofsem_op is greater thansemval, andIPC_NOWAITis       specified insem_flg,semop() fails, witherrno set toEAGAIN(and       none of the operations insops is performed).  Otherwise,semncnt       (the counter of threads waiting for this semaphore's value to       increase) is incremented by one and the thread sleeps until one of       the following occurs:       •semval becomes greater than or equal to the absolute value ofsem_op: the operation now proceeds, as described above.       •  The semaphore set is removed from the system:semop() fails,          witherrno set toEIDRM.       •  The calling thread catches a signal: the value ofsemncnt is          decremented andsemop() fails, witherrno set toEINTR.       On successful completion, thesempid value for each semaphore       specified in the array pointed to bysops is set to the caller's       process ID.  In addition, thesem_otime is set to the current       time.semtimedop()semtimedop() behaves identically tosemop() except that in those       cases where the calling thread would sleep, the duration of that       sleep is limited by the amount of elapsed time specified by thetimespec structure whose address is passed in thetimeout       argument.  (This sleep interval will be rounded up to the system       clock granularity, and kernel scheduling delays mean that the       interval may overrun by a small amount.)  If the specified time       limit has been reached,semtimedop() fails witherrno set toEAGAIN(and none of the operations insops is performed).  If thetimeout argument is NULL, thensemtimedop() behaves exactly likesemop().       Note that ifsemtimedop() is interrupted by a signal, causing the       call to fail with the errorEINTR, the contents oftimeout are       left unchanged.

RETURN VALUE        top

       On success,semop() andsemtimedop() return 0.  On failure, they       return -1, and seterrno to indicate the error.

ERRORS        top

E2BIGThe argumentnsops is greater thanSEMOPM, the maximum              number of operations allowed per system call.EACCESThe calling process does not have the permissions required              to perform the specified semaphore operations, and does not              have theCAP_IPC_OWNERcapability in the user namespace              that governs its IPC namespace.EAGAINAn operation could not proceed immediately and eitherIPC_NOWAITwas specified insem_flg or the time limit              specified intimeout expired.EFAULTAn address specified in either thesops or thetimeout              argument isn't accessible.EFBIGFor some operation the value ofsem_num is less than 0 or              greater than or equal to the number of semaphores in the              set.EIDRMThe semaphore set was removed.EINTRWhile blocked in this system call, the thread caught a              signal; seesignal(7).EINVALThe semaphore set doesn't exist, orsemid is less than              zero, ornsops has a nonpositive value.ENOMEMThesem_flg of some operation specifiedSEM_UNDOand the              system does not have enough memory to allocate the undo              structure.ERANGEFor some operationsem_op+semval is greater thanSEMVMX,              the implementation dependent maximum value forsemval.

STANDARDS        top

       POSIX.1-2008.

VERSIONS        top

       Linux 2.5.52 (backported into Linux 2.4.22), glibc 2.3.3.       POSIX.1-2001, SVr4.

NOTES        top

       Thesem_undo structures of a process aren't inherited by the child       produced byfork(2), but they are inherited across anexecve(2)       system call.semop() is never automatically restarted after being interrupted       by a signal handler, regardless of the setting of theSA_RESTART       flag when establishing a signal handler.       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 theSEM_UNDOflag.  Each       process has a list ofsemadj values—one value for each semaphore       on which it has operated usingSEM_UNDO.  When a process       terminates, each of its per-semaphoresemadj 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 theSETVALorSETALL       request tosemctl(2), the correspondingsemadj values in all       processes are cleared.  Theclone(2)CLONE_SYSVSEMflag allows       more than one process to share asemadj list; seeclone(2) for       details.       Thesemval,sempid,semzcnt, andsemnct values for a semaphore can       all be retrieved using appropriatesemctl(2) calls.Semaphore limits       The following limits on semaphore set resources affect thesemop()       call:SEMOPMMaximum number of operations allowed for onesemop() call.              Before Linux 3.19, the default value for this limit was 32.              Since Linux 3.19, the default value is 500.  On Linux, this              limit can be read and modified via the third field of/proc/sys/kernel/sem.Note: this limit should not be              raised above 1000, because of the risk of thatsemop()              fails due to kernel memory fragmentation when allocating              memory to copy thesops array.SEMVMXMaximum allowable value forsemval: implementation              dependent (32767).       The implementation has no intrinsic limits for the adjust on exit       maximum value (SEMAEM), the system wide maximum number of undo       structures (SEMMNU) and the per-process maximum number of undo       entries system parameters.

BUGS        top

       When a process terminates, its set of associatedsemadj structures       is used to undo the effect of all of the semaphore operations it       performed with theSEM_UNDOflag.  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 whenIPC_NOWAITis 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.       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.

EXAMPLES        top

       The following code segment usessemop() 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 setsemid 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 ofsemop() can be found inshmop(2).

SEE ALSO        top

clone(2),semctl(2),semget(2),sigaction(2),capabilities(7),sem_overview(7),sysvipc(7),time(7)

COLOPHON        top

       This page is part of theman-pages (Linux kernel and C library       user-space interface documentation) project.  Information about       the project can be found at        ⟨https://www.kernel.org/doc/man-pages/⟩.  If you have a bug report       for this manual page, see       ⟨https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/tree/CONTRIBUTING⟩.       This page was obtained from the tarball man-pages-6.15.tar.gz       fetched from       ⟨https://mirrors.edge.kernel.org/pub/linux/docs/man-pages/⟩ on       2025-08-11.  If you discover any rendering problems in this HTML       version of the page, or you believe there is a better or more up-       to-date source for the page, or you have corrections or       improvements to the information in this COLOPHON (which isnot       part of the original manual page), send a mail to       man-pages@man7.orgLinux man-pages 6.15            2025-05-17semop(2)

Pages that refer to this page:ipcs(1)lsipc(1)pcp-ipcs(1)clone(2)fork(2)ipc(2)semctl(2)semget(2)syscalls(2)unshare(2)proc_sys_kernel(5)pthreads(7)sem_overview(7)signal(7)sysvipc(7)



HTML rendering created 2025-09-06 byMichael Kerrisk, author ofThe Linux Programming Interface.

For details of in-depthLinux/UNIX system programming training courses that I teach, lookhere.

Hosting byjambit GmbH.

Cover of TLPI


[8]ページ先頭

©2009-2025 Movatter.jp