1 .\" Copyright (C) 2008 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" %%%LICENSE_START(GPLv2+_SW_3_PARA)
4 .\" This program is free software; you can redistribute it and/or modify
5 .\" it under the terms of the GNU General Public License as published by
6 .\" the Free Software Foundation; either version 2 of the License, or
7 .\" (at your option) any later version.
9 .\" This program is distributed in the hope that it will be useful,
10 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
11 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 .\" GNU General Public License for more details.
14 .\" You should have received a copy of the GNU General Public
15 .\" License along with this manual; if not, see
16 .\" <http://www.gnu.org/licenses/>.
19 .TH TIMERFD_CREATE 2 2020-04-11 Linux "Linux Programmer's Manual"
21 timerfd_create, timerfd_settime, timerfd_gettime \-
22 timers that notify via file descriptors
25 .B #include <sys/timerfd.h>
27 .BI "int timerfd_create(int " clockid ", int " flags );
29 .BI "int timerfd_settime(int " fd ", int " flags ,
30 .BI " const struct itimerspec *" new_value ,
31 .BI " struct itimerspec *" old_value );
33 .BI "int timerfd_gettime(int " fd ", struct itimerspec *" curr_value );
36 These system calls create and operate on a timer
37 that delivers timer expiration notifications via a file descriptor.
38 They provide an alternative to the use of
42 with the advantage that the file descriptor may be monitored by
48 The use of these three system calls is analogous to the use of
50 .BR timer_settime (2),
52 .BR timer_gettime (2).
53 (There is no analog of
54 .BR timer_getoverrun (2),
55 since that functionality is provided by
61 creates a new timer object,
62 and returns a file descriptor that refers to that timer.
65 argument specifies the clock that is used to mark the progress
66 of the timer, and must be one of the following:
69 A settable system-wide real-time clock.
72 A nonsettable monotonically increasing clock that measures time
73 from some unspecified point in the past that does not change
76 .BR CLOCK_BOOTTIME " (Since Linux 3.15)"
77 .\" commit 4a2378a943f09907fb1ae35c15de917f60289c14
80 this is a monotonically increasing clock.
83 clock does not measure the time while a system is suspended, the
85 clock does include the time during which the system is suspended.
86 This is useful for applications that need to be suspend-aware.
88 is not suitable for such applications, since that clock is affected
89 by discontinuous changes to the system clock.
91 .BR CLOCK_REALTIME_ALARM " (since Linux 3.11)"
92 .\" commit 11ffa9d6065f344a9bd769a2452f26f2f671e5f8
95 but will wake the system if it is suspended.
96 The caller must have the
98 capability in order to set a timer against this clock.
100 .BR CLOCK_BOOTTIME_ALARM " (since Linux 3.11)"
101 .\" commit 11ffa9d6065f344a9bd769a2452f26f2f671e5f8
104 but will wake the system if it is suspended.
105 The caller must have the
107 capability in order to set a timer against this clock.
111 for some further details on the above clocks.
113 The current value of each of these clocks can be retrieved using
114 .BR clock_gettime (2).
116 Starting with Linux 2.6.27, the following values may be bitwise ORed in
118 to change the behavior of
119 .BR timerfd_create ():
124 file status flag on the open file description (see
126 referred to by the new file descriptor.
127 Using this flag saves extra calls to
129 to achieve the same result.
132 Set the close-on-exec
134 flag on the new file descriptor.
135 See the description of the
139 for reasons why this may be useful.
141 In Linux versions up to and including 2.6.26,
143 must be specified as zero.
144 .SS timerfd_settime()
145 .BR timerfd_settime ()
146 arms (starts) or disarms (stops)
147 the timer referred to by the file descriptor
152 argument specifies the initial expiration and interval for the timer.
155 structure used for this argument contains two fields,
156 each of which is in turn a structure of type
162 time_t tv_sec; /* Seconds */
163 long tv_nsec; /* Nanoseconds */
167 struct timespec it_interval; /* Interval for periodic timer */
168 struct timespec it_value; /* Initial expiration */
173 .I new_value.it_value
174 specifies the initial expiration of the timer,
175 in seconds and nanoseconds.
176 Setting either field of
177 .I new_value.it_value
178 to a nonzero value arms the timer.
179 Setting both fields of
180 .I new_value.it_value
181 to zero disarms the timer.
183 Setting one or both fields of
184 .I new_value.it_interval
185 to nonzero values specifies the period, in seconds and nanoseconds,
186 for repeated timer expirations after the initial expiration.
188 .I new_value.it_interval
189 are zero, the timer expires just once, at the time specified by
190 .IR new_value.it_value .
193 the initial expiration time specified in
195 is interpreted relative to the current time
196 on the timer's clock at the time of the call (i.e.,
197 .I new_value.it_value
198 specifies a time relative to the current value of the clock specified by
200 An absolute timeout can be selected via the
206 argument is a bit mask that can include the following values:
210 .I new_value.it_value
211 as an absolute value on the timer's clock.
212 The timer will expire when the value of the timer's
213 clock reaches the value specified in
214 .IR new_value.it_value .
216 .BR TFD_TIMER_CANCEL_ON_SET
217 If this flag is specified along with
219 and the clock for this timer is
222 .BR CLOCK_REALTIME_ALARM ,
223 then mark this timer as cancelable if the real-time clock
224 undergoes a discontinuous change
225 .RB ( settimeofday (2),
226 .BR clock_settime (2),
228 When such changes occur, a current or future
230 from the file descriptor will fail with the error
235 argument is not NULL, then the
237 structure that it points to is used to return the setting of the timer
238 that was current at the time of the call;
239 see the description of
240 .BR timerfd_gettime ()
243 .SS timerfd_gettime()
244 .BR timerfd_gettime ()
249 structure that contains the current setting of the timer
250 referred to by the file descriptor
255 field returns the amount of time
256 until the timer will next expire.
257 If both fields of this structure are zero,
258 then the timer is currently disarmed.
259 This field always contains a relative value, regardless of whether the
260 .BR TFD_TIMER_ABSTIME
261 flag was specified when setting the timer.
265 field returns the interval of the timer.
266 If both fields of this structure are zero,
267 then the timer is set to expire just once, at the time specified by
268 .IR curr_value.it_value .
269 .SS Operating on a timer file descriptor
270 The file descriptor returned by
271 .BR timerfd_create ()
272 supports the following additional operations:
275 If the timer has already expired one or more times since
276 its settings were last modified using
277 .BR timerfd_settime (),
278 or since the last successful
280 then the buffer given to
282 returns an unsigned 8-byte integer
284 containing the number of expirations that have occurred.
285 (The returned value is in host byte order\(emthat is,
286 the native byte order for integers on the host machine.)
288 If no timer expirations have occurred at the time of the
290 then the call either blocks until the next timer expiration,
291 or fails with the error
293 if the file descriptor has been made nonblocking
305 if the size of the supplied buffer is less than 8 bytes.
307 If the associated clock is either
310 .BR CLOCK_REALTIME_ALARM ,
311 the timer is absolute
312 .RB ( TFD_TIMER_ABSTIME ),
314 .BR TFD_TIMER_CANCEL_ON_SET
315 was specified when calling
316 .BR timerfd_settime (),
321 if the real-time clock undergoes a discontinuous change.
322 (This allows the reading application to discover
323 such discontinuous changes to the clock.)
325 If the associated clock is either
328 .BR CLOCK_REALTIME_ALARM ,
329 the timer is absolute
330 .RB ( TFD_TIMER_ABSTIME ),
332 .BR TFD_TIMER_CANCEL_ON_SET
335 specified when calling
336 .BR timerfd_settime (),
337 then a discontinuous negative change to the clock (e.g.,
338 .BR clock_settime (2))
341 to unblock, but return a value of 0 (i.e., no bytes read),
342 if the clock change occurs after the time expired,
345 on the file descriptor.
347 .BR poll "(2), " select "(2) (and similar)"
348 The file descriptor is readable
356 if one or more timer expirations have occurred.
358 The file descriptor also supports the other file-descriptor
366 The following timerfd-specific command is supported:
369 .BR TFD_IOC_SET_TICKS " (since Linux 3.17)"
370 .\" commit 5442e9fbd7c23172a1c9bc736629cd123a9923f0
371 Adjust the number of timer expirations that have occurred.
372 The argument is a pointer to a nonzero 8-byte integer
374 containing the new number of expirations.
375 Once the number is set, any waiter on the timer is woken up.
376 The only purpose of this command is to restore the expirations
377 for the purpose of checkpoint/restore.
378 This operation is available only if the kernel was configured with the
379 .BR CONFIG_CHECKPOINT_RESTORE
384 When the file descriptor is no longer required it should be closed.
385 When all file descriptors associated with the same timer object
387 the timer is disarmed and its resources are freed by the kernel.
389 .SS fork(2) semantics
392 the child inherits a copy of the file descriptor created by
393 .BR timerfd_create ().
394 The file descriptor refers to the same underlying
395 timer object as the corresponding file descriptor in the parent,
398 in the child will return information about
399 expirations of the timer.
401 .SS execve(2) semantics
402 A file descriptor created by
403 .BR timerfd_create ()
406 and continues to generate timer expirations if the timer was armed.
409 .BR timerfd_create ()
410 returns a new file descriptor.
411 On error, \-1 is returned and
413 is set to indicate the error.
415 .BR timerfd_settime ()
417 .BR timerfd_gettime ()
419 on error they return \-1, and set
421 to indicate the error.
423 .BR timerfd_create ()
424 can fail with the following errors:
434 or, in Linux 2.6.26 or earlier,
439 The per-process limit on the number of open file descriptors has been reached.
442 The system-wide limit on the total number of open files has been
446 Could not mount (internal) anonymous inode device.
449 There was insufficient kernel memory to create the timer.
454 .BR CLOCK_REALTIME_ALARM
456 ,BR CLOCK_BOOTTIME_ALARM
457 but the caller did not have the
461 .BR timerfd_settime ()
463 .BR timerfd_gettime ()
464 can fail with the following errors:
468 is not a valid file descriptor.
475 is not valid a pointer.
479 is not a valid timerfd file descriptor.
481 .BR timerfd_settime ()
482 can also fail with the following errors:
489 is not properly initialized (one of the
491 falls outside the range zero to 999,999,999).
494 .\" This case only checked since 2.6.29, and 2.2.2[78].some-stable-version.
495 .\" In older kernel versions, no check was made for invalid flags.
499 These system calls are available on Linux since kernel 2.6.25.
500 Library support is provided by glibc since version 2.8.
502 These system calls are Linux-specific.
504 Suppose the following scenario for
507 .BR CLOCK_REALTIME_ALARM
508 timer that was created with
509 .BR timerfd_create ():
511 The timer has been started
512 .RB ( timerfd_settime ())
514 .BR TFD_TIMER_ABSTIME
516 .BR TFD_TIMER_CANCEL_ON_SET
519 A discontinuous change (e.g.
520 .BR settimeofday (2))
521 is subsequently made to the
525 the caller once more calls
526 .BR timerfd_settime ()
527 to rearm the timer (without first doing a
529 on the file descriptor).
531 In this case the following occurs:
534 .BR timerfd_settime ()
539 (This enables the caller to know that the previous timer was affected
540 by a discontinuous change to the clock.)
543 .I "is successfully rearmed"
544 with the settings provided in the second
545 .BR timerfd_settime ()
547 (This was probably an implementation accident, but won't be fixed now,
548 in case there are applications that depend on this behaviour.)
552 .BR timerfd_create ()
553 supports fewer types of clock IDs than
554 .BR timer_create (2).
556 The following program creates a timer and then monitors its progress.
557 The program accepts up to three command-line arguments.
558 The first argument specifies the number of seconds for
559 the initial expiration of the timer.
560 The second argument specifies the interval for the timer, in seconds.
561 The third argument specifies the number of times the program should
562 allow the timer to expire before terminating.
563 The second and third command-line arguments are optional.
565 The following shell session demonstrates the use of the program:
569 .RB "$" " a.out 3 1 100"
571 3.000: read: 1; total=1
572 4.000: read: 1; total=2
573 .BR "^Z " " # type control-Z to suspend the program"
574 [1]+ Stopped ./timerfd3_demo 3 1 100
575 .RB "$ " "fg" " # Resume execution after a few seconds"
577 9.660: read: 5; total=7
578 10.000: read: 1; total=8
579 11.000: read: 1; total=9
580 .BR "^C " " # type control-C to suspend the program"
586 .\" The commented out code here is what we currently need until
587 .\" the required stuff is in glibc
590 .\"/* Link with -lrt */
591 .\"#define _GNU_SOURCE
592 .\"#include <sys/syscall.h>
593 .\"#include <unistd.h>
595 .\"#if defined(__i386__)
596 .\"#define __NR_timerfd_create 322
597 .\"#define __NR_timerfd_settime 325
598 .\"#define __NR_timerfd_gettime 326
602 .\"timerfd_create(int clockid, int flags)
604 .\" return syscall(__NR_timerfd_create, clockid, flags);
608 .\"timerfd_settime(int fd, int flags, struct itimerspec *new_value,
609 .\" struct itimerspec *curr_value)
611 .\" return syscall(__NR_timerfd_settime, fd, flags, new_value,
616 .\"timerfd_gettime(int fd, struct itimerspec *curr_value)
618 .\" return syscall(__NR_timerfd_gettime, fd, curr_value);
621 .\"#define TFD_TIMER_ABSTIME (1 << 0)
623 .\"////////////////////////////////////////////////////////////
624 #include <sys/timerfd.h>
629 #include <stdint.h> /* Definition of uint64_t */
631 #define handle_error(msg) \e
632 do { perror(msg); exit(EXIT_FAILURE); } while (0)
635 print_elapsed_time(void)
637 static struct timespec start;
638 struct timespec curr;
639 static int first_call = 1;
644 if (clock_gettime(CLOCK_MONOTONIC, &start) == \-1)
645 handle_error("clock_gettime");
648 if (clock_gettime(CLOCK_MONOTONIC, &curr) == \-1)
649 handle_error("clock_gettime");
651 secs = curr.tv_sec \- start.tv_sec;
652 nsecs = curr.tv_nsec \- start.tv_nsec;
657 printf("%d.%03d: ", secs, (nsecs + 500000) / 1000000);
661 main(int argc, char *argv[])
663 struct itimerspec new_value;
666 uint64_t exp, tot_exp;
669 if ((argc != 2) && (argc != 4)) {
670 fprintf(stderr, "%s init\-secs [interval\-secs max\-exp]\en",
675 if (clock_gettime(CLOCK_REALTIME, &now) == \-1)
676 handle_error("clock_gettime");
678 /* Create a CLOCK_REALTIME absolute timer with initial
679 expiration and interval as specified in command line */
681 new_value.it_value.tv_sec = now.tv_sec + atoi(argv[1]);
682 new_value.it_value.tv_nsec = now.tv_nsec;
684 new_value.it_interval.tv_sec = 0;
687 new_value.it_interval.tv_sec = atoi(argv[2]);
688 max_exp = atoi(argv[3]);
690 new_value.it_interval.tv_nsec = 0;
692 fd = timerfd_create(CLOCK_REALTIME, 0);
694 handle_error("timerfd_create");
696 if (timerfd_settime(fd, TFD_TIMER_ABSTIME, &new_value, NULL) == \-1)
697 handle_error("timerfd_settime");
699 print_elapsed_time();
700 printf("timer started\en");
702 for (tot_exp = 0; tot_exp < max_exp;) {
703 s = read(fd, &exp, sizeof(uint64_t));
704 if (s != sizeof(uint64_t))
705 handle_error("read");
708 print_elapsed_time();
709 printf("read: %llu; total=%llu\en",
710 (unsigned long long) exp,
711 (unsigned long long) tot_exp);
724 .BR timer_create (2),
725 .BR timer_gettime (2),
726 .BR timer_settime (2),