]>
Commit | Line | Data |
---|---|---|
45b81c9c MK |
1 | .\" Copyright (C) 2008 Michael Kerrisk <mtk.manpages@gmail.com> |
2 | .\" | |
f0008367 | 3 | .\" %%%LICENSE_START(GPLv2+_SW_3_PARA) |
45b81c9c MK |
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. | |
8 | .\" | |
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. | |
13 | .\" | |
68fa4398 MK |
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/>. | |
8ff7380d | 17 | .\" %%%LICENSE_END |
45b81c9c | 18 | .\" |
bea08fec MK |
19 | .\" FIXME Linux 3.0: timerfd_settime() adds a TFD_TIMER_CANCEL_ON_SET flag; |
20 | .\" This flag needs to documented. | |
d09de287 | 21 | .\" |
35deeb87 | 22 | .TH TIMERFD_CREATE 2 2016-12-12 Linux "Linux Programmer's Manual" |
45b81c9c MK |
23 | .SH NAME |
24 | timerfd_create, timerfd_settime, timerfd_gettime \- | |
25 | timers that notify via file descriptors | |
26 | .SH SYNOPSIS | |
45b81c9c MK |
27 | .nf |
28 | .B #include <sys/timerfd.h> | |
68e4db0a | 29 | .PP |
45b81c9c | 30 | .BI "int timerfd_create(int " clockid ", int " flags ); |
68e4db0a | 31 | .PP |
45b81c9c MK |
32 | .BI "int timerfd_settime(int " fd ", int " flags , |
33 | .BI " const struct itimerspec *" new_value , | |
5c902597 | 34 | .BI " struct itimerspec *" old_value ); |
68e4db0a | 35 | .PP |
45b81c9c MK |
36 | .BI "int timerfd_gettime(int " fd ", struct itimerspec *" curr_value ); |
37 | .fi | |
38 | .SH DESCRIPTION | |
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 | |
42 | .BR setitimer (2) | |
43 | or | |
804f03e6 | 44 | .BR timer_create (2), |
45b81c9c MK |
45 | with the advantage that the file descriptor may be monitored by |
46 | .BR select (2), | |
47 | .BR poll (2), | |
48 | and | |
49 | .BR epoll (7). | |
50 | ||
51 | The use of these three system calls is analogous to the use of | |
804f03e6 MK |
52 | .BR timer_create (2), |
53 | .BR timer_settime (2), | |
45b81c9c | 54 | and |
804f03e6 | 55 | .BR timer_gettime (2). |
45b81c9c | 56 | (There is no analog of |
804f03e6 | 57 | .BR timer_getoverrun (2), |
45b81c9c MK |
58 | since that functionality is provided by |
59 | .BR read (2), | |
60 | as described below.) | |
61 | .\" | |
62 | .SS timerfd_create() | |
63 | .BR timerfd_create () | |
64 | creates a new timer object, | |
65 | and returns a file descriptor that refers to that timer. | |
66 | The | |
67 | .I clockid | |
68 | argument specifies the clock that is used to mark the progress | |
4b76fffb | 69 | of the timer, and must one of the following: |
4b76fffb | 70 | .TP |
45b81c9c | 71 | .B CLOCK_REALTIME |
4b76fffb MK |
72 | A settable system-wide real-time clock. |
73 | .TP | |
45b81c9c | 74 | .B CLOCK_MONOTONIC |
4b76fffb MK |
75 | A nonsettable monotonically increasing clock that measures time |
76 | from some unspecified point in the past that does not change | |
77 | after system startup. | |
4c747119 MK |
78 | .TP |
79 | .BR CLOCK_BOOTTIME " (Since Linux 3.15)" | |
80 | .\" commit 4a2378a943f09907fb1ae35c15de917f60289c14 | |
81 | Like | |
82 | .BR CLOCK_MONOTONIC , | |
83 | this is a monotonically increasing clock. | |
84 | However, whereas the | |
85 | .BR CLOCK_MONOTONIC | |
86 | clock does not measure the time while a system is suspended, the | |
87 | .BR CLOCK_BOOTTIME | |
88 | clock does include the time during which the system is suspended. | |
89 | This is useful for applications that need to be suspend-aware. | |
90 | .BR CLOCK_REALTIME | |
91 | is not suitable for such applications, since that clock is affected | |
92 | by discontinuous changes to the system clock. | |
93 | .TP | |
94 | .BR CLOCK_REALTIME_ALARM " (since Linux 3.11)" | |
95 | .\" commit 11ffa9d6065f344a9bd769a2452f26f2f671e5f8 | |
96 | This clock is like | |
97 | .BR CLOCK_REALTIME , | |
98 | but will wake the system if it is suspended. | |
99 | The caller must have the | |
100 | .B CAP_WAKE_ALARM | |
101 | capability in order to set a timer against this clock. | |
102 | .TP | |
103 | .BR CLOCK_BOOTTIME_ALARM " (since Linux 3.11)" | |
104 | .\" commit 11ffa9d6065f344a9bd769a2452f26f2f671e5f8 | |
105 | This clock is like | |
106 | .BR CLOCK_BOOTTIME , | |
107 | but will wake the system if it is suspended. | |
108 | The caller must have the | |
109 | .B CAP_WAKE_ALARM | |
110 | capability in order to set a timer against this clock. | |
4b76fffb | 111 | .PP |
45b81c9c | 112 | The current value of each of these clocks can be retrieved using |
0eb44391 | 113 | .BR clock_gettime (2). |
45b81c9c | 114 | |
3df1b973 MK |
115 | Starting with Linux 2.6.27, the following values may be bitwise ORed in |
116 | .IR flags | |
117 | to change the behavior of | |
118 | .BR timerfd_create (): | |
119 | .TP 14 | |
120 | .B TFD_NONBLOCK | |
121 | Set the | |
122 | .BR O_NONBLOCK | |
b25c5b23 | 123 | file status flag on the new open file description. |
3df1b973 | 124 | Using this flag saves extra calls to |
ef7db4f5 | 125 | .BR fcntl (2) |
3df1b973 MK |
126 | to achieve the same result. |
127 | .TP | |
128 | .B TFD_CLOEXEC | |
129 | Set the close-on-exec | |
130 | .RB ( FD_CLOEXEC ) | |
131 | flag on the new file descriptor. | |
c5571b61 | 132 | See the description of the |
3df1b973 MK |
133 | .B O_CLOEXEC |
134 | flag in | |
135 | .BR open (2) | |
136 | for reasons why this may be useful. | |
137 | .PP | |
138 | In Linux versions up to and including 2.6.26, | |
45b81c9c | 139 | .I flags |
3df1b973 | 140 | must be specified as zero. |
45b81c9c MK |
141 | .SS timerfd_settime() |
142 | .BR timerfd_settime () | |
143 | arms (starts) or disarms (stops) | |
144 | the timer referred to by the file descriptor | |
145 | .IR fd . | |
146 | ||
147 | The | |
148 | .I new_value | |
149 | argument specifies the initial expiration and interval for the timer. | |
150 | The | |
151 | .I itimer | |
152 | structure used for this argument contains two fields, | |
153 | each of which is in turn a structure of type | |
154 | .IR timespec : | |
155 | .in +4n | |
156 | .nf | |
157 | ||
158 | struct timespec { | |
159 | time_t tv_sec; /* Seconds */ | |
160 | long tv_nsec; /* Nanoseconds */ | |
161 | }; | |
162 | ||
163 | struct itimerspec { | |
164 | struct timespec it_interval; /* Interval for periodic timer */ | |
165 | struct timespec it_value; /* Initial expiration */ | |
166 | }; | |
167 | .fi | |
168 | .in | |
169 | .PP | |
170 | .I new_value.it_value | |
171 | specifies the initial expiration of the timer, | |
172 | in seconds and nanoseconds. | |
173 | Setting either field of | |
174 | .I new_value.it_value | |
c7094399 | 175 | to a nonzero value arms the timer. |
45b81c9c MK |
176 | Setting both fields of |
177 | .I new_value.it_value | |
178 | to zero disarms the timer. | |
179 | ||
180 | Setting one or both fields of | |
181 | .I new_value.it_interval | |
c7094399 | 182 | to nonzero values specifies the period, in seconds and nanoseconds, |
45b81c9c MK |
183 | for repeated timer expirations after the initial expiration. |
184 | If both fields of | |
185 | .I new_value.it_interval | |
186 | are zero, the timer expires just once, at the time specified by | |
187 | .IR new_value.it_value . | |
188 | ||
51062576 MK |
189 | By default, |
190 | the initial expiration time specified in | |
191 | .I new_value | |
192 | is interpreted relative to the current time | |
193 | on the timer's clock at the time of the call (i.e., | |
194 | .I new_value.it_value | |
195 | specifies a time relative to the current value of the clock specified by | |
196 | .IR clockid ). | |
197 | An absolute timeout can be selected via the | |
198 | .I flags | |
199 | argument. | |
200 | ||
45b81c9c MK |
201 | The |
202 | .I flags | |
51062576 MK |
203 | argument is a bit mask that can include the following values: |
204 | .TP | |
205 | .B TFD_TIMER_ABSTIME | |
206 | Interpret | |
207 | .I new_value.it_value | |
208 | as an absolute value on the timer's clock. | |
209 | The timer will expire when the value of the timer's | |
45b81c9c | 210 | clock reaches the value specified in |
51062576 | 211 | .IR new_value.it_value . |
e9477548 MK |
212 | .TP |
213 | .BR TFD_TIMER_CANCEL_ON_SET | |
214 | If this flag is specified along with | |
215 | .B TFD_TIMER_ABSTIME | |
216 | and the clock for this timer is | |
217 | .BR CLOCK_REALTIME | |
218 | or | |
219 | .BR CLOCK_REALTIME_ALARM , | |
220 | then mark this timer as cancelable if the real-time clock | |
221 | undergoes a discontinuous change | |
222 | .RB ( settimeofday (2), | |
223 | .BR clock_settime (2), | |
224 | or similar). | |
225 | When such changes occur, a current or future | |
226 | .BR read (2) | |
227 | from the file descriptor will fail with the error | |
228 | .BR ECANCELED . | |
51062576 | 229 | .PP |
009cdc22 | 230 | If the |
5c902597 | 231 | .I old_value |
009cdc22 MK |
232 | argument is not NULL, then the |
233 | .I itimerspec | |
234 | structure that it points to is used to return the setting of the timer | |
235 | that was current at the time of the call; | |
236 | see the description of | |
45b81c9c MK |
237 | .BR timerfd_gettime () |
238 | following. | |
239 | .\" | |
240 | .SS timerfd_gettime() | |
241 | .BR timerfd_gettime () | |
242 | returns, in | |
243 | .IR curr_value , | |
244 | an | |
245 | .IR itimerspec | |
dc55661b | 246 | structure that contains the current setting of the timer |
45b81c9c MK |
247 | referred to by the file descriptor |
248 | .IR fd . | |
249 | ||
250 | The | |
251 | .I it_value | |
252 | field returns the amount of time | |
253 | until the timer will next expire. | |
254 | If both fields of this structure are zero, | |
255 | then the timer is currently disarmed. | |
256 | This field always contains a relative value, regardless of whether the | |
257 | .BR TFD_TIMER_ABSTIME | |
258 | flag was specified when setting the timer. | |
259 | ||
260 | The | |
261 | .I it_interval | |
262 | field returns the interval of the timer. | |
263 | If both fields of this structure are zero, | |
264 | then the timer is set to expire just once, at the time specified by | |
265 | .IR curr_value.it_value . | |
266 | .SS Operating on a timer file descriptor | |
267 | The file descriptor returned by | |
268 | .BR timerfd_create () | |
269 | supports the following operations: | |
270 | .TP | |
271 | .BR read (2) | |
272 | If the timer has already expired one or more times since | |
273 | its settings were last modified using | |
274 | .BR timerfd_settime (), | |
275 | or since the last successful | |
276 | .BR read (2), | |
277 | then the buffer given to | |
278 | .BR read (2) | |
279 | returns an unsigned 8-byte integer | |
280 | .RI ( uint64_t ) | |
281 | containing the number of expirations that have occurred. | |
88879aeb MK |
282 | (The returned value is in host byte order\(emthat is, |
283 | the native byte order for integers on the host machine.) | |
45b81c9c MK |
284 | .IP |
285 | If no timer expirations have occurred at the time of the | |
286 | .BR read (2), | |
287 | then the call either blocks until the next timer expiration, | |
288 | or fails with the error | |
289 | .B EAGAIN | |
ff40dbb3 | 290 | if the file descriptor has been made nonblocking |
45b81c9c MK |
291 | (via the use of the |
292 | .BR fcntl (2) | |
293 | .B F_SETFL | |
294 | operation to set the | |
295 | .B O_NONBLOCK | |
296 | flag). | |
297 | .IP | |
298 | A | |
299 | .BR read (2) | |
300 | will fail with the error | |
301 | .B EINVAL | |
302 | if the size of the supplied buffer is less than 8 bytes. | |
e9477548 MK |
303 | |
304 | If the associated clock is either | |
305 | .BR CLOCK_REALTIME | |
306 | or | |
307 | .BR CLOCK_REALTIME_ALARM , | |
308 | the timer is absolute | |
309 | .RB ( TFD_TIMER_ABSTIME ), | |
310 | and the flag | |
311 | .BR TFD_TIMER_CANCEL_ON_SET | |
312 | was specified when calling | |
313 | .BR timerfd_settime (), | |
314 | then | |
315 | .BR read (2) | |
316 | will fail with the error | |
317 | .BR ECANCELED | |
318 | if the real-time clock undergoes a discontinuous change. | |
319 | (This allows the reading application to discover | |
320 | such discontinuous changes to the clock.) | |
45b81c9c MK |
321 | .TP |
322 | .BR poll "(2), " select "(2) (and similar)" | |
323 | The file descriptor is readable | |
324 | (the | |
325 | .BR select (2) | |
326 | .I readfds | |
327 | argument; the | |
328 | .BR poll (2) | |
329 | .B POLLIN | |
330 | flag) | |
331 | if one or more timer expirations have occurred. | |
332 | .IP | |
333 | The file descriptor also supports the other file-descriptor | |
334 | multiplexing APIs: | |
335 | .BR pselect (2), | |
336 | .BR ppoll (2), | |
337 | and | |
338 | .BR epoll (7). | |
339 | .TP | |
5a3eb9cd | 340 | .BR ioctl "(2)" |
80d27367 MK |
341 | The following timerfd-specific command is supported: |
342 | .RS | |
343 | .TP | |
40f6b649 MK |
344 | .BR TFD_IOC_SET_TICKS " (since Linux 3.17)" |
345 | .\" commit 5442e9fbd7c23172a1c9bc736629cd123a9923f0 | |
80d27367 MK |
346 | Adjust the number of timer expirations that have occurred. |
347 | The argument is a pointer to a nonzero 8-byte integer | |
5a3eb9cd CG |
348 | .RI ( uint64_t *) |
349 | containing the new number of expirations. | |
80d27367 | 350 | Once the number is set, any waiter on the timer is woken up. |
5a3eb9cd | 351 | The only purpose of this command is to restore the expirations |
80d27367 MK |
352 | for the purpose of checkpoint/restore. |
353 | This operation is available only if the kernel was configured with the | |
5a3eb9cd | 354 | .BR CONFIG_CHECKPOINT_RESTORE |
80d27367 MK |
355 | option. |
356 | .RE | |
5a3eb9cd | 357 | .TP |
45b81c9c MK |
358 | .BR close (2) |
359 | When the file descriptor is no longer required it should be closed. | |
360 | When all file descriptors associated with the same timer object | |
361 | have been closed, | |
362 | the timer is disarmed and its resources are freed by the kernel. | |
363 | .\" | |
364 | .SS fork(2) semantics | |
365 | After a | |
366 | .BR fork (2), | |
367 | the child inherits a copy of the file descriptor created by | |
368 | .BR timerfd_create (). | |
369 | The file descriptor refers to the same underlying | |
370 | timer object as the corresponding file descriptor in the parent, | |
371 | and | |
372 | .BR read (2)s | |
373 | in the child will return information about | |
374 | expirations of the timer. | |
375 | .\" | |
376 | .SS execve(2) semantics | |
377 | A file descriptor created by | |
378 | .BR timerfd_create () | |
379 | is preserved across | |
380 | .BR execve (2), | |
381 | and continues to generate timer expirations if the timer was armed. | |
47297adb | 382 | .SH RETURN VALUE |
45b81c9c MK |
383 | On success, |
384 | .BR timerfd_create () | |
385 | returns a new file descriptor. | |
386 | On error, \-1 is returned and | |
387 | .I errno | |
388 | is set to indicate the error. | |
389 | ||
390 | .BR timerfd_settime () | |
391 | and | |
392 | .BR timerfd_gettime () | |
393 | return 0 on success; | |
394 | on error they return \-1, and set | |
395 | .I errno | |
396 | to indicate the error. | |
397 | .SH ERRORS | |
398 | .BR timerfd_create () | |
399 | can fail with the following errors: | |
400 | .TP | |
401 | .B EINVAL | |
402 | The | |
403 | .I clockid | |
404 | argument is neither | |
405 | .B CLOCK_MONOTONIC | |
406 | nor | |
407 | .BR CLOCK_REALTIME ; | |
3df1b973 MK |
408 | .TP |
409 | .B EINVAL | |
410 | .I flags | |
411 | is invalid; | |
412 | or, in Linux 2.6.26 or earlier, | |
45b81c9c | 413 | .I flags |
c7094399 | 414 | is nonzero. |
45b81c9c MK |
415 | .TP |
416 | .B EMFILE | |
26c32fab | 417 | The per-process limit on the number of open file descriptors has been reached. |
45b81c9c MK |
418 | .TP |
419 | .B ENFILE | |
420 | The system-wide limit on the total number of open files has been | |
421 | reached. | |
422 | .TP | |
423 | .B ENODEV | |
424 | Could not mount (internal) anonymous inode device. | |
425 | .TP | |
426 | .B ENOMEM | |
427 | There was insufficient kernel memory to create the timer. | |
428 | .PP | |
429 | .BR timerfd_settime () | |
430 | and | |
431 | .BR timerfd_gettime () | |
432 | can fail with the following errors: | |
433 | .TP | |
434 | .B EBADF | |
435 | .I fd | |
436 | is not a valid file descriptor. | |
437 | .TP | |
2bbb3907 MK |
438 | .B EFAULT |
439 | .IR new_value , | |
440 | .IR old_value , | |
441 | or | |
442 | .I curr_value | |
443 | is not valid a pointer. | |
444 | .TP | |
45b81c9c MK |
445 | .B EINVAL |
446 | .I fd | |
447 | is not a valid timerfd file descriptor. | |
c71b054d MK |
448 | .PP |
449 | .BR timerfd_settime () | |
450 | can also fail with the following errors: | |
451 | .TP | |
452 | .B EINVAL | |
45b81c9c MK |
453 | .I new_value |
454 | is not properly initialized (one of the | |
455 | .I tv_nsec | |
456 | falls outside the range zero to 999,999,999). | |
86d95cf2 MK |
457 | .TP |
458 | .B EINVAL | |
459 | .\" This case only checked since 2.6.29, and 2.2.2[78].some-stable-version. | |
460 | .\" In older kernel versions, no check was made for invalid flags. | |
461 | .I flags | |
462 | is invalid. | |
45b81c9c MK |
463 | .SH VERSIONS |
464 | These system calls are available on Linux since kernel 2.6.25. | |
d6a54d42 | 465 | Library support is provided by glibc since version 2.8. |
45b81c9c MK |
466 | .SH CONFORMING TO |
467 | These system calls are Linux-specific. | |
7484d5a7 MK |
468 | .SH BUGS |
469 | Currently, | |
470 | .\" 2.6.29 | |
471 | .BR timerfd_create () | |
472 | supports fewer types of clock IDs than | |
473 | .BR timer_create (2). | |
45b81c9c MK |
474 | .SH EXAMPLE |
475 | The following program creates a timer and then monitors its progress. | |
476 | The program accepts up to three command-line arguments. | |
477 | The first argument specifies the number of seconds for | |
478 | the initial expiration of the timer. | |
479 | The second argument specifies the interval for the timer, in seconds. | |
480 | The third argument specifies the number of times the program should | |
481 | allow the timer to expire before terminating. | |
482 | The second and third command-line arguments are optional. | |
483 | ||
484 | The following shell session demonstrates the use of the program: | |
485 | .in +4n | |
486 | .nf | |
487 | ||
b43a3b30 | 488 | .RB "$" " a.out 3 1 100" |
45b81c9c MK |
489 | 0.000: timer started |
490 | 3.000: read: 1; total=1 | |
491 | 4.000: read: 1; total=2 | |
b43a3b30 | 492 | .BR "^Z " " # type control-Z to suspend the program" |
45b81c9c | 493 | [1]+ Stopped ./timerfd3_demo 3 1 100 |
b43a3b30 | 494 | .RB "$ " "fg" " # Resume execution after a few seconds" |
45b81c9c MK |
495 | a.out 3 1 100 |
496 | 9.660: read: 5; total=7 | |
497 | 10.000: read: 1; total=8 | |
498 | 11.000: read: 1; total=9 | |
b43a3b30 | 499 | .BR "^C " " # type control-C to suspend the program" |
45b81c9c MK |
500 | .fi |
501 | .in | |
9c330504 | 502 | .SS Program source |
d84d0300 | 503 | \& |
45b81c9c | 504 | .nf |
45b81c9c MK |
505 | .\" The commented out code here is what we currently need until |
506 | .\" the required stuff is in glibc | |
507 | .\" | |
508 | .\" | |
509 | .\"/* Link with -lrt */ | |
510 | .\"#define _GNU_SOURCE | |
511 | .\"#include <sys/syscall.h> | |
512 | .\"#include <unistd.h> | |
513 | .\"#include <time.h> | |
514 | .\"#if defined(__i386__) | |
515 | .\"#define __NR_timerfd_create 322 | |
516 | .\"#define __NR_timerfd_settime 325 | |
517 | .\"#define __NR_timerfd_gettime 326 | |
518 | .\"#endif | |
519 | .\" | |
520 | .\"static int | |
521 | .\"timerfd_create(int clockid, int flags) | |
522 | .\"{ | |
523 | .\" return syscall(__NR_timerfd_create, clockid, flags); | |
524 | .\"} | |
525 | .\" | |
526 | .\"static int | |
527 | .\"timerfd_settime(int fd, int flags, struct itimerspec *new_value, | |
528 | .\" struct itimerspec *curr_value) | |
529 | .\"{ | |
530 | .\" return syscall(__NR_timerfd_settime, fd, flags, new_value, | |
531 | .\" curr_value); | |
532 | .\"} | |
533 | .\" | |
534 | .\"static int | |
535 | .\"timerfd_gettime(int fd, struct itimerspec *curr_value) | |
536 | .\"{ | |
537 | .\" return syscall(__NR_timerfd_gettime, fd, curr_value); | |
538 | .\"} | |
539 | .\" | |
540 | .\"#define TFD_TIMER_ABSTIME (1 << 0) | |
541 | .\" | |
542 | .\"//////////////////////////////////////////////////////////// | |
45b81c9c MK |
543 | #include <sys/timerfd.h> |
544 | #include <time.h> | |
545 | #include <unistd.h> | |
546 | #include <stdlib.h> | |
547 | #include <stdio.h> | |
548 | #include <stdint.h> /* Definition of uint64_t */ | |
549 | ||
550 | #define handle_error(msg) \\ | |
551 | do { perror(msg); exit(EXIT_FAILURE); } while (0) | |
552 | ||
553 | static void | |
554 | print_elapsed_time(void) | |
555 | { | |
556 | static struct timespec start; | |
557 | struct timespec curr; | |
558 | static int first_call = 1; | |
559 | int secs, nsecs; | |
560 | ||
561 | if (first_call) { | |
562 | first_call = 0; | |
563 | if (clock_gettime(CLOCK_MONOTONIC, &start) == \-1) | |
564 | handle_error("clock_gettime"); | |
565 | } | |
566 | ||
567 | if (clock_gettime(CLOCK_MONOTONIC, &curr) == \-1) | |
568 | handle_error("clock_gettime"); | |
569 | ||
570 | secs = curr.tv_sec \- start.tv_sec; | |
571 | nsecs = curr.tv_nsec \- start.tv_nsec; | |
572 | if (nsecs < 0) { | |
573 | secs\-\-; | |
574 | nsecs += 1000000000; | |
575 | } | |
576 | printf("%d.%03d: ", secs, (nsecs + 500000) / 1000000); | |
577 | } | |
578 | ||
579 | int | |
580 | main(int argc, char *argv[]) | |
581 | { | |
582 | struct itimerspec new_value; | |
583 | int max_exp, fd; | |
584 | struct timespec now; | |
585 | uint64_t exp, tot_exp; | |
586 | ssize_t s; | |
587 | ||
588 | if ((argc != 2) && (argc != 4)) { | |
589 | fprintf(stderr, "%s init\-secs [interval\-secs max\-exp]\\n", | |
590 | argv[0]); | |
591 | exit(EXIT_FAILURE); | |
592 | } | |
593 | ||
594 | if (clock_gettime(CLOCK_REALTIME, &now) == \-1) | |
595 | handle_error("clock_gettime"); | |
596 | ||
597 | /* Create a CLOCK_REALTIME absolute timer with initial | |
598 | expiration and interval as specified in command line */ | |
599 | ||
600 | new_value.it_value.tv_sec = now.tv_sec + atoi(argv[1]); | |
601 | new_value.it_value.tv_nsec = now.tv_nsec; | |
602 | if (argc == 2) { | |
603 | new_value.it_interval.tv_sec = 0; | |
604 | max_exp = 1; | |
605 | } else { | |
606 | new_value.it_interval.tv_sec = atoi(argv[2]); | |
607 | max_exp = atoi(argv[3]); | |
608 | } | |
609 | new_value.it_interval.tv_nsec = 0; | |
610 | ||
611 | fd = timerfd_create(CLOCK_REALTIME, 0); | |
612 | if (fd == \-1) | |
613 | handle_error("timerfd_create"); | |
614 | ||
a279595b | 615 | if (timerfd_settime(fd, TFD_TIMER_ABSTIME, &new_value, NULL) == \-1) |
45b81c9c MK |
616 | handle_error("timerfd_settime"); |
617 | ||
618 | print_elapsed_time(); | |
619 | printf("timer started\\n"); | |
620 | ||
621 | for (tot_exp = 0; tot_exp < max_exp;) { | |
622 | s = read(fd, &exp, sizeof(uint64_t)); | |
623 | if (s != sizeof(uint64_t)) | |
624 | handle_error("read"); | |
625 | ||
626 | tot_exp += exp; | |
627 | print_elapsed_time(); | |
628 | printf("read: %llu; total=%llu\\n", | |
629 | (unsigned long long) exp, | |
630 | (unsigned long long) tot_exp); | |
631 | } | |
632 | ||
633 | exit(EXIT_SUCCESS); | |
634 | } | |
635 | .fi | |
47297adb | 636 | .SH SEE ALSO |
45b81c9c MK |
637 | .BR eventfd (2), |
638 | .BR poll (2), | |
639 | .BR read (2), | |
640 | .BR select (2), | |
641 | .BR setitimer (2), | |
642 | .BR signalfd (2), | |
804f03e6 MK |
643 | .BR timer_create (2), |
644 | .BR timer_gettime (2), | |
645 | .BR timer_settime (2), | |
45b81c9c MK |
646 | .BR epoll (7), |
647 | .BR time (7) |