X-Git-Url: https://gapil.gnulinux.it/gitweb/?p=gapil.git;a=blobdiff_plain;f=sources%2Ftest_timerfdfork.c;h=19d37b680ae7494b7b89749c0271bc4b8151e222;hp=04fad750973f0726300a3e987fc530d5fc59e7fb;hb=265547995607b3ec2c04f9b8b035b416e892920b;hpb=bd8c369514b9f49a58fe22c095f5313a521e6ee2 diff --git a/sources/test_timerfdfork.c b/sources/test_timerfdfork.c index 04fad75..19d37b6 100644 --- a/sources/test_timerfdfork.c +++ b/sources/test_timerfdfork.c @@ -37,24 +37,35 @@ #include /* standard I/O library */ #include /* C strings library */ #include /* timerfd */ -// #include +#include /* Linux epoll interface */ +#include + +#include "macros.h" +#include "Gapil.h" /* Help printing routine */ void usage(void); +void die(char *); + +#define MAX_EPOLL_EV 10 int main(int argc, char *argv[]) { /* * Variables definition */ - int i, fd; + int i, n, nread, fd, epfd; + int wait=5, interval=1, nswait=0, nsinter=0; // timer default + pid_t pid; + struct epoll_event epev, events[MAX_EPOLL_EV]; struct itimerspec expiring; + uint64_t expired; /* * Input section: decode command line parameters * Use getopt function */ opterr = 0; /* don't want writing to stderr */ - while ( (i = getopt(argc, argv, "h")) != -1) { + while ( (i = getopt(argc, argv, "ht:i:w:n:")) != -1) { switch (i) { /* * Handling options @@ -64,6 +75,18 @@ int main(int argc, char *argv[]) usage(); return -1; break; + case 'i': /* timer interval */ + interval = strtol(optarg, NULL, 10); /* convert input */ + break; + case 't': /* timer expiring */ + wait = strtol(optarg, NULL, 10); /* convert input */ + break; + case 'n': /* timer interval in ns */ + nsinter = strtol(optarg, NULL, 10); /* convert input */ + break; + case 'w': /* timer expiring in ns */ + nswait = strtol(optarg, NULL, 10); /* convert input */ + break; case '?': /* unrecognized options */ printf("Unrecognized options -%c\n",optopt); usage(); @@ -83,14 +106,56 @@ int main(int argc, char *argv[]) printf("From %d arguments, removed %d options\n", argc, optind); usage(); } - + /* timerfd setup */ fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK); - expiring.it_interval.tv_sec=1; - expiring.it_interval.tv_nsec=0; - expiring.it_value.tv_sec=5; - expiring.it_value.tv_nsec=0; - if (timerfd_settime(fd, 0, expiring, NULL)) { - perror("Cannot set timer"); + expiring.it_interval.tv_sec=interval; + expiring.it_interval.tv_nsec=nsinter; + expiring.it_value.tv_sec=wait; + expiring.it_value.tv_nsec=nswait; + if (timerfd_settime(fd, 0, &expiring, NULL)) { + die("Cannot set timer"); + } + printf("Timer interval %i sec, timer time %i sec\n", wait, interval); + pid = fork(); + /* epoll setup */ + if ((epfd=epoll_create(5)) < 0) + die("Failing on epoll_create"); + epev.data.fd = fd; + epev.events = EPOLLIN; + if (epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &epev)) + die("Failing in epoll_ctl"); + /* main loop */ + while (1) { + while (n=epoll_wait(epfd, events, MAX_EPOLL_EV, -1)) { + if (n < 0) { + if (errno != EAGAIN) + die("error on epoll_wait"); + else + continue; + } else { + printf("Got %i events, pid %i, time %i\n", n, pid, time(NULL)); + /* loop on epoll events */ + for (i=0; i