text
stringlengths
0
234
.\"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 <inttypes.h> /* definition of priu64 */
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h> /* definition of uint64_t */
#define handle_error(msg) \e
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]\en",
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\en");