#include <semaphore.h> #include <time.h> int sem_timedwait(sem_t *restrict sem, const struct timespec *restrict abstime);
The timeout shall expire when the absolute time specified by abstime passes, as measured by the clock on which timeouts are based (that is, when the value of that clock equals or exceeds abstime), or if the absolute time specified by abstime has already been passed at the time of the call.
The timeout shall be based on the CLOCK_REALTIME clock. The resolution of the timeout shall be the resolution of the clock on which it is based. The timespec data type is defined as a structure in the <time.h> header.
Under no circumstance shall the function fail with a timeout if the semaphore can be locked immediately. The validity of the abstime need not be checked if the semaphore can be locked immediately.
The sem_timedwait() function may fail if:
The following sections are informative.
#include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <semaphore.h> #include <time.h> #include <assert.h> #include <errno.h> #include <signal.h> sem_t sem; static void handler(int sig) { int sav_errno = errno; static const char info_msg[] = "sem_post() from handler\n"; write(STDOUT_FILENO, info_msg, sizeof info_msg - 1); if (sem_post(&sem) == -1) { static const char err_msg[] = "sem_post() failed\n"; write(STDERR_FILENO, err_msg, sizeof err_msg - 1); _exit(EXIT_FAILURE); } errno = sav_errno; } int main(int argc, char *argv[]) { struct sigaction sa; struct timespec ts; int s; if (argc != 3) { fprintf(stderr, "Usage: %s <alarm-secs> <wait-secs>\n", argv[0]); exit(EXIT_FAILURE); } if (sem_init(&sem, 0, 0) == -1) { perror("sem_init"); exit(EXIT_FAILURE); } /* Establish SIGALRM handler; set alarm timer using argv[1] */ sa.sa_handler = handler; sigemptyset(&sa.sa_mask); sa.sa_flags = 0; if (sigaction(SIGALRM, &sa, NULL) == -1) { perror("sigaction"); exit(EXIT_FAILURE); } alarm(atoi(argv[1])); /* Calculate relative interval as current time plus number of seconds given argv[2] */ if (clock_gettime(CLOCK_REALTIME, &ts) == -1) { perror("clock_gettime"); exit(EXIT_FAILURE); } ts.tv_sec += atoi(argv[2]); printf("main() about to call sem_timedwait()\n"); while ((s = sem_timedwait(&sem, &ts)) == -1 && errno == EINTR) continue; /* Restart if interrupted by handler */ /* Check what happened */ if (s == -1) { if (errno == ETIMEDOUT) printf("sem_timedwait() timed out\n"); else perror("sem_timedwait"); } else printf("sem_timedwait() succeeded\n"); exit((s == 0) ? EXIT_SUCCESS : EXIT_FAILURE); }
The Base Definitions volume of POSIX.1-2017, Section 3.291, Priority Inversion, <semaphore.h>, <time.h>
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .