TIMERFD(2)
๐ NAME ๐
okay so hi bestie, welcome to timerfd(2) from the System Calls Manual โ the page for timerfd, timerfd_create, timerfd_gettime, and timerfd_settime โ literally timers with file descriptor semantics. ๐
๐ LIBRARY ๐
Standard C Library: libc, link with -lc. easy.
โจ SYNOPSIS โจ
#include <sys/timerfd.h>
int
timerfd_create(int clockid, int flags);
int
timerfd_gettime(int fd, struct itimerspec *curr_value);
int
timerfd_settime(int fd, int flags, const struct itimerspec *new_value,
struct itimerspec *old_value);
๐ง DESCRIPTION ๐ง
The timerfd system calls operate on timers, identified by special timerfd file descriptors. Theyโre analogous to timer_create(), timer_gettime(), and timer_settime() per-process timer functions, but use a timerfd descriptor in place of timerid. ๐ซถ
All timerfd descriptors possess traditional file descriptor semantics; they may be passed to other processes, preserved across fork(2), and monitored via kevent(2), poll(2), or select(2). When a timerfd descriptor is no longer needed, it may be disposed of using close(2). iconic.
๐ timerfd_create() ๐
Initialize a timerfd object and return its file descriptor. The clockid argument specifies the clock used as a timing base and may be:
CLOCK_REALTIMEโ increments as a wall clock should.CLOCK_BOOTTIMEโ also in the clock list here.CLOCK_MONOTONICโ increments monotonically in SI seconds.CLOCK_UPTIMEโ increments monotonically in SI seconds, but is paused while the system is suspended.
See clock_gettime(2) for more precise definitions.
The flags argument may contain the result of OR-ing the following values:
TFD_CLOEXECโ the newly generated file descriptor will close-on-exec.TFD_NONBLOCKโ do not block on read/write operations.
๐ธ timerfd_gettime() ๐ธ
Retrieve the current state of the timer denoted by fd. The result is stored in curr_value as a struct itimerspec. The it_value and it_interval members of curr_value represent the relative time until the next expiration and the interval reload value last set by timerfd_settime(), respectively.
๐ฆ timerfd_settime() ๐ฆ
Update the timer denoted by fd with the struct itimerspec in new_value. The it_value member of new_value should contain the amount of time before the timer expires, or zero if the timer should be disarmed. The it_interval member should contain the reload time if an interval timer is desired.
The previous timer state will be stored in old_value given old_value is not NULL.
The flags argument may contain the result of OR-ing the following values:
TFD_TIMER_ABSTIMEโ expiration will occur at the absolute time provided innew_value. Normally,new_valuerepresents a relative time compared to the timerโsclockidclock.TFD_TIMER_CANCEL_ON_SETโ ifclockidhas been set toCLOCK_REALTIMEand the realtime clock has experienced a discontinuous jump, then the timer will be canceled and the nextread(2)will fail withECANCELED.
๐จ heads up, bestie: thatTFD_TIMER_CANCEL_ON_SETbehavior is a real caveat โ timer canceled, nextread(2)fails withECANCELED, no cap.
๐๏ธ File operations have the following semantics: ๐๏ธ
๐ฅ read(2) ๐ฅ
Transfer the number of timer expirations that have occurred since the last successful read(2) or timerfd_settime() into the output buffer of size uint64_t. If the expiration counter is zero, read(2) blocks until a timer expiration occurs unless TFD_NONBLOCK is set, where EAGAIN is returned.
๐ poll(2) ๐
The file descriptor is readable when its timer expiration counter is greater than zero.
๐๏ธ ioctl(2) ๐๏ธ
FIOASYNC intโ a non-zero input will set theFASYNCflag. A zero input will clear theFASYNCflag.FIONBIO intโ a non-zero input will set theFNONBLOCKflag. A zero input will clear theFNONBLOCKflag.
๐งพ RETURN VALUES ๐งพ
The timerfd_create() system call creates a timerfd object and returns its file descriptor. If an error occurs, -1 is returned and the global variable errno is set to indicate the error.
The timerfd_gettime() and timerfd_settime() system calls return 0 on success. If an error occurs, -1 is returned and the global variable errno is set to indicate the error.
๐ ERRORS ๐
The timerfd_create() system call fails if:
[EINVAL]โ the specifiedclockidis not supported.[EINVAL]โ the providedflagsare invalid.[EMFILE]โ the per-process descriptor table is full.[ENFILE]โ the system file table is full.[ENOMEM]โ the kernel failed to allocate enough memory for the timer.
Both timerfd_gettime() and timerfd_settime() system calls fail if:
[EBADF]โ the providedfdis invalid.[EFAULT]โ the addresses provided bycurr_value,new_value, orold_valueare invalid.[EINVAL]โ the providedfdis valid, but was not generated bytimerfd_create().
The following errors only apply to timerfd_settime():
[EINVAL]โ the providedflagsare invalid.[EINVAL]โ a nanosecond field in thenew_valueargument specified a value less than zero, or greater than or equal to10^9.[ECANCELED]โ the timer was created with the clock IDCLOCK_REALTIME, was configured with theTFD_TIMER_CANCEL_ON_SETflag, and the system realtime clock experienced a discontinuous change without being read.
A read from a timerfd object fails if:
[EAGAIN]โ the timerโs expiration counter is zero and thetimerfdobject is set for non-blocking I/O.[ECANCELED]โ the timer was created with the clock IDCLOCK_REALTIME, was configured with theTFD_TIMER_CANCEL_ON_SETflag, and the system realtime clock experienced a discontinuous change.[EINVAL]โ the size of the read buffer is not large enough to hold theuint64_tsized timer expiration counter.
๐ฏ SEE ALSO ๐ฏ
eventfd(2), kqueue(2), poll(2), read(2), timer_create(2), timer_gettime(2), timer_settime(2) โ thatโs the whole squad, bestie.
๐ STANDARDS ๐
The timerfd system calls originated from Linux and are non-standard. so yeah, not a portable-everywhere situation, periodt.
๐ฐ๏ธ HISTORY ๐ฐ๏ธ
The timerfd facility was originally ported to FreeBSDโs Linux compatibility layer by Dmitry Chagin <dchagin@FreeBSD.org> in FreeBSD 12.0. It was revised and adapted to be native by Jake Freeland <jfree@FreeBSD.org> in FreeBSD 14.0.
xoxo ๐ your Unix bestie โ this page is from Debian, dated June 28, 2024, and itโs TIMERFD(2), periodt. ๐
๐ฆ where this page comes from
- freebsd 15.1: base 15.1
- ๐ the page's license: BSD-2-Clause
- Copyright (c) 2023 Jake Freeland <jfree@FreeBSD.org>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms