1 .\" Copyright (C) 2008 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" This program is free software; you can redistribute it and/or modify
4 .\" it under the terms of the GNU General Public License as published by
5 .\" the Free Software Foundation; either version 2 of the License, or
6 .\" (at your option) any later version.
8 .\" This program is distributed in the hope that it will be useful,
9 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
10 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 .\" GNU General Public License for more details.
13 .\" You should have received a copy of the GNU General Public
14 .\" License along with this manual; if not, see
15 .\" <http://www.gnu.org/licenses/>.
17 .\" FIXME: Linux 3.0: timerfd_settime() adds a TFD_TIMER_CANCEL_ON_SET flag.
19 .TH TIMERFD_CREATE 2 2011-09-14 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 either
71 is a settable system-wide clock.
73 is a nonsettable clock that is not affected
74 by discontinuous changes in the system clock
75 (e.g., manual changes to system time).
76 The current value of each of these clocks can be retrieved using
77 .BR clock_gettime (2).
79 Starting with Linux 2.6.27, the following values may be bitwise ORed in
81 to change the behavior of
82 .BR timerfd_create ():
87 file status flag on the new open file description.
88 Using this flag saves extra calls to
90 to achieve the same result.
95 flag on the new file descriptor.
96 See the description of the
100 for reasons why this may be useful.
102 In Linux versions up to and including 2.6.26,
104 must be specified as zero.
105 .SS timerfd_settime()
106 .BR timerfd_settime ()
107 arms (starts) or disarms (stops)
108 the timer referred to by the file descriptor
113 argument specifies the initial expiration and interval for the timer.
116 structure used for this argument contains two fields,
117 each of which is in turn a structure of type
123 time_t tv_sec; /* Seconds */
124 long tv_nsec; /* Nanoseconds */
128 struct timespec it_interval; /* Interval for periodic timer */
129 struct timespec it_value; /* Initial expiration */
134 .I new_value.it_value
135 specifies the initial expiration of the timer,
136 in seconds and nanoseconds.
137 Setting either field of
138 .I new_value.it_value
139 to a nonzero value arms the timer.
140 Setting both fields of
141 .I new_value.it_value
142 to zero disarms the timer.
144 Setting one or both fields of
145 .I new_value.it_interval
146 to nonzero values specifies the period, in seconds and nanoseconds,
147 for repeated timer expirations after the initial expiration.
149 .I new_value.it_interval
150 are zero, the timer expires just once, at the time specified by
151 .IR new_value.it_value .
155 argument is either 0, to start a relative timer
156 .RI ( new_value.it_value
157 specifies a time relative to the current value of the clock specified by
160 .BR TFD_TIMER_ABSTIME ,
161 to start an absolute timer
162 .RI ( new_value.it_value
163 specifies an absolute time for the clock specified by
165 that is, the timer will expire when the value of that
166 clock reaches the value specified in
167 .IR new_value.it_value ).
171 argument is not NULL, then the
173 structure that it points to is used to return the setting of the timer
174 that was current at the time of the call;
175 see the description of
176 .BR timerfd_gettime ()
179 .SS timerfd_gettime()
180 .BR timerfd_gettime ()
185 structure that contains the current setting of the timer
186 referred to by the file descriptor
191 field returns the amount of time
192 until the timer will next expire.
193 If both fields of this structure are zero,
194 then the timer is currently disarmed.
195 This field always contains a relative value, regardless of whether the
196 .BR TFD_TIMER_ABSTIME
197 flag was specified when setting the timer.
201 field returns the interval of the timer.
202 If both fields of this structure are zero,
203 then the timer is set to expire just once, at the time specified by
204 .IR curr_value.it_value .
205 .SS Operating on a timer file descriptor
206 The file descriptor returned by
207 .BR timerfd_create ()
208 supports the following operations:
211 If the timer has already expired one or more times since
212 its settings were last modified using
213 .BR timerfd_settime (),
214 or since the last successful
216 then the buffer given to
218 returns an unsigned 8-byte integer
220 containing the number of expirations that have occurred.
221 (The returned value is in host byte order,
222 i.e., the native byte order for integers on the host machine.)
224 If no timer expirations have occurred at the time of the
226 then the call either blocks until the next timer expiration,
227 or fails with the error
229 if the file descriptor has been made nonblocking
239 will fail with the error
241 if the size of the supplied buffer is less than 8 bytes.
243 .BR poll "(2), " select "(2) (and similar)"
244 The file descriptor is readable
252 if one or more timer expirations have occurred.
254 The file descriptor also supports the other file-descriptor
262 When the file descriptor is no longer required it should be closed.
263 When all file descriptors associated with the same timer object
265 the timer is disarmed and its resources are freed by the kernel.
267 .SS fork(2) semantics
270 the child inherits a copy of the file descriptor created by
271 .BR timerfd_create ().
272 The file descriptor refers to the same underlying
273 timer object as the corresponding file descriptor in the parent,
276 in the child will return information about
277 expirations of the timer.
279 .SS execve(2) semantics
280 A file descriptor created by
281 .BR timerfd_create ()
284 and continues to generate timer expirations if the timer was armed.
287 .BR timerfd_create ()
288 returns a new file descriptor.
289 On error, \-1 is returned and
291 is set to indicate the error.
293 .BR timerfd_settime ()
295 .BR timerfd_gettime ()
297 on error they return \-1, and set
299 to indicate the error.
301 .BR timerfd_create ()
302 can fail with the following errors:
315 or, in Linux 2.6.26 or earlier,
320 The per-process limit of open file descriptors has been reached.
323 The system-wide limit on the total number of open files has been
327 Could not mount (internal) anonymous inode device.
330 There was insufficient kernel memory to create the timer.
332 .BR timerfd_settime ()
334 .BR timerfd_gettime ()
335 can fail with the following errors:
339 is not a valid file descriptor.
346 is not valid a pointer.
350 is not a valid timerfd file descriptor.
352 .BR timerfd_settime ()
353 can also fail with the following errors:
357 is not properly initialized (one of the
359 falls outside the range zero to 999,999,999).
362 .\" This case only checked since 2.6.29, and 2.2.2[78].some-stable-version.
363 .\" In older kernel versions, no check was made for invalid flags.
367 These system calls are available on Linux since kernel 2.6.25.
368 Library support is provided by glibc since version 2.8.
370 These system calls are Linux-specific.
374 .BR timerfd_create ()
375 supports fewer types of clock IDs than
376 .BR timer_create (2).
378 The following program creates a timer and then monitors its progress.
379 The program accepts up to three command-line arguments.
380 The first argument specifies the number of seconds for
381 the initial expiration of the timer.
382 The second argument specifies the interval for the timer, in seconds.
383 The third argument specifies the number of times the program should
384 allow the timer to expire before terminating.
385 The second and third command-line arguments are optional.
387 The following shell session demonstrates the use of the program:
391 .RB "$" " a.out 3 1 100"
393 3.000: read: 1; total=1
394 4.000: read: 1; total=2
395 .BR "^Z " " # type control-Z to suspend the program"
396 [1]+ Stopped ./timerfd3_demo 3 1 100
397 .RB "$ " "fg" " # Resume execution after a few seconds"
399 9.660: read: 5; total=7
400 10.000: read: 1; total=8
401 11.000: read: 1; total=9
402 .BR "^C " " # type control-C to suspend the program"
408 .\" The commented out code here is what we currently need until
409 .\" the required stuff is in glibc
412 .\"/* Link with -lrt */
413 .\"#define _GNU_SOURCE
414 .\"#include <sys/syscall.h>
415 .\"#include <unistd.h>
417 .\"#if defined(__i386__)
418 .\"#define __NR_timerfd_create 322
419 .\"#define __NR_timerfd_settime 325
420 .\"#define __NR_timerfd_gettime 326
424 .\"timerfd_create(int clockid, int flags)
426 .\" return syscall(__NR_timerfd_create, clockid, flags);
430 .\"timerfd_settime(int fd, int flags, struct itimerspec *new_value,
431 .\" struct itimerspec *curr_value)
433 .\" return syscall(__NR_timerfd_settime, fd, flags, new_value,
438 .\"timerfd_gettime(int fd, struct itimerspec *curr_value)
440 .\" return syscall(__NR_timerfd_gettime, fd, curr_value);
443 .\"#define TFD_TIMER_ABSTIME (1 << 0)
445 .\"////////////////////////////////////////////////////////////
446 #include <sys/timerfd.h>
451 #include <stdint.h> /* Definition of uint64_t */
453 #define handle_error(msg) \\
454 do { perror(msg); exit(EXIT_FAILURE); } while (0)
457 print_elapsed_time(void)
459 static struct timespec start;
460 struct timespec curr;
461 static int first_call = 1;
466 if (clock_gettime(CLOCK_MONOTONIC, &start) == \-1)
467 handle_error("clock_gettime");
470 if (clock_gettime(CLOCK_MONOTONIC, &curr) == \-1)
471 handle_error("clock_gettime");
473 secs = curr.tv_sec \- start.tv_sec;
474 nsecs = curr.tv_nsec \- start.tv_nsec;
479 printf("%d.%03d: ", secs, (nsecs + 500000) / 1000000);
483 main(int argc, char *argv[])
485 struct itimerspec new_value;
488 uint64_t exp, tot_exp;
491 if ((argc != 2) && (argc != 4)) {
492 fprintf(stderr, "%s init\-secs [interval\-secs max\-exp]\\n",
497 if (clock_gettime(CLOCK_REALTIME, &now) == \-1)
498 handle_error("clock_gettime");
500 /* Create a CLOCK_REALTIME absolute timer with initial
501 expiration and interval as specified in command line */
503 new_value.it_value.tv_sec = now.tv_sec + atoi(argv[1]);
504 new_value.it_value.tv_nsec = now.tv_nsec;
506 new_value.it_interval.tv_sec = 0;
509 new_value.it_interval.tv_sec = atoi(argv[2]);
510 max_exp = atoi(argv[3]);
512 new_value.it_interval.tv_nsec = 0;
514 fd = timerfd_create(CLOCK_REALTIME, 0);
516 handle_error("timerfd_create");
518 if (timerfd_settime(fd, TFD_TIMER_ABSTIME, &new_value, NULL) == \-1)
519 handle_error("timerfd_settime");
521 print_elapsed_time();
522 printf("timer started\\n");
524 for (tot_exp = 0; tot_exp < max_exp;) {
525 s = read(fd, &exp, sizeof(uint64_t));
526 if (s != sizeof(uint64_t))
527 handle_error("read");
530 print_elapsed_time();
531 printf("read: %llu; total=%llu\\n",
532 (unsigned long long) exp,
533 (unsigned long long) tot_exp);
546 .BR timer_create (2),
547 .BR timer_gettime (2),
548 .BR timer_settime (2),