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 .\" FIXME Linux 3.0: timerfd_settime() adds a TFD_TIMER_CANCEL_ON_SET flag;
20 .\" This flag needs to documented.
22 .TH TIMERFD_CREATE 2 2019-03-06 Linux "Linux Programmer's Manual"
24 timerfd_create, timerfd_settime, timerfd_gettime \-
25 timers that notify via file descriptors
28 .B #include <sys/timerfd.h>
30 .BI "int timerfd_create(int " clockid ", int " flags );
32 .BI "int timerfd_settime(int " fd ", int " flags ,
33 .BI " const struct itimerspec *" new_value ,
34 .BI " struct itimerspec *" old_value );
36 .BI "int timerfd_gettime(int " fd ", struct itimerspec *" curr_value );
39 These system calls create and operate on a timer
40 that delivers timer expiration notifications via a file descriptor.
41 They provide an alternative to the use of
45 with the advantage that the file descriptor may be monitored by
51 The use of these three system calls is analogous to the use of
53 .BR timer_settime (2),
55 .BR timer_gettime (2).
56 (There is no analog of
57 .BR timer_getoverrun (2),
58 since that functionality is provided by
64 creates a new timer object,
65 and returns a file descriptor that refers to that timer.
68 argument specifies the clock that is used to mark the progress
69 of the timer, and must be one of the following:
72 A settable system-wide real-time clock.
75 A nonsettable monotonically increasing clock that measures time
76 from some unspecified point in the past that does not change
79 .BR CLOCK_BOOTTIME " (Since Linux 3.15)"
80 .\" commit 4a2378a943f09907fb1ae35c15de917f60289c14
83 this is a monotonically increasing clock.
86 clock does not measure the time while a system is suspended, the
88 clock does include the time during which the system is suspended.
89 This is useful for applications that need to be suspend-aware.
91 is not suitable for such applications, since that clock is affected
92 by discontinuous changes to the system clock.
94 .BR CLOCK_REALTIME_ALARM " (since Linux 3.11)"
95 .\" commit 11ffa9d6065f344a9bd769a2452f26f2f671e5f8
98 but will wake the system if it is suspended.
99 The caller must have the
101 capability in order to set a timer against this clock.
103 .BR CLOCK_BOOTTIME_ALARM " (since Linux 3.11)"
104 .\" commit 11ffa9d6065f344a9bd769a2452f26f2f671e5f8
107 but will wake the system if it is suspended.
108 The caller must have the
110 capability in order to set a timer against this clock.
112 The current value of each of these clocks can be retrieved using
113 .BR clock_gettime (2).
115 Starting with Linux 2.6.27, the following values may be bitwise ORed in
117 to change the behavior of
118 .BR timerfd_create ():
123 file status flag on the open file description (see
125 referred to by the new file descriptor.
126 Using this flag saves extra calls to
128 to achieve the same result.
131 Set the close-on-exec
133 flag on the new file descriptor.
134 See the description of the
138 for reasons why this may be useful.
140 In Linux versions up to and including 2.6.26,
142 must be specified as zero.
143 .SS timerfd_settime()
144 .BR timerfd_settime ()
145 arms (starts) or disarms (stops)
146 the timer referred to by the file descriptor
151 argument specifies the initial expiration and interval for the timer.
154 structure used for this argument contains two fields,
155 each of which is in turn a structure of type
161 time_t tv_sec; /* Seconds */
162 long tv_nsec; /* Nanoseconds */
166 struct timespec it_interval; /* Interval for periodic timer */
167 struct timespec it_value; /* Initial expiration */
172 .I new_value.it_value
173 specifies the initial expiration of the timer,
174 in seconds and nanoseconds.
175 Setting either field of
176 .I new_value.it_value
177 to a nonzero value arms the timer.
178 Setting both fields of
179 .I new_value.it_value
180 to zero disarms the timer.
182 Setting one or both fields of
183 .I new_value.it_interval
184 to nonzero values specifies the period, in seconds and nanoseconds,
185 for repeated timer expirations after the initial expiration.
187 .I new_value.it_interval
188 are zero, the timer expires just once, at the time specified by
189 .IR new_value.it_value .
192 the initial expiration time specified in
194 is interpreted relative to the current time
195 on the timer's clock at the time of the call (i.e.,
196 .I new_value.it_value
197 specifies a time relative to the current value of the clock specified by
199 An absolute timeout can be selected via the
205 argument is a bit mask that can include the following values:
209 .I new_value.it_value
210 as an absolute value on the timer's clock.
211 The timer will expire when the value of the timer's
212 clock reaches the value specified in
213 .IR new_value.it_value .
215 .BR TFD_TIMER_CANCEL_ON_SET
216 If this flag is specified along with
218 and the clock for this timer is
221 .BR CLOCK_REALTIME_ALARM ,
222 then mark this timer as cancelable if the real-time clock
223 undergoes a discontinuous change
224 .RB ( settimeofday (2),
225 .BR clock_settime (2),
227 When such changes occur, a current or future
229 from the file descriptor will fail with the error
234 argument is not NULL, then the
236 structure that it points to is used to return the setting of the timer
237 that was current at the time of the call;
238 see the description of
239 .BR timerfd_gettime ()
242 .SS timerfd_gettime()
243 .BR timerfd_gettime ()
248 structure that contains the current setting of the timer
249 referred to by the file descriptor
254 field returns the amount of time
255 until the timer will next expire.
256 If both fields of this structure are zero,
257 then the timer is currently disarmed.
258 This field always contains a relative value, regardless of whether the
259 .BR TFD_TIMER_ABSTIME
260 flag was specified when setting the timer.
264 field returns the interval of the timer.
265 If both fields of this structure are zero,
266 then the timer is set to expire just once, at the time specified by
267 .IR curr_value.it_value .
268 .SS Operating on a timer file descriptor
269 The file descriptor returned by
270 .BR timerfd_create ()
271 supports the following operations:
274 If the timer has already expired one or more times since
275 its settings were last modified using
276 .BR timerfd_settime (),
277 or since the last successful
279 then the buffer given to
281 returns an unsigned 8-byte integer
283 containing the number of expirations that have occurred.
284 (The returned value is in host byte order\(emthat is,
285 the native byte order for integers on the host machine.)
287 If no timer expirations have occurred at the time of the
289 then the call either blocks until the next timer expiration,
290 or fails with the error
292 if the file descriptor has been made nonblocking
304 if the size of the supplied buffer is less than 8 bytes.
306 If the associated clock is either
309 .BR CLOCK_REALTIME_ALARM ,
310 the timer is absolute
311 .RB ( TFD_TIMER_ABSTIME ),
313 .BR TFD_TIMER_CANCEL_ON_SET
314 was specified when calling
315 .BR timerfd_settime (),
320 if the real-time clock undergoes a discontinuous change.
321 (This allows the reading application to discover
322 such discontinuous changes to the clock.)
324 .BR poll "(2), " select "(2) (and similar)"
325 The file descriptor is readable
333 if one or more timer expirations have occurred.
335 The file descriptor also supports the other file-descriptor
343 The following timerfd-specific command is supported:
346 .BR TFD_IOC_SET_TICKS " (since Linux 3.17)"
347 .\" commit 5442e9fbd7c23172a1c9bc736629cd123a9923f0
348 Adjust the number of timer expirations that have occurred.
349 The argument is a pointer to a nonzero 8-byte integer
351 containing the new number of expirations.
352 Once the number is set, any waiter on the timer is woken up.
353 The only purpose of this command is to restore the expirations
354 for the purpose of checkpoint/restore.
355 This operation is available only if the kernel was configured with the
356 .BR CONFIG_CHECKPOINT_RESTORE
361 When the file descriptor is no longer required it should be closed.
362 When all file descriptors associated with the same timer object
364 the timer is disarmed and its resources are freed by the kernel.
366 .SS fork(2) semantics
369 the child inherits a copy of the file descriptor created by
370 .BR timerfd_create ().
371 The file descriptor refers to the same underlying
372 timer object as the corresponding file descriptor in the parent,
375 in the child will return information about
376 expirations of the timer.
378 .SS execve(2) semantics
379 A file descriptor created by
380 .BR timerfd_create ()
383 and continues to generate timer expirations if the timer was armed.
386 .BR timerfd_create ()
387 returns a new file descriptor.
388 On error, \-1 is returned and
390 is set to indicate the error.
392 .BR timerfd_settime ()
394 .BR timerfd_gettime ()
396 on error they return \-1, and set
398 to indicate the error.
400 .BR timerfd_create ()
401 can fail with the following errors:
414 or, in Linux 2.6.26 or earlier,
419 The per-process limit on the number of open file descriptors has been reached.
422 The system-wide limit on the total number of open files has been
426 Could not mount (internal) anonymous inode device.
429 There was insufficient kernel memory to create the timer.
431 .BR timerfd_settime ()
433 .BR timerfd_gettime ()
434 can fail with the following errors:
438 is not a valid file descriptor.
445 is not valid a pointer.
449 is not a valid timerfd file descriptor.
451 .BR timerfd_settime ()
452 can also fail with the following errors:
456 is not properly initialized (one of the
458 falls outside the range zero to 999,999,999).
461 .\" This case only checked since 2.6.29, and 2.2.2[78].some-stable-version.
462 .\" In older kernel versions, no check was made for invalid flags.
466 These system calls are available on Linux since kernel 2.6.25.
467 Library support is provided by glibc since version 2.8.
469 These system calls are Linux-specific.
473 .BR timerfd_create ()
474 supports fewer types of clock IDs than
475 .BR timer_create (2).
477 The following program creates a timer and then monitors its progress.
478 The program accepts up to three command-line arguments.
479 The first argument specifies the number of seconds for
480 the initial expiration of the timer.
481 The second argument specifies the interval for the timer, in seconds.
482 The third argument specifies the number of times the program should
483 allow the timer to expire before terminating.
484 The second and third command-line arguments are optional.
486 The following shell session demonstrates the use of the program:
490 .RB "$" " a.out 3 1 100"
492 3.000: read: 1; total=1
493 4.000: read: 1; total=2
494 .BR "^Z " " # type control-Z to suspend the program"
495 [1]+ Stopped ./timerfd3_demo 3 1 100
496 .RB "$ " "fg" " # Resume execution after a few seconds"
498 9.660: read: 5; total=7
499 10.000: read: 1; total=8
500 11.000: read: 1; total=9
501 .BR "^C " " # type control-C to suspend the program"
507 .\" The commented out code here is what we currently need until
508 .\" the required stuff is in glibc
511 .\"/* Link with -lrt */
512 .\"#define _GNU_SOURCE
513 .\"#include <sys/syscall.h>
514 .\"#include <unistd.h>
516 .\"#if defined(__i386__)
517 .\"#define __NR_timerfd_create 322
518 .\"#define __NR_timerfd_settime 325
519 .\"#define __NR_timerfd_gettime 326
523 .\"timerfd_create(int clockid, int flags)
525 .\" return syscall(__NR_timerfd_create, clockid, flags);
529 .\"timerfd_settime(int fd, int flags, struct itimerspec *new_value,
530 .\" struct itimerspec *curr_value)
532 .\" return syscall(__NR_timerfd_settime, fd, flags, new_value,
537 .\"timerfd_gettime(int fd, struct itimerspec *curr_value)
539 .\" return syscall(__NR_timerfd_gettime, fd, curr_value);
542 .\"#define TFD_TIMER_ABSTIME (1 << 0)
544 .\"////////////////////////////////////////////////////////////
545 #include <sys/timerfd.h>
550 #include <stdint.h> /* Definition of uint64_t */
552 #define handle_error(msg) \e
553 do { perror(msg); exit(EXIT_FAILURE); } while (0)
556 print_elapsed_time(void)
558 static struct timespec start;
559 struct timespec curr;
560 static int first_call = 1;
565 if (clock_gettime(CLOCK_MONOTONIC, &start) == \-1)
566 handle_error("clock_gettime");
569 if (clock_gettime(CLOCK_MONOTONIC, &curr) == \-1)
570 handle_error("clock_gettime");
572 secs = curr.tv_sec \- start.tv_sec;
573 nsecs = curr.tv_nsec \- start.tv_nsec;
578 printf("%d.%03d: ", secs, (nsecs + 500000) / 1000000);
582 main(int argc, char *argv[])
584 struct itimerspec new_value;
587 uint64_t exp, tot_exp;
590 if ((argc != 2) && (argc != 4)) {
591 fprintf(stderr, "%s init\-secs [interval\-secs max\-exp]\en",
596 if (clock_gettime(CLOCK_REALTIME, &now) == \-1)
597 handle_error("clock_gettime");
599 /* Create a CLOCK_REALTIME absolute timer with initial
600 expiration and interval as specified in command line */
602 new_value.it_value.tv_sec = now.tv_sec + atoi(argv[1]);
603 new_value.it_value.tv_nsec = now.tv_nsec;
605 new_value.it_interval.tv_sec = 0;
608 new_value.it_interval.tv_sec = atoi(argv[2]);
609 max_exp = atoi(argv[3]);
611 new_value.it_interval.tv_nsec = 0;
613 fd = timerfd_create(CLOCK_REALTIME, 0);
615 handle_error("timerfd_create");
617 if (timerfd_settime(fd, TFD_TIMER_ABSTIME, &new_value, NULL) == \-1)
618 handle_error("timerfd_settime");
620 print_elapsed_time();
621 printf("timer started\en");
623 for (tot_exp = 0; tot_exp < max_exp;) {
624 s = read(fd, &exp, sizeof(uint64_t));
625 if (s != sizeof(uint64_t))
626 handle_error("read");
629 print_elapsed_time();
630 printf("read: %llu; total=%llu\en",
631 (unsigned long long) exp,
632 (unsigned long long) tot_exp);
645 .BR timer_create (2),
646 .BR timer_gettime (2),
647 .BR timer_settime (2),