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
|
.\" Copyright (C) 2008 Michael Kerrisk <mtk.manpages@gmail.com>
.\"
.\" This program is free software; you can redistribute it and/or modify
.\" it under the terms of the GNU General Public License as published by
.\" the Free Software Foundation; either version 2 of the License, or
.\" (at your option) any later version.
.\"
.\" This program is distributed in the hope that it will be useful,
.\" but WITHOUT ANY WARRANTY; without even the implied warranty of
.\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
.\" GNU General Public License for more details.
.\"
.\" You should have received a copy of the GNU General Public License
.\" along with this program; if not, write to the Free Software
.\" Foundation, Inc., 59 Temple Place, Suite 330, Boston,
.\" MA 02111-1307 USA
.\"
.\" Japanese Version Copyright (c) 2008 Akihiro MOTOKI
.\" all rights reserved.
.\" Translated 2008-11-19, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v3.13
.\"
.TH TIMERFD_CREATE 2 2009-02-11 Linux "Linux Programmer's Manual"
.SH ̾
timerfd_create, timerfd_settime, timerfd_gettime \-
եǥץͳΤ륿ޡ
.SH
.nf
.B #include <sys/timerfd.h>
.sp
.BI "int timerfd_create(int " clockid ", int " flags );
.sp
.BI "int timerfd_settime(int " fd ", int " flags ,
.BI " const struct itimerspec *" new_value ,
.BI " struct itimerspec *" old_value );
.sp
.BI "int timerfd_gettime(int " fd ", struct itimerspec *" curr_value );
.fi
.SH
ΥƥॳϡλΤեǥץͳ
ޡԤ
ϡ
.BR setitimer (2)
.BR timer_create (2)
ѤˡȤʤΤǤꡢΥեǥץ
.BR select (2),
.BR poll (2),
.BR epoll (7)
ǴƻǤȤ롣
ΥƥॳȤΤϡ줾
.BR timer_create (2),
.BR timer_settime (2),
.BR timer_gettime (2)
ȤΤƱͤǤ
.RB ( timer_getoverrun (2)
бΤϤʤʲ褦
εǽ
.BR read (2)
ˤ)
.\"
.SS timerfd_create()
.BR timerfd_create ()
ϿΥޡ֥ȤΥޡȤե
ǥץ֤
.I clockid
ϡޡοĽ뤿ΥåꤹΤǡ
.B CLOCK_REALTIME
.B CLOCK_MONOTONIC
Τ줫ǤʤФʤʤ
.B CLOCK_REALTIME
ϥƥΤǻѤ륯åǡΥåѹǽǤ롣
.B CLOCK_MONOTONIC
ѹ뤳ȤΤʤåǡ(ƥμưǤѹʤɤ)
ƥ९åϢ³Ѳαƶʤ
Υåθߤͤ
.BR clock_gettime (2)
ȤäƼǤ롣
Linux 2.6.27 ʹߤǤϡ
ʲͤΤĤӥåñ̤ (OR) ǻꤹ뤳Ȥǡ
.BR timerfd_create ()
οѹ뤳ȤǤ롣
.TP 14
.B TFD_NONBLOCK
륪ץե뵭 (open file description)
.B O_NONBLOCK
ե륹ơե饰åȤ롣
Υե饰ȤȤǡ
.B O_NONBLOCK
åȤ뤿
.BR fcntl (2)
ɲäǸƤӽФɬפʤʤ롣
.TP
.B TFD_CLOEXEC
եǥץФ
close-on-exec
.RB ( FD_CLOEXEC )
ե饰åȤ롣
Υե饰ΩͳˤĤƤϡ
.BR open (2)
.B O_CLOEXEC
ե饰ȤΤȡ
.PP
С 2.6.26 Linux Ǥϡ
.I flags
̤ѤǤꡢ0 ꤷʤФʤʤ
.SS timerfd_settime()
.BR timerfd_settime ()
ϡեǥץ
.I fd
ˤ껲Ȥ륿ޡϤߤꤹ롣
.I new_value
ϡޡλ (expiration) νͤȴֳ (interval)
ꤹ롣ΰǻѤƤ
.I itimerspec
¤Τˤ 2 ĤΥեɤꡢƥեɤ
.I timespec
ι¤ΤǤ롣
.in +4n
.nf
struct timespec {
time_t tv_sec; /* Seconds */
long tv_nsec; /* Nanoseconds */
};
struct itimerspec {
struct timespec it_interval; /* Interval for periodic timer */
struct timespec it_value; /* Initial expiration */
};
.fi
.in
.PP
.I new_value.it_value
ϥޡλ֤νͤäȥʥäǻꤹ롣
.I new_value.it_value
ΥեɤΤʤȤ 0 ʳͤꤹȡ
ޡϤ롣
ξΥեɤ 0 ꤹȡޡߤ롣
.I new_value.it_interval
ϥޡΰܤλ˷֤ޡλֳ֤äȥʥäǻꤹ롣
.I new_value.it_interval
ΥեɤΤʤȤ 0 ʳͤꤹȡ
֤ޡͭˤʤ롣
ξΥեɤ 0 ꤷ硢ޡ
.I new_value.it_value
ǻꤵ줿ָˡλߤ롣
.I flags
ˤ 0
.B TFD_TIMER_ABSTIME
ꤹ롣
0 л勵ޡ̣
.I new_value.it_interval
Ǥ
.I clockid
ǻꤵ줿åθߤͤŪʻꤹ롣
.B TFD_TIMER_ABSTIME
л勵ޡ̣
.I new_value.it_interval
Ǥ
.I clockid
ǻꤵ줿åлꤹ롣Ĥޤꡢ
åͤ
.I new_value.it_interval
ǻꤵ줿ã顢ޡλ롣
.I old_value
̤ơ
.BR timerfd_settime ()
ƤӽФǤΥޡݻ¤Τ֤롣
.BR timerfd_gettime ()
ȡ
.\"
.SS timerfd_gettime()
.BR timerfd_gettime ()
ϡեǥץ
.I fd
ǻȤ륿ޡθߤ꤬ä
.I itimerspec
¤Τ
.I curr_value
˳Ǽ֤
.I it_value
եɤϡޡλޤǤλĤ֤֤
ι¤ΤξΥեɤ 0 ǤСޡϸߤƤ롣
ޡ
.B TFD_TIMER_ABSTIME
ե饰ꤵ줿˴ؤ餺ΥեɤϾͤǼ롣
.I it_interval
եɤϡޡδֳ֤֤
ι¤ΤξΥեɤ 0 ǤСޡ
.I new_value.it_value
ǻꤵ줿ָ˰λߤ褦ꤵƤ롣
.SS ޡեǥץФ
.BR timerfd_create ()
֤եǥץϰʲݡȤƤ롣
.TP
.BR read (2)
.BR timerfd_settime ()
Ȥäƥޡ꤬Ǹѹưʹߡޤ
.BR read (2)
θƤӽФ˺ǸưʹߤˡޡλʾȯƤС
.BR read (2)
Ϥ줿Хåեˡޡλ 8 ХȤ unsigned
.RI ( uint64_t )
֤
(֤ͤϥۥȥХȥĤޤꤽΥۥȥޥˤ
̾ΥХȥǤ)
.IP
.BR read (2)
ԤäǥޡλȯƤʤС
.BR read (2)
(block) 롢⤷ϥեǥץ
(non-blocking) ꤵƤϥ顼
.B EAGAIN
ǼԤ (ߥ⡼ɤˤˤϡ
.BR fcntl (2)
.B F_SETFL
̿
.B O_NONBLOCK
ե饰åȤ)
.IP
Ϥ줿ХåեΥ 8 Х̤ξ硢
.BR read (2)
ϥ顼
.B EINVAL
ǼԤ롣
.TP
.BR poll "(2), " select "(2) (Ʊͤ)"
İʾΥޡλȯƤС
եǥץɤ߽ФǽȤʤ
.RB ( select (2)
.I readfds
.BR poll (2)
.B POLLIN
ե饰)
.IP
Υեǥץϡ¾Υեǥץ¿ API Ǥ
.BR pselect (2),
.BR ppoll (2),
.BR epoll (7)
⥵ݡȤƤ롣
.TP
.BR close (2)
եǥץʹߤɬפʤʤäݤˤϡ٤Ǥ롣
Ʊ timer ֥Ȥ˴Ϣդ줿եǥץ
ȡΥޡϲ졢
Υ֥Ѥλͥˤ롣
.\"
.SS fork(2) Ǥΰ
.BR fork (2)
Ԥȡҥץ
.BR timerfd_create ()
ˤ줿եǥץΥԡ
Ѿ롣Υեǥץϡƥץб
եǥץƱޡ֥ȤȤƤꡢ
ҥץ
.BR read (2)
ǤƱޡλ˴ؤ֤롣
.\"
.SS execve(2) Ǥΰ
.BR execve (2)
.BR timerfd_create ()
ˤ줿եǥץݻ졢
ޡϤƤˤϥޡλȯ³롣
.SH ֤
ȡ
.BR timerfd_create ()
Ͽեǥץ֤
顼ξ硢\-1 ֤
.I errno
˥顼ͤꤹ롣
.BR timerfd_settime ()
.BR timerfd_gettime ()
0 ֤
顼ξ硢\-1 ֤
.I errno
˥顼ͤꤹ롣
.SH 顼
.BR timerfd_create ()
ϰʲΥ顼ǼԤǽ롣
.TP
.B EINVAL
.I clockid
.B CLOCK_MONOTONIC
Ǥ
.B CLOCK_REALTIME
Ǥʤ
.TP
.B EINVAL
.I flags
̵Ǥ롣⤷ϡLinux 2.6.26 ξ
.I flags
0 ʳǤ롣
.TP
.B EMFILE
ץѤߤΥեǥץοץξ¤
ãƤ
.TP
.B ENFILE
ץѤߤΥեƥΤξ¤ãƤ
.TP
.B ENODEV
(ͥ) ̵̾ inode ǥХޥȤǤʤä
.TP
.B ENOMEM
ޡΤ˽ʬʥ꤬ͥʤä
.PP
.BR timerfd_settime ()
.BR timerfd_gettime ()
ϰʲΥ顼ǼԤǽ롣
.TP
.B EBADF
.I fd
ͭʥեǥץǤʤ
.TP
.B EFAULT
.IR new_value ,
.IR old_value ,
.I curr_value
ͭʥݥǤϤʤ
.TP
.B EINVAL
.I fd
ͭ timerfd եǥץǤʤ
.I new_value
Ŭڤ˽Ƥʤ
.RI ( tv_nsec
ΰĤ 0 999,999,999 ޤǤϰϤäƤʤ)
.SH С
Υƥॳϥͥ 2.6.25 ʹߤ Linux ѲǽǤ롣
饤֥¦ΥݡȤϥС 2.8 ʹߤ glibc Ƥ롣
.SH
Υƥॳ Linux ͭǤ롣
.SH
ʲΥץϡޡοĽ˥ΤǤ롣
ΥץϺ 3 ĤΥޥɥ饤ꡢ
Ǥϥޡλ֤ν (ÿñ)
Ǥϥޡδֳ (ÿñ)
軰Ǥϥޡλץबλ뤫ꤹ롣
軰ϾάǽǤ롣
ʲΥΥåϤΥץλΤǤ롣
.in +4n
.nf
.RB "$" " a.out 3 1 100"
0.000: timer started
3.000: read: 1; total=1
4.000: read: 1; total=2
.BR "^Z " " # type control-Z to suspend the program"
[1]+ Stopped ./timerfd3_demo 3 1 100
.RB "$ " "fg" " # Resume execution after a few seconds"
a.out 3 1 100
9.660: read: 5; total=7
10.000: read: 1; total=8
11.000: read: 1; total=9
.BR "^C " " # type control-C to suspend the program"
.fi
.in
.SS ץΥ
\&
.nf
.\" The commented out code here is what we currently need until
.\" the required stuff is in glibc
.\"
.\"
.\"/* Link with -lrt */
.\"#define _GNU_SOURCE
.\"#include <sys/syscall.h>
.\"#include <unistd.h>
.\"#include <time.h>
.\"#if defined(__i386__)
.\"#define __NR_timerfd_create 322
.\"#define __NR_timerfd_settime 325
.\"#define __NR_timerfd_gettime 326
.\"#endif
.\"
.\"static int
.\"timerfd_create(int clockid, int flags)
.\"{
.\" return syscall(__NR_timerfd_create, clockid, flags);
.\"}
.\"
.\"static int
.\"timerfd_settime(int fd, int flags, struct itimerspec *new_value,
.\" struct itimerspec *curr_value)
.\"{
.\" return syscall(__NR_timerfd_settime, fd, flags, new_value,
.\" curr_value);
.\"}
.\"
.\"static int
.\"timerfd_gettime(int fd, struct itimerspec *curr_value)
.\"{
.\" return syscall(__NR_timerfd_gettime, fd, curr_value);
.\"}
.\"
.\"#define TFD_TIMER_ABSTIME (1 << 0)
.\"
.\"////////////////////////////////////////////////////////////
#include <sys/timerfd.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h> /* Definition of uint64_t */
#define handle_error(msg) \\
do { perror(msg); exit(EXIT_FAILURE); } while (0)
static void
print_elapsed_time(void)
{
static struct timespec start;
struct timespec curr;
static int first_call = 1;
int secs, nsecs;
if (first_call) {
first_call = 0;
if (clock_gettime(CLOCK_MONOTONIC, &start) == \-1)
handle_error("clock_gettime");
}
if (clock_gettime(CLOCK_MONOTONIC, &curr) == \-1)
handle_error("clock_gettime");
secs = curr.tv_sec \- start.tv_sec;
nsecs = curr.tv_nsec \- start.tv_nsec;
if (nsecs < 0) {
secs\-\-;
nsecs += 1000000000;
}
printf("%d.%03d: ", secs, (nsecs + 500000) / 1000000);
}
int
main(int argc, char *argv[])
{
struct itimerspec new_value;
int max_exp, fd;
struct timespec now;
uint64_t exp, tot_exp;
ssize_t s;
if ((argc != 2) && (argc != 4)) {
fprintf(stderr, "%s init\-secs [interval\-secs max\-exp]\\n",
argv[0]);
exit(EXIT_FAILURE);
}
if (clock_gettime(CLOCK_REALTIME, &now) == \-1)
handle_error("clock_gettime");
/* Create a CLOCK_REALTIME absolute timer with initial
expiration and interval as specified in command line */
new_value.it_value.tv_sec = now.tv_sec + atoi(argv[1]);
new_value.it_value.tv_nsec = now.tv_nsec;
if (argc == 2) {
new_value.it_interval.tv_sec = 0;
max_exp = 1;
} else {
new_value.it_interval.tv_sec = atoi(argv[2]);
max_exp = atoi(argv[3]);
}
new_value.it_interval.tv_nsec = 0;
fd = timerfd_create(CLOCK_REALTIME, 0);
if (fd == \-1)
handle_error("timerfd_create");
if (timerfd_settime(fd, TFD_TIMER_ABSTIME, &new_value, NULL) == \-1)
handle_error("timerfd_settime");
print_elapsed_time();
printf("timer started\\n");
for (tot_exp = 0; tot_exp < max_exp;) {
s = read(fd, &exp, sizeof(uint64_t));
if (s != sizeof(uint64_t))
handle_error("read");
tot_exp += exp;
print_elapsed_time();
printf("read: %llu; total=%llu\\n",
(unsigned long long) exp,
(unsigned long long) tot_exp);
}
exit(EXIT_SUCCESS);
}
.fi
.SH Х
ߤΤȤ
.\" 2.6.29
.BR timerfd_create ()
бƤ clockid μ
.BR timer_create (2)
⾯ʤ
.SH Ϣ
.BR eventfd (2),
.BR poll (2),
.BR read (2),
.BR select (2),
.BR setitimer (2),
.BR signalfd (2),
.BR timer_create (2),
.BR timer_gettime (2),
.BR timer_settime (2),
.BR epoll (7),
.BR time (7)
|