man-pages/man2/semop.2.html
2021-03-31 01:06:50 +01:00

788 lines
16 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of SEMOP</TITLE>
</HEAD><BODY>
<H1>SEMOP</H1>
Section: Linux Programmer's Manual (2)<BR>Updated: 2019-08-02<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
semop, semtimedop - System V semaphore operations
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/sys/types.h">sys/types.h</A>&gt;</B>
<B>#include &lt;<A HREF="file:///usr/include/sys/ipc.h">sys/ipc.h</A>&gt;</B>
<B>#include &lt;<A HREF="file:///usr/include/sys/sem.h">sys/sem.h</A>&gt;</B>
<B>int semop(int </B><I>semid</I><B>, struct sembuf *</B><I>sops</I><B>, size_t </B><I>nsops</I><B>);</B>
<B>int semtimedop(int </B><I>semid</I><B>, struct sembuf *</B><I>sops</I><B>, size_t </B><I>nsops</I><B>,</B>
<B> const struct timespec *</B><I>timeout</I><B>);</B>
</PRE>
<P>
Feature Test Macro Requirements for glibc (see
<B><A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A></B>(7)):
<P>
<B>semtimedop</B>():
_GNU_SOURCE
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
Each semaphore in a System&nbsp;V semaphore set
has the following associated values:
<P>
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
<P>
<B>semop</B>()
performs operations on selected semaphores in the set indicated by
<I>semid</I>.
Each of the
<I>nsops</I>
elements in the array pointed to by
<I>sops</I>
is a structure that
specifies an operation to be performed on a single semaphore.
The elements of this structure are of type
<I>struct sembuf</I>,
containing the following members:
<P>
unsigned short sem_num; /* semaphore number */
short sem_op; /* semaphore operation */
short sem_flg; /* operation flags */
<P>
Flags recognized in
<I>sem_flg</I>
are
<B>IPC_NOWAIT</B>
and
<B>SEM_UNDO</B>.
If an operation specifies
<B>SEM_UNDO</B>,
it will be automatically undone when the process terminates.
<P>
The set of operations contained in
<I>sops</I>
is performed in
<I>array order</I>,
and
<I>atomically</I>,
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
<B>IPC_NOWAIT</B>
flag in the individual
<I>sem_flg</I>
fields, as noted below.
<P>
Each operation is performed on the
<I>sem_num</I>-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 of
<I>sem_op</I>.
<P>
If
<I>sem_op</I>
is a positive integer, the operation adds this value to
the semaphore value
(<I>semval</I>).
Furthermore, if
<B>SEM_UNDO</B>
is specified for this operation, the system subtracts the value
<I>sem_op</I>
from the semaphore adjustment
(<I>semadj</I>)
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.
<P>
If
<I>sem_op</I>
is zero, the process must have read permission on the semaphore
set.
This is a &quot;wait-for-zero&quot; operation: if
<I>semval</I>
is zero, the operation can immediately proceed.
Otherwise, if
<B>IPC_NOWAIT</B>
is specified in
<I>sem_flg</I>,
<B>semop</B>()
fails with
<I>errno</I>
set to
<B>EAGAIN</B>
(and none of the operations in
<I>sops</I>
is performed).
Otherwise,
<I>semzcnt</I>
(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:
<DL COMPACT>
<DT id="1">&bull;<DD>
<I>semval</I>
becomes 0, at which time the value of
<I>semzcnt</I>
is decremented.
<DT id="2">&bull;<DD>
The semaphore set
is removed:
<B>semop</B>()
fails, with
<I>errno</I>
set to
<B>EIDRM</B>.
<DT id="3">&bull;<DD>
The calling thread catches a signal:
the value of
<I>semzcnt</I>
is decremented and
<B>semop</B>()
fails, with
<I>errno</I>
set to
<B>EINTR</B>.
</DL>
<P>
If
<I>sem_op</I>
is less than zero, the process must have alter permission on the
semaphore set.
If
<I>semval</I>
is greater than or equal to the absolute value of
<I>sem_op</I>,
the operation can proceed immediately:
the absolute value of
<I>sem_op</I>
is subtracted from
<I>semval</I>,
and, if
<B>SEM_UNDO</B>
is specified for this operation, the system adds the absolute value of
<I>sem_op</I>
to the semaphore adjustment
(<I>semadj</I>)
value for this semaphore.
If the absolute value of
<I>sem_op</I>
is greater than
<I>semval</I>,
and
<B>IPC_NOWAIT</B>
is specified in
<I>sem_flg</I>,
<B>semop</B>()
fails, with
<I>errno</I>
set to
<B>EAGAIN</B>
(and none of the operations in
<I>sops</I>
is performed).
Otherwise,
<I>semncnt</I>
(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:
<DL COMPACT>
<DT id="4">&bull;<DD>
<I>semval</I>
becomes greater than or equal to the absolute value of
<I>sem_op</I>:
the operation now proceeds, as described above.
<DT id="5">&bull;<DD>
The semaphore set is removed from the system:
<B>semop</B>()
fails, with
<I>errno</I>
set to
<B>EIDRM</B>.
<DT id="6">&bull;<DD>
The calling thread catches a signal:
the value of
<I>semncnt</I>
is decremented and
<B>semop</B>()
fails, with
<I>errno</I>
set to
<B>EINTR</B>.
</DL>
<P>
On successful completion, the
<I>sempid</I>
value for each semaphore specified in the array pointed to by
<I>sops</I>
is set to the caller's process ID.
In addition, the
<I>sem_otime</I>
is set to the current time.
<A NAME="lbAE">&nbsp;</A>
<H3>semtimedop()</H3>
<B>semtimedop</B>()
behaves identically to
<B>semop</B>()
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 the
<I>timespec</I>
structure whose address is passed in the
<I>timeout</I>
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,
<B>semtimedop</B>()
fails with
<I>errno</I>
set to
<B>EAGAIN</B>
(and none of the operations in
<I>sops</I>
is performed).
If the
<I>timeout</I>
argument is NULL,
then
<B>semtimedop</B>()
behaves exactly like
<B>semop</B>().
<P>
Note that if
<B>semtimedop</B>()
is interrupted by a signal, causing the call to fail with the error
<B>EINTR</B>,
the contents of
<I>timeout</I>
are left unchanged.
<A NAME="lbAF">&nbsp;</A>
<H2>RETURN VALUE</H2>
If successful,
<B>semop</B>()
and
<B>semtimedop</B>()
return 0;
otherwise they return -1
with
<I>errno</I>
indicating the error.
<A NAME="lbAG">&nbsp;</A>
<H2>ERRORS</H2>
On failure,
<I>errno</I>
is set to one of the following:
<DL COMPACT>
<DT id="7"><B>E2BIG</B>
<DD>
The argument
<I>nsops</I>
is greater than
<B>SEMOPM</B>,
the maximum number of operations allowed per system
call.
<DT id="8"><B>EACCES</B>
<DD>
The calling process does not have the permissions required
to perform the specified semaphore operations,
and does not have the
<B>CAP_IPC_OWNER</B>
capability in the user namespace that governs its IPC namespace.
<DT id="9"><B>EAGAIN</B>
<DD>
An operation could not proceed immediately and either
<B>IPC_NOWAIT</B>
was specified in
<I>sem_flg</I>
or the time limit specified in
<I>timeout</I>
expired.
<DT id="10"><B>EFAULT</B>
<DD>
An address specified in either the
<I>sops</I>
or the
<I>timeout</I>
argument isn't accessible.
<DT id="11"><B>EFBIG</B>
<DD>
For some operation the value of
<I>sem_num</I>
is less than 0 or greater than or equal to the number
of semaphores in the set.
<DT id="12"><B>EIDRM</B>
<DD>
The semaphore set was removed.
<DT id="13"><B>EINTR</B>
<DD>
While blocked in this system call, the thread caught a signal; see
<B><A HREF="/cgi-bin/man/man2html?7+signal">signal</A></B>(7).
<DT id="14"><B>EINVAL</B>
<DD>
The semaphore set doesn't exist, or
<I>semid</I>
is less than zero, or
<I>nsops</I>
has a nonpositive value.
<DT id="15"><B>ENOMEM</B>
<DD>
The
<I>sem_flg</I>
of some operation specified
<B>SEM_UNDO</B>
and the system does not have enough memory to allocate the undo
structure.
<DT id="16"><B>ERANGE</B>
<DD>
For some operation
<I>sem_op+semval</I>
is greater than
<B>SEMVMX</B>,
the implementation dependent maximum value for
<I>semval</I>.
</DL>
<A NAME="lbAH">&nbsp;</A>
<H2>VERSIONS</H2>
<B>semtimedop</B>()
first appeared in Linux 2.5.52,
and was subsequently backported into kernel 2.4.22.
Glibc support for
<B>semtimedop</B>()
first appeared in version 2.3.3.
<A NAME="lbAI">&nbsp;</A>
<H2>CONFORMING TO</H2>
POSIX.1-2001, POSIX.1-2008, SVr4.
<A NAME="lbAJ">&nbsp;</A>
<H2>NOTES</H2>
The inclusion of
<I>&lt;<A HREF="file:///usr/include/sys/types.h">sys/types.h</A>&gt;</I>
and
<I>&lt;<A HREF="file:///usr/include/sys/ipc.h">sys/ipc.h</A>&gt;</I>
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.
<P>
The
<I>sem_undo</I>
structures of a process aren't inherited by the child produced by
<B><A HREF="/cgi-bin/man/man2html?2+fork">fork</A></B>(2),
but they are inherited across an
<B><A HREF="/cgi-bin/man/man2html?2+execve">execve</A></B>(2)
system call.
<P>
<B>semop</B>()
is never automatically restarted after being interrupted by a signal handler,
regardless of the setting of the
<B>SA_RESTART</B>
flag when establishing a signal handler.
<P>
A semaphore adjustment
(<I>semadj</I>)
value is a per-process, per-semaphore integer that is the negated sum
of all operations performed on a semaphore specifying the
<B>SEM_UNDO</B>
flag.
Each process has a list of
<I>semadj</I>
values---one value for each semaphore on which it has operated using
<B>SEM_UNDO</B>.
When a process terminates, each of its per-semaphore
<I>semadj</I>
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
<B>SETVAL</B>
or
<B>SETALL</B>
request to
<B><A HREF="/cgi-bin/man/man2html?2+semctl">semctl</A></B>(2),
the corresponding
<I>semadj</I>
values in all processes are cleared.
The
<B><A HREF="/cgi-bin/man/man2html?2+clone">clone</A></B>(2)
<B>CLONE_SYSVSEM</B>
flag allows more than one process to share a
<I>semadj</I>
list; see
<B><A HREF="/cgi-bin/man/man2html?2+clone">clone</A></B>(2)
for details.
<P>
The <I>semval</I>, <I>sempid</I>, <I>semzcnt</I>, and <I>semnct</I> values
for a semaphore can all be retrieved using appropriate
<B><A HREF="/cgi-bin/man/man2html?2+semctl">semctl</A></B>(2)
calls.
<A NAME="lbAK">&nbsp;</A>
<H3>Semaphore limits</H3>
The following limits on semaphore set resources affect the
<B>semop</B>()
call:
<DL COMPACT>
<DT id="17"><B>SEMOPM</B>
<DD>
Maximum number of operations allowed for one
<B>semop</B>()
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
<I>/proc/sys/kernel/sem</I>.
<I>Note</I>:
this limit should not be raised above 1000,
because of the risk of that
<B>semop</B>()
fails due to kernel memory fragmentation when allocating memory to copy the
<I>sops</I>
array.
<DT id="18"><B>SEMVMX</B>
<DD>
Maximum allowable value for
<I>semval</I>:
implementation dependent (32767).
</DL>
<P>
The implementation has no intrinsic limits for
the adjust on exit maximum value
(<B>SEMAEM</B>),
the system wide maximum number of undo structures
(<B>SEMMNU</B>)
and the per-process maximum number of undo entries system parameters.
<A NAME="lbAL">&nbsp;</A>
<H2>BUGS</H2>
When a process terminates, its set of associated
<I>semadj</I>
structures is used to undo the effect of all of the
semaphore operations it performed with the
<B>SEM_UNDO</B>
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
<B>IPC_NOWAIT</B>
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.
<P>
In kernels 2.6.x, x &lt;= 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 kernel 2.6.11.
<A NAME="lbAM">&nbsp;</A>
<H2>EXAMPLE</H2>
The following code segment uses
<B>semop</B>()
to atomically wait for the value of semaphore 0 to become zero,
and then increment the semaphore value by one.
<P>
struct sembuf sops[2];
int semid;
<P>
/* Code to set <I>semid</I> omitted */
<P>
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;
<P>
sops[1].sem_num = 0; /* Operate on semaphore 0 */
sops[1].sem_op = 1; /* Increment value by one */
sops[1].sem_flg = 0;
<P>
if (semop(semid, sops, 2) == -1) {
<BR>&nbsp;&nbsp;&nbsp;&nbsp;perror(&quot;semop&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
}
<A NAME="lbAN">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?2+clone">clone</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+semctl">semctl</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+semget">semget</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+sigaction">sigaction</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?7+capabilities">capabilities</A></B>(7),
<B><A HREF="/cgi-bin/man/man2html?7+sem_overview">sem_overview</A></B>(7),
<B><A HREF="/cgi-bin/man/man2html?7+sysvipc">sysvipc</A></B>(7),
<B><A HREF="/cgi-bin/man/man2html?7+time">time</A></B>(7)
<A NAME="lbAO">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="19"><A HREF="#lbAB">NAME</A><DD>
<DT id="20"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="21"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DL>
<DT id="22"><A HREF="#lbAE">semtimedop()</A><DD>
</DL>
<DT id="23"><A HREF="#lbAF">RETURN VALUE</A><DD>
<DT id="24"><A HREF="#lbAG">ERRORS</A><DD>
<DT id="25"><A HREF="#lbAH">VERSIONS</A><DD>
<DT id="26"><A HREF="#lbAI">CONFORMING TO</A><DD>
<DT id="27"><A HREF="#lbAJ">NOTES</A><DD>
<DL>
<DT id="28"><A HREF="#lbAK">Semaphore limits</A><DD>
</DL>
<DT id="29"><A HREF="#lbAL">BUGS</A><DD>
<DT id="30"><A HREF="#lbAM">EXAMPLE</A><DD>
<DT id="31"><A HREF="#lbAN">SEE ALSO</A><DD>
<DT id="32"><A HREF="#lbAO">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:34 GMT, March 31, 2021
</BODY>
</HTML>