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