|
Packit |
7cfc04 |
'\" t
|
|
Packit |
7cfc04 |
.\" Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
|
|
Packit |
7cfc04 |
.\"
|
|
Packit |
7cfc04 |
.\" %%%LICENSE_START(VERBATIM)
|
|
Packit |
7cfc04 |
.\" Permission is granted to make and distribute verbatim copies of this
|
|
Packit |
7cfc04 |
.\" manual provided the copyright notice and this permission notice are
|
|
Packit |
7cfc04 |
.\" preserved on all copies.
|
|
Packit |
7cfc04 |
.\"
|
|
Packit |
7cfc04 |
.\" Permission is granted to copy and distribute modified versions of this
|
|
Packit |
7cfc04 |
.\" manual under the conditions for verbatim copying, provided that the
|
|
Packit |
7cfc04 |
.\" entire resulting derived work is distributed under the terms of a
|
|
Packit |
7cfc04 |
.\" permission notice identical to this one.
|
|
Packit |
7cfc04 |
.\"
|
|
Packit |
7cfc04 |
.\" Since the Linux kernel and libraries are constantly changing, this
|
|
Packit |
7cfc04 |
.\" manual page may be incorrect or out-of-date. The author(s) assume no
|
|
Packit |
7cfc04 |
.\" responsibility for errors or omissions, or for damages resulting from
|
|
Packit |
7cfc04 |
.\" the use of the information contained herein. The author(s) may not
|
|
Packit |
7cfc04 |
.\" have taken the same level of care in the production of this manual,
|
|
Packit |
7cfc04 |
.\" which is licensed free of charge, as they might when working
|
|
Packit |
7cfc04 |
.\" professionally.
|
|
Packit |
7cfc04 |
.\"
|
|
Packit |
7cfc04 |
.\" Formatted or processed versions of this manual, if unaccompanied by
|
|
Packit |
7cfc04 |
.\" the source, must acknowledge the copyright and authors of this work.
|
|
Packit |
7cfc04 |
.\" %%%LICENSE_END
|
|
Packit |
7cfc04 |
.\"
|
|
Packit |
7cfc04 |
.TH SEM_WAIT 3 2017-09-15 "Linux" "Linux Programmer's Manual"
|
|
Packit |
7cfc04 |
.SH NAME
|
|
Packit |
7cfc04 |
sem_wait, sem_timedwait, sem_trywait \- lock a semaphore
|
|
Packit |
7cfc04 |
.SH SYNOPSIS
|
|
Packit |
7cfc04 |
.nf
|
|
Packit |
7cfc04 |
.B #include <semaphore.h>
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
.BI "int sem_wait(sem_t *" sem );
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
.BI "int sem_trywait(sem_t *" sem );
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
.BI "int sem_timedwait(sem_t *" sem ", const struct timespec *" abs_timeout );
|
|
Packit |
7cfc04 |
.fi
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
Link with \fI\-pthread\fP.
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
.in -4n
|
|
Packit |
7cfc04 |
Feature Test Macro Requirements for glibc (see
|
|
Packit |
7cfc04 |
.BR feature_test_macros (7)):
|
|
Packit |
7cfc04 |
.in
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
.BR sem_timedwait ():
|
|
Packit |
7cfc04 |
_POSIX_C_SOURCE\ >=\ 200112L
|
|
Packit |
7cfc04 |
.SH DESCRIPTION
|
|
Packit |
7cfc04 |
.BR sem_wait ()
|
|
Packit |
7cfc04 |
decrements (locks) the semaphore pointed to by
|
|
Packit |
7cfc04 |
.IR sem .
|
|
Packit |
7cfc04 |
If the semaphore's value is greater than zero,
|
|
Packit |
7cfc04 |
then the decrement proceeds, and the function returns, immediately.
|
|
Packit |
7cfc04 |
If the semaphore currently has the value zero,
|
|
Packit |
7cfc04 |
then the call blocks until either it becomes possible to perform
|
|
Packit |
7cfc04 |
the decrement (i.e., the semaphore value rises above zero),
|
|
Packit |
7cfc04 |
or a signal handler interrupts the call.
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
.BR sem_trywait ()
|
|
Packit |
7cfc04 |
is the same as
|
|
Packit |
7cfc04 |
.BR sem_wait (),
|
|
Packit |
7cfc04 |
except that if the decrement cannot be immediately performed,
|
|
Packit |
7cfc04 |
then call returns an error
|
|
Packit |
7cfc04 |
.RI ( errno
|
|
Packit |
7cfc04 |
set to
|
|
Packit |
7cfc04 |
.BR EAGAIN )
|
|
Packit |
7cfc04 |
instead of blocking.
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
.BR sem_timedwait ()
|
|
Packit |
7cfc04 |
is the same as
|
|
Packit |
7cfc04 |
.BR sem_wait (),
|
|
Packit |
7cfc04 |
except that
|
|
Packit |
7cfc04 |
.I abs_timeout
|
|
Packit |
7cfc04 |
specifies a limit on the amount of time that the call
|
|
Packit |
7cfc04 |
should block if the decrement cannot be immediately performed.
|
|
Packit |
7cfc04 |
The
|
|
Packit |
7cfc04 |
.I abs_timeout
|
|
Packit |
7cfc04 |
argument points to a structure that specifies an absolute timeout
|
|
Packit |
7cfc04 |
in seconds and nanoseconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC).
|
|
Packit |
7cfc04 |
This structure is defined as follows:
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
.in +4n
|
|
Packit |
7cfc04 |
.EX
|
|
Packit |
7cfc04 |
struct timespec {
|
|
Packit |
7cfc04 |
time_t tv_sec; /* Seconds */
|
|
Packit |
7cfc04 |
long tv_nsec; /* Nanoseconds [0 .. 999999999] */
|
|
Packit |
7cfc04 |
};
|
|
Packit |
7cfc04 |
.EE
|
|
Packit |
7cfc04 |
.in
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
If the timeout has already expired by the time of the call,
|
|
Packit |
7cfc04 |
and the semaphore could not be locked immediately,
|
|
Packit |
7cfc04 |
then
|
|
Packit |
7cfc04 |
.BR sem_timedwait ()
|
|
Packit |
7cfc04 |
fails with a timeout error
|
|
Packit |
7cfc04 |
.RI ( errno
|
|
Packit |
7cfc04 |
set to
|
|
Packit |
7cfc04 |
.BR ETIMEDOUT ).
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
If the operation can be performed immediately, then
|
|
Packit |
7cfc04 |
.BR sem_timedwait ()
|
|
Packit |
7cfc04 |
never fails with a timeout error, regardless of the value of
|
|
Packit |
7cfc04 |
.IR abs_timeout .
|
|
Packit |
7cfc04 |
Furthermore, the validity of
|
|
Packit |
7cfc04 |
.I abs_timeout
|
|
Packit |
7cfc04 |
is not checked in this case.
|
|
Packit |
7cfc04 |
.SH RETURN VALUE
|
|
Packit |
7cfc04 |
All of these functions return 0 on success;
|
|
Packit |
7cfc04 |
on error, the value of the semaphore is left unchanged,
|
|
Packit |
7cfc04 |
\-1 is returned, and
|
|
Packit |
7cfc04 |
.I errno
|
|
Packit |
7cfc04 |
is set to indicate the error.
|
|
Packit |
7cfc04 |
.SH ERRORS
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.B EINTR
|
|
Packit |
7cfc04 |
The call was interrupted by a signal handler; see
|
|
Packit |
7cfc04 |
.BR signal (7).
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.B EINVAL
|
|
Packit |
7cfc04 |
.I sem
|
|
Packit |
7cfc04 |
is not a valid semaphore.
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
The following additional error can occur for
|
|
Packit |
7cfc04 |
.BR sem_trywait ():
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.B EAGAIN
|
|
Packit |
7cfc04 |
The operation could not be performed without blocking (i.e., the
|
|
Packit |
7cfc04 |
semaphore currently has the value zero).
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
The following additional errors can occur for
|
|
Packit |
7cfc04 |
.BR sem_timedwait ():
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.B EINVAL
|
|
Packit |
7cfc04 |
The value of
|
|
Packit |
7cfc04 |
.I abs_timeout.tv_nsecs
|
|
Packit |
7cfc04 |
is less than 0, or greater than or equal to 1000 million.
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.B ETIMEDOUT
|
|
Packit |
7cfc04 |
The call timed out before the semaphore could be locked.
|
|
Packit |
7cfc04 |
.\" POSIX.1-2001 also allows EDEADLK -- "A deadlock condition
|
|
Packit |
7cfc04 |
.\" was detected", but this does not occur on Linux(?).
|
|
Packit |
7cfc04 |
.SH ATTRIBUTES
|
|
Packit |
7cfc04 |
For an explanation of the terms used in this section, see
|
|
Packit |
7cfc04 |
.BR attributes (7).
|
|
Packit |
7cfc04 |
.TS
|
|
Packit |
7cfc04 |
allbox;
|
|
Packit |
7cfc04 |
lbw26 lb lb
|
|
Packit |
7cfc04 |
l l l.
|
|
Packit |
7cfc04 |
Interface Attribute Value
|
|
Packit |
7cfc04 |
T{
|
|
Packit |
7cfc04 |
.BR sem_wait (),
|
|
Packit |
7cfc04 |
.BR sem_trywait (),
|
|
Packit |
7cfc04 |
.BR sem_timedwait ()
|
|
Packit |
7cfc04 |
T} Thread safety MT-Safe
|
|
Packit |
7cfc04 |
.TE
|
|
Packit |
7cfc04 |
.SH CONFORMING TO
|
|
Packit |
7cfc04 |
POSIX.1-2001, POSIX.1-2008.
|
|
Packit |
7cfc04 |
.SH EXAMPLE
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
The (somewhat trivial) program shown below operates on an
|
|
Packit |
7cfc04 |
unnamed semaphore.
|
|
Packit |
7cfc04 |
The program expects two command-line arguments.
|
|
Packit |
7cfc04 |
The first argument specifies a seconds value that is used to
|
|
Packit |
7cfc04 |
set an alarm timer to generate a
|
|
Packit |
7cfc04 |
.B SIGALRM
|
|
Packit |
7cfc04 |
signal.
|
|
Packit |
7cfc04 |
This handler performs a
|
|
Packit |
7cfc04 |
.BR sem_post (3)
|
|
Packit |
7cfc04 |
to increment the semaphore that is being waited on in
|
|
Packit |
7cfc04 |
.I main()
|
|
Packit |
7cfc04 |
using
|
|
Packit |
7cfc04 |
.BR sem_timedwait ().
|
|
Packit |
7cfc04 |
The second command-line argument specifies the length
|
|
Packit |
7cfc04 |
of the timeout, in seconds, for
|
|
Packit |
7cfc04 |
.BR sem_timedwait ().
|
|
Packit |
7cfc04 |
The following shows what happens on two different runs of the program:
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
.in +4n
|
|
Packit |
7cfc04 |
.EX
|
|
Packit |
7cfc04 |
.RB "$" " ./a.out 2 3"
|
|
Packit |
7cfc04 |
About to call sem_timedwait()
|
|
Packit |
7cfc04 |
sem_post() from handler
|
|
Packit |
7cfc04 |
sem_timedwait() succeeded
|
|
Packit |
7cfc04 |
.RB "$" " ./a.out 2 1"
|
|
Packit |
7cfc04 |
About to call sem_timedwait()
|
|
Packit |
7cfc04 |
sem_timedwait() timed out
|
|
Packit |
7cfc04 |
.EE
|
|
Packit |
7cfc04 |
.in
|
|
Packit |
7cfc04 |
.SS Program source
|
|
Packit |
7cfc04 |
\&
|
|
Packit |
7cfc04 |
.EX
|
|
Packit |
7cfc04 |
#include <unistd.h>
|
|
Packit |
7cfc04 |
#include <stdio.h>
|
|
Packit |
7cfc04 |
#include <stdlib.h>
|
|
Packit |
7cfc04 |
#include <semaphore.h>
|
|
Packit |
7cfc04 |
#include <time.h>
|
|
Packit |
7cfc04 |
#include <assert.h>
|
|
Packit |
7cfc04 |
#include <errno.h>
|
|
Packit |
7cfc04 |
#include <signal.h>
|
|
Packit |
7cfc04 |
|
|
Packit |
7cfc04 |
sem_t sem;
|
|
Packit |
7cfc04 |
|
|
Packit |
7cfc04 |
#define handle_error(msg) \\
|
|
Packit |
7cfc04 |
do { perror(msg); exit(EXIT_FAILURE); } while (0)
|
|
Packit |
7cfc04 |
|
|
Packit |
7cfc04 |
static void
|
|
Packit |
7cfc04 |
handler(int sig)
|
|
Packit |
7cfc04 |
{
|
|
Packit |
7cfc04 |
write(STDOUT_FILENO, "sem_post() from handler\\n", 24);
|
|
Packit |
7cfc04 |
if (sem_post(&sem) == \-1) {
|
|
Packit |
7cfc04 |
write(STDERR_FILENO, "sem_post() failed\\n", 18);
|
|
Packit |
7cfc04 |
_exit(EXIT_FAILURE);
|
|
Packit |
7cfc04 |
}
|
|
Packit |
7cfc04 |
}
|
|
Packit |
7cfc04 |
|
|
Packit |
7cfc04 |
int
|
|
Packit |
7cfc04 |
main(int argc, char *argv[])
|
|
Packit |
7cfc04 |
{
|
|
Packit |
7cfc04 |
struct sigaction sa;
|
|
Packit |
7cfc04 |
struct timespec ts;
|
|
Packit |
7cfc04 |
int s;
|
|
Packit |
7cfc04 |
|
|
Packit |
7cfc04 |
if (argc != 3) {
|
|
Packit |
7cfc04 |
fprintf(stderr, "Usage: %s <alarm\-secs> <wait\-secs>\\n",
|
|
Packit |
7cfc04 |
argv[0]);
|
|
Packit |
7cfc04 |
exit(EXIT_FAILURE);
|
|
Packit |
7cfc04 |
}
|
|
Packit |
7cfc04 |
|
|
Packit |
7cfc04 |
if (sem_init(&sem, 0, 0) == \-1)
|
|
Packit |
7cfc04 |
handle_error("sem_init");
|
|
Packit |
7cfc04 |
|
|
Packit |
7cfc04 |
/* Establish SIGALRM handler; set alarm timer using argv[1] */
|
|
Packit |
7cfc04 |
|
|
Packit |
7cfc04 |
sa.sa_handler = handler;
|
|
Packit |
7cfc04 |
sigemptyset(&sa.sa_mask);
|
|
Packit |
7cfc04 |
sa.sa_flags = 0;
|
|
Packit |
7cfc04 |
if (sigaction(SIGALRM, &sa, NULL) == \-1)
|
|
Packit |
7cfc04 |
handle_error("sigaction");
|
|
Packit |
7cfc04 |
|
|
Packit |
7cfc04 |
alarm(atoi(argv[1]));
|
|
Packit |
7cfc04 |
|
|
Packit |
7cfc04 |
/* Calculate relative interval as current time plus
|
|
Packit |
7cfc04 |
number of seconds given argv[2] */
|
|
Packit |
7cfc04 |
|
|
Packit |
7cfc04 |
if (clock_gettime(CLOCK_REALTIME, &ts) == \-1)
|
|
Packit |
7cfc04 |
handle_error("clock_gettime");
|
|
Packit |
7cfc04 |
|
|
Packit |
7cfc04 |
ts.tv_sec += atoi(argv[2]);
|
|
Packit |
7cfc04 |
|
|
Packit |
7cfc04 |
printf("main() about to call sem_timedwait()\\n");
|
|
Packit |
7cfc04 |
while ((s = sem_timedwait(&sem, &ts)) == \-1 && errno == EINTR)
|
|
Packit |
7cfc04 |
continue; /* Restart if interrupted by handler */
|
|
Packit |
7cfc04 |
|
|
Packit |
7cfc04 |
/* Check what happened */
|
|
Packit |
7cfc04 |
|
|
Packit |
7cfc04 |
if (s == \-1) {
|
|
Packit |
7cfc04 |
if (errno == ETIMEDOUT)
|
|
Packit |
7cfc04 |
printf("sem_timedwait() timed out\\n");
|
|
Packit |
7cfc04 |
else
|
|
Packit |
7cfc04 |
perror("sem_timedwait");
|
|
Packit |
7cfc04 |
} else
|
|
Packit |
7cfc04 |
printf("sem_timedwait() succeeded\\n");
|
|
Packit |
7cfc04 |
|
|
Packit |
7cfc04 |
exit((s == 0) ? EXIT_SUCCESS : EXIT_FAILURE);
|
|
Packit |
7cfc04 |
}
|
|
Packit |
7cfc04 |
.EE
|
|
Packit |
7cfc04 |
.SH SEE ALSO
|
|
Packit |
7cfc04 |
.BR clock_gettime (2),
|
|
Packit |
7cfc04 |
.BR sem_getvalue (3),
|
|
Packit |
7cfc04 |
.BR sem_post (3),
|
|
Packit |
7cfc04 |
.BR sem_overview (7),
|
|
Packit |
7cfc04 |
.BR time (7)
|
|
Packit |
7cfc04 |
.SH COLOPHON
|
|
Packit |
7cfc04 |
This page is part of release 4.15 of the Linux
|
|
Packit |
7cfc04 |
.I man-pages
|
|
Packit |
7cfc04 |
project.
|
|
Packit |
7cfc04 |
A description of the project,
|
|
Packit |
7cfc04 |
information about reporting bugs,
|
|
Packit |
7cfc04 |
and the latest version of this page,
|
|
Packit |
7cfc04 |
can be found at
|
|
Packit |
7cfc04 |
\%https://www.kernel.org/doc/man\-pages/.
|