-
Notifications
You must be signed in to change notification settings - Fork 0
/
timerfd_settime.html
750 lines (573 loc) · 22.4 KB
/
timerfd_settime.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
<!-- Creator : groff version 1.22.4 -->
<!-- CreationDate: Wed Jan 29 11:25:46 2020 -->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta name="generator" content="groff -Thtml, see www.gnu.org">
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<meta name="Content-Style" content="text/css">
<style type="text/css">
p { margin-top: 0; margin-bottom: 0; vertical-align: top }
pre { margin-top: 0; margin-bottom: 0; vertical-align: top }
table { margin-top: 0; margin-bottom: 0; vertical-align: top }
h1 { text-align: center }
</style>
<title>TIMERFD_CREATE</title>
</head>
<body>
<h1 align="center">TIMERFD_CREATE</h1>
<a href="#NAME">NAME</a><br>
<a href="#SYNOPSIS">SYNOPSIS</a><br>
<a href="#DESCRIPTION">DESCRIPTION</a><br>
<a href="#RETURN VALUE">RETURN VALUE</a><br>
<a href="#ERRORS">ERRORS</a><br>
<a href="#VERSIONS">VERSIONS</a><br>
<a href="#CONFORMING TO">CONFORMING TO</a><br>
<a href="#BUGS">BUGS</a><br>
<a href="#EXAMPLE">EXAMPLE</a><br>
<a href="#SEE ALSO">SEE ALSO</a><br>
<a href="#COLOPHON">COLOPHON</a><br>
<hr>
<h2>NAME
<a name="NAME"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">timerfd_create,
timerfd_settime, timerfd_gettime - timers that notify via
file descriptors</p>
<h2>SYNOPSIS
<a name="SYNOPSIS"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em"><b>#include
<sys/timerfd.h></b></p>
<p style="margin-left:11%; margin-top: 1em"><b>int
timerfd_create(int</b> <i>clockid</i><b>, int</b>
<i>flags</i><b>);</b></p>
<p style="margin-left:11%; margin-top: 1em"><b>int
timerfd_settime(int</b> <i>fd</i><b>, int</b>
<i>flags</i><b>, <br>
const struct itimerspec *</b><i>new_value</i><b>, <br>
struct itimerspec *</b><i>old_value</i><b>);</b></p>
<p style="margin-left:11%; margin-top: 1em"><b>int
timerfd_gettime(int</b> <i>fd</i><b>, struct itimerspec
*</b><i>curr_value</i><b>);</b></p>
<h2>DESCRIPTION
<a name="DESCRIPTION"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">These system
calls create and operate on a timer that delivers timer
expiration notifications via a file descriptor. They provide
an alternative to the use of <b>setitimer</b>(2) or
<b>timer_create</b>(2), with the advantage that the file
descriptor may be monitored by <b>select</b>(2),
<b>poll</b>(2), and <b>epoll</b>(7).</p>
<p style="margin-left:11%; margin-top: 1em">The use of
these three system calls is analogous to the use of
<b>timer_create</b>(2), <b>timer_settime</b>(2), and
<b>timer_gettime</b>(2). (There is no analog of
<b>timer_getoverrun</b>(2), since that functionality is
provided by <b>read</b>(2), as described below.)</p>
<p style="margin-left:11%; margin-top: 1em"><b>timerfd_create()
<br>
timerfd_create</b>() creates a new timer object, and returns
a file descriptor that refers to that timer. The
<i>clockid</i> argument specifies the clock that is used to
mark the progress of the timer, and must be one of the
following: <b><br>
CLOCK_REALTIME</b></p>
<p style="margin-left:22%;">A settable system-wide
real-time clock.</p>
<p style="margin-left:11%;"><b>CLOCK_MONOTONIC</b></p>
<p style="margin-left:22%;">A nonsettable monotonically
increasing clock that measures time from some unspecified
point in the past that does not change after system
startup.</p>
<p style="margin-left:11%;"><b>CLOCK_BOOTTIME</b> (Since
Linux 3.15)</p>
<p style="margin-left:22%;">Like <b>CLOCK_MONOTONIC</b>,
this is a monotonically increasing clock. However, whereas
the <b>CLOCK_MONOTONIC</b> clock does not measure the time
while a system is suspended, the <b>CLOCK_BOOTTIME</b> clock
does include the time during which the system is suspended.
This is useful for applications that need to be
suspend-aware. <b>CLOCK_REALTIME</b> is not suitable for
such applications, since that clock is affected by
discontinuous changes to the system clock.</p>
<p style="margin-left:11%;"><b>CLOCK_REALTIME_ALARM</b>
(since Linux 3.11)</p>
<p style="margin-left:22%;">This clock is like
<b>CLOCK_REALTIME</b>, but will wake the system if it is
suspended. The caller must have the <b>CAP_WAKE_ALARM</b>
capability in order to set a timer against this clock.</p>
<p style="margin-left:11%;"><b>CLOCK_BOOTTIME_ALARM</b>
(since Linux 3.11)</p>
<p style="margin-left:22%;">This clock is like
<b>CLOCK_BOOTTIME</b>, but will wake the system if it is
suspended. The caller must have the <b>CAP_WAKE_ALARM</b>
capability in order to set a timer against this clock.</p>
<p style="margin-left:11%; margin-top: 1em">The current
value of each of these clocks can be retrieved using
<b>clock_gettime</b>(2).</p>
<p style="margin-left:11%; margin-top: 1em">Starting with
Linux 2.6.27, the following values may be bitwise ORed in
<i>flags</i> to change the behavior of
<b>timerfd_create</b>():</p>
<table width="100%" border="0" rules="none" frame="void"
cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td width="11%"></td>
<td width="18%">
<p><b>TFD_NONBLOCK</b></p></td>
<td width="3%"></td>
<td width="68%">
<p>Set the <b>O_NONBLOCK</b> file status flag on the open
file description (see <b>open</b>(2)) referred to by the new
file descriptor. Using this flag saves extra calls to
<b>fcntl</b>(2) to achieve the same result.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="18%">
<p><b>TFD_CLOEXEC</b></p></td>
<td width="3%"></td>
<td width="68%">
<p>Set the close-on-exec (<b>FD_CLOEXEC</b>) flag on the
new file descriptor. See the description of the
<b>O_CLOEXEC</b> flag in <b>open</b>(2) for reasons why this
may be useful.</p></td></tr>
</table>
<p style="margin-left:11%; margin-top: 1em">In Linux
versions up to and including 2.6.26, <i>flags</i> must be
specified as zero.</p>
<p style="margin-left:11%; margin-top: 1em"><b>timerfd_settime()
<br>
timerfd_settime</b>() arms (starts) or disarms (stops) the
timer referred to by the file descriptor <i>fd</i>.</p>
<p style="margin-left:11%; margin-top: 1em">The
<i>new_value</i> argument specifies the initial expiration
and interval for the timer. The <i>itimerspec</i> structure
used for this argument contains two fields, each of which is
in turn a structure of type <i>timespec</i>:</p>
<p style="margin-left:17%; margin-top: 1em">struct timespec
{ <br>
time_t tv_sec; /* Seconds */ <br>
long tv_nsec; /* Nanoseconds */ <br>
};</p>
<p style="margin-left:17%; margin-top: 1em">struct
itimerspec { <br>
struct timespec it_interval; /* Interval for periodic timer
*/ <br>
struct timespec it_value; /* Initial expiration */ <br>
};</p>
<p style="margin-left:11%; margin-top: 1em"><i>new_value.it_value</i>
specifies the initial expiration of the timer, in seconds
and nanoseconds. Setting either field of
<i>new_value.it_value</i> to a nonzero value arms the timer.
Setting both fields of <i>new_value.it_value</i> to zero
disarms the timer.</p>
<p style="margin-left:11%; margin-top: 1em">Setting one or
both fields of <i>new_value.it_interval</i> to nonzero
values specifies the period, in seconds and nanoseconds, for
repeated timer expirations after the initial expiration. If
both fields of <i>new_value.it_interval</i> are zero, the
timer expires just once, at the time specified by
<i>new_value.it_value</i>.</p>
<p style="margin-left:11%; margin-top: 1em">By default, the
initial expiration time specified in <i>new_value</i> is
interpreted relative to the current time on the
timer’s clock at the time of the call (i.e.,
<i>new_value.it_value</i> specifies a time relative to the
current value of the clock specified by <i>clockid</i>). An
absolute timeout can be selected via the <i>flags</i>
argument.</p>
<p style="margin-left:11%; margin-top: 1em">The
<i>flags</i> argument is a bit mask that can include the
following values: <b><br>
TFD_TIMER_ABSTIME</b></p>
<p style="margin-left:22%;">Interpret
<i>new_value.it_value</i> as an absolute value on the
timer’s clock. The timer will expire when the value of
the timer’s clock reaches the value specified in
<i>new_value.it_value</i>.</p>
<p style="margin-left:11%;"><b>TFD_TIMER_CANCEL_ON_SET</b></p>
<p style="margin-left:22%;">If this flag is specified along
with <b>TFD_TIMER_ABSTIME</b> and the clock for this timer
is <b>CLOCK_REALTIME</b> or <b>CLOCK_REALTIME_ALARM</b>,
then mark this timer as cancelable if the real-time clock
undergoes a discontinuous change (<b>settimeofday</b>(2),
<b>clock_settime</b>(2), or similar). When such changes
occur, a current or future <b>read</b>(2) from the file
descriptor will fail with the error <b>ECANCELED</b>.</p>
<p style="margin-left:11%; margin-top: 1em">If the
<i>old_value</i> argument is not NULL, then the
<i>itimerspec</i> structure that it points to is used to
return the setting of the timer that was current at the time
of the call; see the description of <b>timerfd_gettime</b>()
following.</p>
<p style="margin-left:11%; margin-top: 1em"><b>timerfd_gettime()
<br>
timerfd_gettime</b>() returns, in <i>curr_value</i>, an
<i>itimerspec</i> structure that contains the current
setting of the timer referred to by the file descriptor
<i>fd</i>.</p>
<p style="margin-left:11%; margin-top: 1em">The
<i>it_value</i> field returns the amount of time until the
timer will next expire. If both fields of this structure are
zero, then the timer is currently disarmed. This field
always contains a relative value, regardless of whether the
<b>TFD_TIMER_ABSTIME</b> flag was specified when setting the
timer.</p>
<p style="margin-left:11%; margin-top: 1em">The
<i>it_interval</i> field returns the interval of the timer.
If both fields of this structure are zero, then the timer is
set to expire just once, at the time specified by
<i>curr_value.it_value</i>.</p>
<p style="margin-left:11%; margin-top: 1em"><b>Operating on
a timer file descriptor</b> <br>
The file descriptor returned by <b>timerfd_create</b>()
supports the following operations: <b><br>
read</b>(2)</p>
<p style="margin-left:22%;">If the timer has already
expired one or more times since its settings were last
modified using <b>timerfd_settime</b>(), or since the last
successful <b>read</b>(2), then the buffer given to
<b>read</b>(2) returns an unsigned 8-byte integer
(<i>uint64_t</i>) containing the number of expirations that
have occurred. (The returned value is in host byte
order—that is, the native byte order for integers on
the host machine.)</p>
<p style="margin-left:22%; margin-top: 1em">If no timer
expirations have occurred at the time of the <b>read</b>(2),
then the call either blocks until the next timer expiration,
or fails with the error <b>EAGAIN</b> if the file descriptor
has been made nonblocking (via the use of the
<b>fcntl</b>(2) <b>F_SETFL</b> operation to set the
<b>O_NONBLOCK</b> flag).</p>
<p style="margin-left:22%; margin-top: 1em">A
<b>read</b>(2) fails with the error <b>EINVAL</b> if the
size of the supplied buffer is less than 8 bytes.</p>
<p style="margin-left:22%; margin-top: 1em">If the
associated clock is either <b>CLOCK_REALTIME</b> or
<b>CLOCK_REALTIME_ALARM</b>, the timer is absolute
(<b>TFD_TIMER_ABSTIME</b>), and the flag
<b>TFD_TIMER_CANCEL_ON_SET</b> was specified when calling
<b>timerfd_settime</b>(), then <b>read</b>(2) fails with the
error <b>ECANCELED</b> if the real-time clock undergoes a
discontinuous change. (This allows the reading application
to discover such discontinuous changes to the clock.)</p>
<p style="margin-left:11%;"><b>poll</b>(2),
<b>select</b>(2) (and similar)</p>
<p style="margin-left:22%;">The file descriptor is readable
(the <b>select</b>(2) <i>readfds</i> argument; the
<b>poll</b>(2) <b>POLLIN</b> flag) if one or more timer
expirations have occurred.</p>
<p style="margin-left:22%; margin-top: 1em">The file
descriptor also supports the other file-descriptor
multiplexing APIs: <b>pselect</b>(2), <b>ppoll</b>(2), and
<b>epoll</b>(7).</p>
<p style="margin-left:11%;"><b>ioctl</b>(2)</p>
<p style="margin-left:22%;">The following timerfd-specific
command is supported: <b><br>
TFD_IOC_SET_TICKS</b> (since Linux 3.17)</p>
<p style="margin-left:32%;">Adjust the number of timer
expirations that have occurred. The argument is a pointer to
a nonzero 8-byte integer (<i>uint64_t</i>*) containing the
new number of expirations. Once the number is set, any
waiter on the timer is woken up. The only purpose of this
command is to restore the expirations for the purpose of
checkpoint/restore. This operation is available only if the
kernel was configured with the
<b>CONFIG_CHECKPOINT_RESTORE</b> option.</p>
<p style="margin-left:11%;"><b>close</b>(2)</p>
<p style="margin-left:22%;">When the file descriptor is no
longer required it should be closed. When all file
descriptors associated with the same timer object have been
closed, the timer is disarmed and its resources are freed by
the kernel.</p>
<p style="margin-left:11%; margin-top: 1em"><b>fork(2)
semantics</b> <br>
After a <b>fork</b>(2), the child inherits a copy of the
file descriptor created by <b>timerfd_create</b>(). The file
descriptor refers to the same underlying timer object as the
corresponding file descriptor in the parent, and
<b>read</b>(2)s in the child will return information about
expirations of the timer.</p>
<p style="margin-left:11%; margin-top: 1em"><b>execve(2)
semantics</b> <br>
A file descriptor created by <b>timerfd_create</b>() is
preserved across <b>execve</b>(2), and continues to generate
timer expirations if the timer was armed.</p>
<h2>RETURN VALUE
<a name="RETURN VALUE"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">On success,
<b>timerfd_create</b>() returns a new file descriptor. On
error, -1 is returned and <i>errno</i> is set to indicate
the error.</p>
<p style="margin-left:11%; margin-top: 1em"><b>timerfd_settime</b>()
and <b>timerfd_gettime</b>() return 0 on success; on error
they return -1, and set <i>errno</i> to indicate the
error.</p>
<h2>ERRORS
<a name="ERRORS"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em"><b>timerfd_create</b>()
can fail with the following errors:</p>
<table width="100%" border="0" rules="none" frame="void"
cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>EINVAL</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>The <i>clockid</i> argument is neither
<b>CLOCK_MONOTONIC</b> nor <b>CLOCK_REALTIME</b>;</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>EINVAL</b></p></td>
<td width="2%"></td>
<td width="78%">
<p><i>flags</i> is invalid; or, in Linux 2.6.26 or earlier,
<i>flags</i> is nonzero.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>EMFILE</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>The per-process limit on the number of open file
descriptors has been reached.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>ENFILE</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>The system-wide limit on the total number of open files
has been reached.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>ENODEV</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>Could not mount (internal) anonymous inode device.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>ENOMEM</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>There was insufficient kernel memory to create the
timer.</p> </td></tr>
</table>
<p style="margin-left:11%; margin-top: 1em"><b>timerfd_settime</b>()
and <b>timerfd_gettime</b>() can fail with the following
errors:</p>
<table width="100%" border="0" rules="none" frame="void"
cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p style="margin-top: 1em"><b>EBADF</b></p></td>
<td width="2%"></td>
<td width="78%">
<p style="margin-top: 1em"><i>fd</i> is not a valid file
descriptor.</p> </td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>EFAULT</b></p></td>
<td width="2%"></td>
<td width="78%">
<p><i>new_value</i>, <i>old_value</i>, or <i>curr_value</i>
is not valid a pointer.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>EINVAL</b></p></td>
<td width="2%"></td>
<td width="78%">
<p><i>fd</i> is not a valid timerfd file descriptor.</p></td></tr>
</table>
<p style="margin-left:11%; margin-top: 1em"><b>timerfd_settime</b>()
can also fail with the following errors:</p>
<table width="100%" border="0" rules="none" frame="void"
cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p style="margin-top: 1em"><b>EINVAL</b></p></td>
<td width="2%"></td>
<td width="78%">
<p style="margin-top: 1em"><i>new_value</i> is not properly
initialized (one of the <i>tv_nsec</i> falls outside the
range zero to 999,999,999).</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>EINVAL</b></p></td>
<td width="2%"></td>
<td width="78%">
<p><i>flags</i> is invalid.</p></td></tr>
</table>
<h2>VERSIONS
<a name="VERSIONS"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">These system
calls are available on Linux since kernel 2.6.25. Library
support is provided by glibc since version 2.8.</p>
<h2>CONFORMING TO
<a name="CONFORMING TO"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">These system
calls are Linux-specific.</p>
<h2>BUGS
<a name="BUGS"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">Currently,
<b>timerfd_create</b>() supports fewer types of clock IDs
than <b>timer_create</b>(2).</p>
<h2>EXAMPLE
<a name="EXAMPLE"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">The following
program creates a timer and then monitors its progress. The
program accepts up to three command-line arguments. The
first argument specifies the number of seconds for the
initial expiration of the timer. The second argument
specifies the interval for the timer, in seconds. The third
argument specifies the number of times the program should
allow the timer to expire before terminating. The second and
third command-line arguments are optional.</p>
<p style="margin-left:11%; margin-top: 1em">The following
shell session demonstrates the use of the program:</p>
<p style="margin-left:17%; margin-top: 1em">$ <b>a.out 3 1
100</b> <br>
0.000: timer started <br>
3.000: read: 1; total=1 <br>
4.000: read: 1; total=2 <b><br>
^Z</b> # type control-Z to suspend the program <br>
[1]+ Stopped ./timerfd3_demo 3 1 100 <br>
$ <b>fg</b> # Resume execution after a few seconds <br>
a.out 3 1 100 <br>
9.660: read: 5; total=7 <br>
10.000: read: 1; total=8 <br>
11.000: read: 1; total=9 <b><br>
^C</b> # type control-C to suspend the program</p>
<p style="margin-left:11%; margin-top: 1em"><b>Program
source</b> <br>
#include <sys/timerfd.h> <br>
#include <time.h> <br>
#include <unistd.h> <br>
#include <stdlib.h> <br>
#include <stdio.h> <br>
#include <stdint.h> /* Definition of uint64_t */</p>
<p style="margin-left:11%; margin-top: 1em">#define
handle_error(msg) \ <br>
do { perror(msg); exit(EXIT_FAILURE); } while (0)</p>
<p style="margin-left:11%; margin-top: 1em">static void
<br>
print_elapsed_time(void) <br>
{ <br>
static struct timespec start; <br>
struct timespec curr; <br>
static int first_call = 1; <br>
int secs, nsecs;</p>
<p style="margin-left:11%; margin-top: 1em">if (first_call)
{ <br>
first_call = 0; <br>
if (clock_gettime(CLOCK_MONOTONIC, &start) == -1) <br>
handle_error("clock_gettime"); <br>
}</p>
<p style="margin-left:11%; margin-top: 1em">if
(clock_gettime(CLOCK_MONOTONIC, &curr) == -1) <br>
handle_error("clock_gettime");</p>
<p style="margin-left:11%; margin-top: 1em">secs =
curr.tv_sec - start.tv_sec; <br>
nsecs = curr.tv_nsec - start.tv_nsec; <br>
if (nsecs < 0) { <br>
secs--; <br>
nsecs += 1000000000; <br>
} <br>
printf("%d.%03d: ", secs, (nsecs + 500000) /
1000000); <br>
}</p>
<p style="margin-left:11%; margin-top: 1em">int <br>
main(int argc, char *argv[]) <br>
{ <br>
struct itimerspec new_value; <br>
int max_exp, fd; <br>
struct timespec now; <br>
uint64_t exp, tot_exp; <br>
ssize_t s;</p>
<p style="margin-left:11%; margin-top: 1em">if ((argc != 2)
&& (argc != 4)) { <br>
fprintf(stderr, "%s init-secs [interval-secs
max-exp]\n", <br>
argv[0]); <br>
exit(EXIT_FAILURE); <br>
}</p>
<p style="margin-left:11%; margin-top: 1em">if
(clock_gettime(CLOCK_REALTIME, &now) == -1) <br>
handle_error("clock_gettime");</p>
<p style="margin-left:11%; margin-top: 1em">/* Create a
CLOCK_REALTIME absolute timer with initial <br>
expiration and interval as specified in command line */</p>
<p style="margin-left:11%; margin-top: 1em">new_value.it_value.tv_sec
= now.tv_sec + atoi(argv[1]); <br>
new_value.it_value.tv_nsec = now.tv_nsec; <br>
if (argc == 2) { <br>
new_value.it_interval.tv_sec = 0; <br>
max_exp = 1; <br>
} else { <br>
new_value.it_interval.tv_sec = atoi(argv[2]); <br>
max_exp = atoi(argv[3]); <br>
} <br>
new_value.it_interval.tv_nsec = 0;</p>
<p style="margin-left:11%; margin-top: 1em">fd =
timerfd_create(CLOCK_REALTIME, 0); <br>
if (fd == -1) <br>
handle_error("timerfd_create");</p>
<p style="margin-left:11%; margin-top: 1em">if
(timerfd_settime(fd, TFD_TIMER_ABSTIME, &new_value,
NULL) == -1) <br>
handle_error("timerfd_settime");</p>
<p style="margin-left:11%; margin-top: 1em">print_elapsed_time();
<br>
printf("timer started\n");</p>
<p style="margin-left:11%; margin-top: 1em">for (tot_exp =
0; tot_exp < max_exp;) { <br>
s = read(fd, &exp, sizeof(uint64_t)); <br>
if (s != sizeof(uint64_t)) <br>
handle_error("read");</p>
<p style="margin-left:11%; margin-top: 1em">tot_exp += exp;
<br>
print_elapsed_time(); <br>
printf("read: %llu; total=%llu\n", <br>
(unsigned long long) exp, <br>
(unsigned long long) tot_exp); <br>
}</p>
<p style="margin-left:11%; margin-top: 1em">exit(EXIT_SUCCESS);
<br>
}</p>
<h2>SEE ALSO
<a name="SEE ALSO"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em"><b>eventfd</b>(2),
<b>poll</b>(2), <b>read</b>(2), <b>select</b>(2),
<b>setitimer</b>(2), <b>signalfd</b>(2),
<b>timer_create</b>(2), <b>timer_gettime</b>(2),
<b>timer_settime</b>(2), <b>epoll</b>(7), <b>time</b>(7)</p>
<h2>COLOPHON
<a name="COLOPHON"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">This page is
part of release 5.02 of the Linux <i>man-pages</i> project.
A description of the project, information about reporting
bugs, and the latest version of this page, can be found at
https://www.kernel.org/doc/man-pages/.</p>
<hr>
</body>
</html>