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

466 lines
9.2 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of SEMGET</TITLE>
</HEAD><BODY>
<H1>SEMGET</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>
semget - get a System V semaphore set identifier
<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>
</PRE>
<P>
<B>int semget(key_t </B><I>key</I><B>,</B>
<B>int </B><I>nsems</I><B>,</B>
<B>int </B><I>semflg</I><B>);</B>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
The
<B>semget</B>()
system call returns the System&nbsp;V semaphore set identifier
associated with the argument
<I>key</I>.
It may be used either to obtain the identifier of a previously created
semaphore set (when
<I>semflg</I>
is zero and
<I>key</I>
does not have the value
<B>IPC_PRIVATE</B>),
or to create a new set.
<P>
A new set of
<I>nsems</I>
semaphores is created if
<I>key</I>
has the value
<B>IPC_PRIVATE</B>
or if no existing semaphore set is associated with
<I>key</I>
and
<B>IPC_CREAT</B>
is specified in
<I>semflg</I>.
<P>
If
<I>semflg</I>
specifies both
<B>IPC_CREAT</B>
and
<B>IPC_EXCL</B>
and a semaphore set already exists for
<I>key</I>,
then
<B>semget</B>()
fails with
<I>errno</I>
set to
<B>EEXIST</B>.
(This is analogous to the effect of the combination
<B>O_CREAT | O_EXCL</B>
for
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2).)
<P>
Upon creation, the least significant 9 bits of the argument
<I>semflg</I>
define the permissions (for owner, group and others)
for the semaphore set.
These bits have the same format, and the same
meaning, as the
<I>mode</I>
argument of
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2)
(though the execute permissions are
not meaningful for semaphores, and write permissions mean permission
to alter semaphore values).
<P>
When creating a new semaphore set,
<B>semget</B>()
initializes the set's associated data structure,
<I>semid_ds</I>
(see
<B><A HREF="/cgi-bin/man/man2html?2+semctl">semctl</A></B>(2)),
as follows:
<DL COMPACT>
<DT id="1"><DD>
<I>sem_perm.cuid</I>
and
<I>sem_perm.uid</I>
are set to the effective user ID of the calling process.
<DT id="2"><DD>
<I>sem_perm.cgid</I>
and
<I>sem_perm.gid</I>
are set to the effective group ID of the calling process.
<DT id="3"><DD>
The least significant 9 bits of
<I>sem_perm.mode</I>
are set to the least significant 9 bits of
<I>semflg</I>.
<DT id="4"><DD>
<I>sem_nsems</I>
is set to the value of
<I>nsems</I>.
<DT id="5"><DD>
<I>sem_otime</I>
is set to 0.
<DT id="6"><DD>
<I>sem_ctime</I>
is set to the current time.
</DL>
<P>
The argument
<I>nsems</I>
can be 0
(a don't care)
when a semaphore set is not being created.
Otherwise,
<I>nsems</I>
must be greater than 0
and less than or equal to the maximum number of semaphores per semaphore set
(<B>SEMMSL</B>).
<P>
If the semaphore set already exists, the permissions are
verified.
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
If successful, the return value will be the semaphore set identifier
(a nonnegative integer), otherwise, -1
is returned, with
<I>errno</I>
indicating the error.
<A NAME="lbAF">&nbsp;</A>
<H2>ERRORS</H2>
On failure,
<I>errno</I>
will be set to one of the following:
<DL COMPACT>
<DT id="7"><B>EACCES</B>
<DD>
A semaphore set exists for
<I>key</I>,
but the calling process does not have permission to access the set,
and does not have the
<B>CAP_IPC_OWNER</B>
capability in the user namespace that governs its IPC namespace.
<DT id="8"><B>EEXIST</B>
<DD>
<B>IPC_CREAT</B>
and
<B>IPC_EXCL</B>
were specified in
<I>semflg</I>,
but a semaphore set already exists for
<I>key</I>.
<DT id="9"><B>EINVAL</B>
<DD>
<I>nsems</I>
is less than 0 or greater than the limit on the number
of semaphores per semaphore set
(<B>SEMMSL</B>).
<DT id="10"><B>EINVAL</B>
<DD>
A semaphore set corresponding to
<I>key</I>
already exists, but
<I>nsems</I>
is larger than the number of semaphores in that set.
<DT id="11"><B>ENOENT</B>
<DD>
No semaphore set exists for
<I>key</I>
and
<I>semflg</I>
did not specify
<B>IPC_CREAT</B>.
<DT id="12"><B>ENOMEM</B>
<DD>
A semaphore set has to be created but the system does not have
enough memory for the new data structure.
<DT id="13"><B>ENOSPC</B>
<DD>
A semaphore set has to be created but the system limit for the maximum
number of semaphore sets
(<B>SEMMNI</B>),
or the system wide maximum number of semaphores
(<B>SEMMNS</B>),
would be exceeded.
</DL>
<A NAME="lbAG">&nbsp;</A>
<H2>CONFORMING TO</H2>
SVr4, POSIX.1-2001.
<A NAME="lbAH">&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>
<B>IPC_PRIVATE</B>
isn't a flag field but a
<I>key_t</I>
type.
If this special value is used for
<I>key</I>,
the system call ignores all but the least significant 9 bits of
<I>semflg</I>
and creates a new semaphore set (on success).
<A NAME="lbAI">&nbsp;</A>
<H3>Semaphore initialization</H3>
The values of the semaphores in a newly created set are indeterminate.
(POSIX.1-2001 and POSIX.1-2008 are explicit on this point,
although POSIX.1-2008 notes that a future version of the standard
may require an implementation to initialize the semaphores to 0.)
Although Linux, like many other implementations,
initializes the semaphore values to 0,
a portable application cannot rely on this:
it should explicitly initialize the semaphores to the desired values.
<P>
Initialization can be done using
<B><A HREF="/cgi-bin/man/man2html?2+semctl">semctl</A></B>(2)
<B>SETVAL</B>
or
<B>SETALL</B>
operation.
Where multiple peers do not know who will be the first to
initialize the set, checking for a nonzero
<I>sem_otime</I>
in the associated data structure retrieved by a
<B><A HREF="/cgi-bin/man/man2html?2+semctl">semctl</A></B>(2)
<B>IPC_STAT</B>
operation can be used to avoid races.
<A NAME="lbAJ">&nbsp;</A>
<H3>Semaphore limits</H3>
The following limits on semaphore set resources affect the
<B>semget</B>()
call:
<DL COMPACT>
<DT id="14"><B>SEMMNI</B>
<DD>
System-wide limit on the number of semaphore sets.
On Linux systems before version 3.19,
the default value for this limit was 128.
Since Linux 3.19,
the default value is 32,000.
On Linux, this limit can be read and modified via the fourth field of
<I>/proc/sys/kernel/sem</I>.
<DT id="15"><B>SEMMSL</B>
<DD>
Maximum number of semaphores per semaphore ID.
On Linux systems before version 3.19,
the default value for this limit was 250.
Since Linux 3.19,
the default value is 32,000.
On Linux, this limit can be read and modified via the first field of
<I>/proc/sys/kernel/sem</I>.
<DT id="16"><B>SEMMNS</B>
<DD>
System-wide limit on the number of semaphores: policy dependent
(on Linux, this limit can be read and modified via the second field of
<I>/proc/sys/kernel/sem</I>).
Note that the number of semaphores system-wide
is also limited by the product of
<B>SEMMSL</B>
and
<B>SEMMNI</B>.
</DL>
<A NAME="lbAK">&nbsp;</A>
<H2>BUGS</H2>
The name choice
<B>IPC_PRIVATE</B>
was perhaps unfortunate,
<B>IPC_NEW</B>
would more clearly show its function.
<A NAME="lbAL">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?2+semctl">semctl</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+semop">semop</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?3+ftok">ftok</A></B>(3),
<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)
<A NAME="lbAM">&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="17"><A HREF="#lbAB">NAME</A><DD>
<DT id="18"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="19"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="20"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="21"><A HREF="#lbAF">ERRORS</A><DD>
<DT id="22"><A HREF="#lbAG">CONFORMING TO</A><DD>
<DT id="23"><A HREF="#lbAH">NOTES</A><DD>
<DL>
<DT id="24"><A HREF="#lbAI">Semaphore initialization</A><DD>
<DT id="25"><A HREF="#lbAJ">Semaphore limits</A><DD>
</DL>
<DT id="26"><A HREF="#lbAK">BUGS</A><DD>
<DT id="27"><A HREF="#lbAL">SEE ALSO</A><DD>
<DT id="28"><A HREF="#lbAM">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>