SEM_TIMEDWAIT(3P)
π SEM_TIMEDWAIT(3P) π
okay bestie so this one is from the POSIX Programmer's Manual β that's the whole vibe of the page. buckle up π
πΈ PROLOG πΈ
Real talk first: this manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (so go peek at the matching Linux manual page for the tea on Linux behavior), or honestly the interface might not even be implemented on Linux at all. π
β¨ NAME β¨
sem_timedwait β lock a semaphore π
π¦ SYNOPSIS π¦
#include <semaphore.h>
#include <time.h>
int sem_timedwait(sem_t *restrict sem,
const struct timespec *restrict abstime);
π DESCRIPTION π
So sem_timedwait() shall lock the semaphore that sem points at, exactly like sem_wait() does. BUT β and this is the main character twist β if the semaphore can't be locked without waiting for another process or thread to unlock it by doing a sem_post(), then this wait shall be terminated when the specified timeout expires. β°
The timeout shall expire when the absolute time specified by abstime passes, as measured by the clock that timeouts are based on (like, when that clock's value equals or exceeds abstime), or if the absolute time in abstime has already passed at the moment of the call. no cap π«Ά
The timeout shall be based on the CLOCK_REALTIME clock. The resolution of the timeout shall be the resolution of the clock it's based on. 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. And the validity of abstime need not be checked if the semaphore can be locked immediately. periodt π
π RETURN VALUE π
sem_timedwait() shall return zero if the calling process successfully performed the semaphore lock operation on the semaphore designated by sem. π
If the call was unsuccessful? The state of the semaphore shall be unchanged, and the function shall return a value of -1 and set errno to indicate the error. π
π ERRORS π
The sem_timedwait() function shall fail if:
EINVALβ the process or thread would have blocked, and theabstimeparameter specified a nanoseconds field value less than zero or greater than or equal to 1000 million. πETIMEDOUTβ the semaphore could not be locked before the specified timeout expired. β
The sem_timedwait() function may fail if:
EDEADLKβ a deadlock condition was detected. πEINTRβ a signal interrupted this function. πEINVALβ thesemargument does not refer to a valid semaphore. π
The following sections are informative. π
π EXAMPLES π
The program below operates on an unnamed semaphore. It expects two command-line arguments. The first argument specifies a seconds value used to set an alarm timer that generates a SIGALRM signal β this handler performs a sem_post(3) to increment the semaphore that main() is waiting on using sem_timedwait(). The second command-line argument specifies the length of the timeout, in seconds, for sem_timedwait(). π§
#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);
}
π«Ά APPLICATION USAGE π«Ά
Apps using these functions may be subject to priority inversion, as discussed in the Base Definitions volume of POSIX.1β2017, Section 3.291, Priority Inversion. π
π RATIONALE π
None. (yes bestie, literally none, iconic behavior) π
π FUTURE DIRECTIONS π
None. β¨
π₯ SEE ALSO π₯
sem_post(), sem_trywait(), semctl(), semget(), semop(), time()
The Base Definitions volume of POSIX.1β2017, Section 3.291, Priority Inversion, <semaphore.h>, <time.h>
π COPYRIGHT π
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
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 .
and that's the whole page, bestie!! mwah π«Ά this one came to you courtesy of IEEE/The Open Group, dated 2017 β go off, queen πβ¨
π¦ where this page comes from
- alpine 3.22: man-pages-posix 2017a-r0 Β· custom
- alpine 3.23: man-pages-posix 2017a-r0 Β· custom
- alpine 3.24: man-pages-posix 2017a-r0 Β· custom
- arch rolling: man-pages 6.19-1
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages-posix 2017a
- opensuse-leap 16.0: man-pages-posix 2017a-160000.4.1 Β· SUSE-IEEE
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms