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

376 lines
11 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of SEM_WAIT</TITLE>
</HEAD><BODY>
<H1>SEM_WAIT</H1>
Section: Linux Programmer's Manual (3)<BR>Updated: 2019-03-06<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_wait, sem_timedwait, sem_trywait - lock a semaphore
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/semaphore.h">semaphore.h</A>&gt;</B>
<B>int sem_wait(sem_t *</B><I>sem</I><B>);</B>
<B>int sem_trywait(sem_t *</B><I>sem</I><B>);</B>
<B>int sem_timedwait(sem_t *</B><I>sem</I><B>, const struct timespec *</B><I>abs_timeout</I><B>);</B>
</PRE>
<P>
Link with <I>-pthread</I>.
<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>sem_timedwait</B>():
_POSIX_C_SOURCE&nbsp;&gt;=&nbsp;200112L
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
<B>sem_wait</B>()
decrements (locks) the semaphore pointed to by
<I>sem</I>.
If the semaphore's value is greater than zero,
then the decrement proceeds, and the function returns, immediately.
If the semaphore currently has the value zero,
then the call blocks until either it becomes possible to perform
the decrement (i.e., the semaphore value rises above zero),
or a signal handler interrupts the call.
<P>
<B>sem_trywait</B>()
is the same as
<B>sem_wait</B>(),
except that if the decrement cannot be immediately performed,
then call returns an error
(<I>errno</I>
set to
<B>EAGAIN</B>)
instead of blocking.
<P>
<B>sem_timedwait</B>()
is the same as
<B>sem_wait</B>(),
except that
<I>abs_timeout</I>
specifies a limit on the amount of time that the call
should block if the decrement cannot be immediately performed.
The
<I>abs_timeout</I>
argument points to a structure that specifies an absolute timeout
in seconds and nanoseconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC).
This structure is defined as follows:
<P>
struct timespec {
<BR>&nbsp;&nbsp;&nbsp;&nbsp;time_t&nbsp;tv_sec;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Seconds&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;&nbsp;&nbsp;tv_nsec;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Nanoseconds&nbsp;[0&nbsp;..&nbsp;999999999]&nbsp;*/
};
<P>
If the timeout has already expired by the time of the call,
and the semaphore could not be locked immediately,
then
<B>sem_timedwait</B>()
fails with a timeout error
(<I>errno</I>
set to
<B>ETIMEDOUT</B>).
<P>
If the operation can be performed immediately, then
<B>sem_timedwait</B>()
never fails with a timeout error, regardless of the value of
<I>abs_timeout</I>.
Furthermore, the validity of
<I>abs_timeout</I>
is not checked in this case.
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
All of these functions return 0 on success;
on error, the value of the semaphore is left unchanged,
-1 is returned, and
<I>errno</I>
is set to indicate the error.
<A NAME="lbAF">&nbsp;</A>
<H2>ERRORS</H2>
<DL COMPACT>
<DT id="1"><B>EINTR</B>
<DD>
The call was interrupted by a signal handler; see
<B><A HREF="/cgi-bin/man/man2html?7+signal">signal</A></B>(7).
<DT id="2"><B>EINVAL</B>
<DD>
<I>sem</I>
is not a valid semaphore.
</DL>
<P>
The following additional error can occur for
<B>sem_trywait</B>():
<DL COMPACT>
<DT id="3"><B>EAGAIN</B>
<DD>
The operation could not be performed without blocking (i.e., the
semaphore currently has the value zero).
</DL>
<P>
The following additional errors can occur for
<B>sem_timedwait</B>():
<DL COMPACT>
<DT id="4"><B>EINVAL</B>
<DD>
The value of
<I>abs_timeout.tv_nsecs</I>
is less than 0, or greater than or equal to 1000 million.
<DT id="5"><B>ETIMEDOUT</B>
<DD>
The call timed out before the semaphore could be locked.
</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_wait</B>(),
<B>sem_trywait</B>(),
<B>sem_timedwait</B>()
</TD><TD>Thread safety</TD><TD>MT-Safe<BR></TD></TR>
</TABLE>
<A NAME="lbAH">&nbsp;</A>
<H2>CONFORMING TO</H2>
POSIX.1-2001, POSIX.1-2008.
<A NAME="lbAI">&nbsp;</A>
<H2>EXAMPLE</H2>
<P>
The (somewhat trivial) program shown below operates on an
unnamed semaphore.
The program expects two command-line arguments.
The first argument specifies a seconds value that is used to
set an alarm timer to generate a
<B>SIGALRM</B>
signal.
This handler performs a
<B><A HREF="/cgi-bin/man/man2html?3+sem_post">sem_post</A></B>(3)
to increment the semaphore that is being waited on in
<I>main()</I>
using
<B>sem_timedwait</B>().
The second command-line argument specifies the length
of the timeout, in seconds, for
<B>sem_timedwait</B>().
The following shows what happens on two different runs of the program:
<P>
$<B> ./a.out 2 3</B>
About to call sem_timedwait()
sem_post() from handler
sem_timedwait() succeeded
$<B> ./a.out 2 1</B>
About to call sem_timedwait()
sem_timedwait() timed out
<A NAME="lbAJ">&nbsp;</A>
<H3>Program source</H3>
#include &lt;<A HREF="file:///usr/include/unistd.h">unistd.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdio.h">stdio.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/semaphore.h">semaphore.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/time.h">time.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/assert.h">assert.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/errno.h">errno.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/signal.h">signal.h</A>&gt;
<P>
sem_t sem;
<P>
#define handle_error(msg) \
<BR>&nbsp;&nbsp;&nbsp;&nbsp;do&nbsp;{&nbsp;perror(msg);&nbsp;exit(EXIT_FAILURE);&nbsp;}&nbsp;while&nbsp;(0)
<P>
static void
handler(int sig)
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;write(STDOUT_FILENO,&nbsp;&quot;sem_post()&nbsp;from&nbsp;handler\n&quot;,&nbsp;24);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(sem_post(&amp;sem)&nbsp;==&nbsp;-1)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;write(STDERR_FILENO,&nbsp;&quot;sem_post()&nbsp;failed\n&quot;,&nbsp;18);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
}
<P>
int
main(int argc, char *argv[])
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;sigaction&nbsp;sa;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;timespec&nbsp;ts;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;s;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(argc&nbsp;!=&nbsp;3)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&nbsp;&quot;Usage:&nbsp;%s&nbsp;&lt;alarm-secs&gt;&nbsp;&lt;wait-secs&gt;\n&quot;,
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;argv[0]);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(sem_init(&amp;sem,&nbsp;0,&nbsp;0)&nbsp;==&nbsp;-1)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;handle_error(&quot;sem_init&quot;);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Establish&nbsp;SIGALRM&nbsp;handler;&nbsp;set&nbsp;alarm&nbsp;timer&nbsp;using&nbsp;argv[1]&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;sa.sa_handler&nbsp;=&nbsp;handler;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;sigemptyset(&amp;sa.sa_mask);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;sa.sa_flags&nbsp;=&nbsp;0;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(sigaction(SIGALRM,&nbsp;&amp;sa,&nbsp;NULL)&nbsp;==&nbsp;-1)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;handle_error(&quot;sigaction&quot;);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;alarm(atoi(argv[1]));
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Calculate&nbsp;relative&nbsp;interval&nbsp;as&nbsp;current&nbsp;time&nbsp;plus
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;number&nbsp;of&nbsp;seconds&nbsp;given&nbsp;argv[2]&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(clock_gettime(CLOCK_REALTIME,&nbsp;&amp;ts)&nbsp;==&nbsp;-1)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;handle_error(&quot;clock_gettime&quot;);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;ts.tv_sec&nbsp;+=&nbsp;atoi(argv[2]);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;main()&nbsp;about&nbsp;to&nbsp;call&nbsp;sem_timedwait()\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;((s&nbsp;=&nbsp;sem_timedwait(&amp;sem,&nbsp;&amp;ts))&nbsp;==&nbsp;-1&nbsp;&amp;&amp;&nbsp;errno&nbsp;==&nbsp;EINTR)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;continue;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Restart&nbsp;if&nbsp;interrupted&nbsp;by&nbsp;handler&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Check&nbsp;what&nbsp;happened&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(s&nbsp;==&nbsp;-1)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(errno&nbsp;==&nbsp;ETIMEDOUT)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;sem_timedwait()&nbsp;timed&nbsp;out\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror(&quot;sem_timedwait&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;sem_timedwait()&nbsp;succeeded\n&quot;);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;exit((s&nbsp;==&nbsp;0)&nbsp;?&nbsp;EXIT_SUCCESS&nbsp;:&nbsp;EXIT_FAILURE);
}
<A NAME="lbAK">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?2+clock_gettime">clock_gettime</A></B>(2),
<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?7+sem_overview">sem_overview</A></B>(7),
<B><A HREF="/cgi-bin/man/man2html?7+time">time</A></B>(7)
<A NAME="lbAL">&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="6"><A HREF="#lbAB">NAME</A><DD>
<DT id="7"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="8"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="9"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="10"><A HREF="#lbAF">ERRORS</A><DD>
<DT id="11"><A HREF="#lbAG">ATTRIBUTES</A><DD>
<DT id="12"><A HREF="#lbAH">CONFORMING TO</A><DD>
<DT id="13"><A HREF="#lbAI">EXAMPLE</A><DD>
<DL>
<DT id="14"><A HREF="#lbAJ">Program source</A><DD>
</DL>
<DT id="15"><A HREF="#lbAK">SEE ALSO</A><DD>
<DT id="16"><A HREF="#lbAL">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>