376 lines
11 KiB
HTML
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"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
sem_wait, sem_timedwait, sem_trywait - lock a semaphore
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
<PRE>
|
|
<B>#include <<A HREF="file:///usr/include/semaphore.h">semaphore.h</A>></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 >= 200112L
|
|
<A NAME="lbAD"> </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> time_t tv_sec; /* Seconds */
|
|
<BR> long tv_nsec; /* Nanoseconds [0 .. 999999999] */
|
|
};
|
|
|
|
|
|
<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"> </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"> </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"> </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"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
POSIX.1-2001, POSIX.1-2008.
|
|
<A NAME="lbAI"> </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"> </A>
|
|
<H3>Program source</H3>
|
|
|
|
|
|
|
|
#include <<A HREF="file:///usr/include/unistd.h">unistd.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdio.h">stdio.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>>
|
|
#include <<A HREF="file:///usr/include/semaphore.h">semaphore.h</A>>
|
|
#include <<A HREF="file:///usr/include/time.h">time.h</A>>
|
|
#include <<A HREF="file:///usr/include/assert.h">assert.h</A>>
|
|
#include <<A HREF="file:///usr/include/errno.h">errno.h</A>>
|
|
#include <<A HREF="file:///usr/include/signal.h">signal.h</A>>
|
|
<P>
|
|
sem_t sem;
|
|
<P>
|
|
#define handle_error(msg) \
|
|
<BR> do { perror(msg); exit(EXIT_FAILURE); } while (0)
|
|
<P>
|
|
static void
|
|
handler(int sig)
|
|
{
|
|
<BR> write(STDOUT_FILENO, "sem_post() from handler\n", 24);
|
|
<BR> if (sem_post(&sem) == -1) {
|
|
<BR> write(STDERR_FILENO, "sem_post() failed\n", 18);
|
|
<BR> _exit(EXIT_FAILURE);
|
|
<BR> }
|
|
}
|
|
<P>
|
|
int
|
|
main(int argc, char *argv[])
|
|
{
|
|
<BR> struct sigaction sa;
|
|
<BR> struct timespec ts;
|
|
<BR> int s;
|
|
<P>
|
|
<BR> if (argc != 3) {
|
|
<BR> fprintf(stderr, "Usage: %s <alarm-secs> <wait-secs>\n",
|
|
<BR> argv[0]);
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> if (sem_init(&sem, 0, 0) == -1)
|
|
<BR> handle_error("sem_init");
|
|
<P>
|
|
<BR> /* Establish SIGALRM handler; set alarm timer using argv[1] */
|
|
<P>
|
|
<BR> sa.sa_handler = handler;
|
|
<BR> sigemptyset(&sa.sa_mask);
|
|
<BR> sa.sa_flags = 0;
|
|
<BR> if (sigaction(SIGALRM, &sa, NULL) == -1)
|
|
<BR> handle_error("sigaction");
|
|
<P>
|
|
<BR> alarm(atoi(argv[1]));
|
|
<P>
|
|
<BR> /* Calculate relative interval as current time plus
|
|
<BR> number of seconds given argv[2] */
|
|
<P>
|
|
<BR> if (clock_gettime(CLOCK_REALTIME, &ts) == -1)
|
|
<BR> handle_error("clock_gettime");
|
|
<P>
|
|
<BR> ts.tv_sec += atoi(argv[2]);
|
|
<P>
|
|
<BR> printf("main() about to call sem_timedwait()\n");
|
|
<BR> while ((s = sem_timedwait(&sem, &ts)) == -1 && errno == EINTR)
|
|
<BR> continue; /* Restart if interrupted by handler */
|
|
<P>
|
|
<BR> /* Check what happened */
|
|
<P>
|
|
<BR> if (s == -1) {
|
|
<BR> if (errno == ETIMEDOUT)
|
|
<BR> printf("sem_timedwait() timed out\n");
|
|
<BR> else
|
|
<BR> perror("sem_timedwait");
|
|
<BR> } else
|
|
<BR> printf("sem_timedwait() succeeded\n");
|
|
<P>
|
|
<BR> exit((s == 0) ? EXIT_SUCCESS : EXIT_FAILURE);
|
|
}
|
|
|
|
<A NAME="lbAK"> </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"> </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"> </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>
|