man-pages/man3/sem_open.3.html
2021-03-31 01:06:50 +01:00

284 lines
5.9 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of SEM_OPEN</TITLE>
</HEAD><BODY>
<H1>SEM_OPEN</H1>
Section: Linux Programmer's Manual (3)<BR>Updated: 2017-09-15<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>
sem_open - initialize and open a named semaphore
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/fcntl.h">fcntl.h</A>&gt;</B> /* For O_* constants */
<B>#include &lt;<A HREF="file:///usr/include/sys/stat.h">sys/stat.h</A>&gt;</B> /* For mode constants */
<B>#include &lt;<A HREF="file:///usr/include/semaphore.h">semaphore.h</A>&gt;</B>
<B>sem_t *sem_open(const char *</B><I>name</I><B>, int </B><I>oflag</I><B>);</B>
<B>sem_t *sem_open(const char *</B><I>name</I><B>, int </B><I>oflag</I><B>, </B>
<B> mode_t </B><I>mode</I><B>, unsigned int </B><I>value</I><B>);</B>
</PRE>
<P>
Link with <I>-pthread</I>.
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
<B>sem_open</B>()
creates a new POSIX semaphore or opens an existing semaphore.
The semaphore is identified by
<I>name</I>.
For details of the construction of
<I>name</I>,
see
<B><A HREF="/cgi-bin/man/man2html?7+sem_overview">sem_overview</A></B>(7).
<P>
The
<I>oflag</I>
argument specifies flags that control the operation of the call.
(Definitions of the flags values can be obtained by including
<I>&lt;<A HREF="file:///usr/include/fcntl.h">fcntl.h</A>&gt;</I>.)
If
<B>O_CREAT</B>
is specified in
<I>oflag</I>,
then the semaphore is created if
it does not already exist.
The owner (user ID) of the semaphore is set to the effective
user ID of the calling process.
The group ownership (group ID) is set to the effective group ID
of the calling process.
If both
<B>O_CREAT</B>
and
<B>O_EXCL</B>
are specified in
<I>oflag</I>,
then an error is returned if a semaphore with the given
<I>name</I>
already exists.
<P>
If
<B>O_CREAT</B>
is specified in
<I>oflag</I>,
then two additional arguments must be supplied.
The
<I>mode</I>
argument specifies the permissions to be placed on the new semaphore,
as for
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2).
(Symbolic definitions for the permissions bits can be obtained by including
<I>&lt;<A HREF="file:///usr/include/sys/stat.h">sys/stat.h</A>&gt;</I>.)
The permissions settings are masked against the process umask.
Both read and write permission should be granted to each class of
user that will access the semaphore.
The
<I>value</I>
argument specifies the initial value for the new semaphore.
If
<B>O_CREAT</B>
is specified, and a semaphore with the given
<I>name</I>
already exists, then
<I>mode</I>
and
<I>value</I>
are ignored.
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
On success,
<B>sem_open</B>()
returns the address of the new semaphore;
this address is used when calling other semaphore-related functions.
On error,
<B>sem_open</B>()
returns
<B>SEM_FAILED</B>,
with
<I>errno</I>
set to indicate the error.
<A NAME="lbAF">&nbsp;</A>
<H2>ERRORS</H2>
<DL COMPACT>
<DT id="1"><B>EACCES</B>
<DD>
The semaphore exists, but the caller does not have permission to
open it.
<DT id="2"><B>EEXIST</B>
<DD>
Both
<B>O_CREAT</B>
and
<B>O_EXCL</B>
were specified in
<I>oflag</I>,
but a semaphore with this
<I>name</I>
already exists.
<DT id="3"><B>EINVAL</B>
<DD>
<I>value</I>
was greater than
<B>SEM_VALUE_MAX</B>.
<DT id="4"><B>EINVAL</B>
<DD>
<I>name</I>
consists of just &quot;/&quot;, followed by no other characters.
<DT id="5"><B>EMFILE</B>
<DD>
The per-process limit on the number of open file descriptors has been reached.
<DT id="6"><B>ENAMETOOLONG</B>
<DD>
<I>name</I>
was too long.
<DT id="7"><B>ENFILE</B>
<DD>
The system-wide limit on the total number of open files has been reached.
<DT id="8"><B>ENOENT</B>
<DD>
The
<B>O_CREAT</B>
flag was not specified in
<I>oflag</I>
and no semaphore with this
<I>name</I>
exists;
or,
<B>O_CREAT</B>
was specified, but
<I>name</I>
wasn't well formed.
<DT id="9"><B>ENOMEM</B>
<DD>
Insufficient memory.
</DL>
<A NAME="lbAG">&nbsp;</A>
<H2>ATTRIBUTES</H2>
For an explanation of the terms used in this section, see
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
<TABLE BORDER>
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
<TR VALIGN=top><TD>
<B>sem_open</B>()
</TD><TD>Thread safety</TD><TD>MT-Safe<BR></TD></TR>
</TABLE>
<P>
<A NAME="lbAH">&nbsp;</A>
<H2>CONFORMING TO</H2>
POSIX.1-2001, POSIX.1-2008.
<A NAME="lbAI">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?3+sem_close">sem_close</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+sem_getvalue">sem_getvalue</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+sem_post">sem_post</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+sem_unlink">sem_unlink</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+sem_wait">sem_wait</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?7+sem_overview">sem_overview</A></B>(7)
<A NAME="lbAJ">&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="10"><A HREF="#lbAB">NAME</A><DD>
<DT id="11"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="12"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="13"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="14"><A HREF="#lbAF">ERRORS</A><DD>
<DT id="15"><A HREF="#lbAG">ATTRIBUTES</A><DD>
<DT id="16"><A HREF="#lbAH">CONFORMING TO</A><DD>
<DT id="17"><A HREF="#lbAI">SEE ALSO</A><DD>
<DT id="18"><A HREF="#lbAJ">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:56 GMT, March 31, 2021
</BODY>
</HTML>