Merge branch 'master' of ssh://gapil.gnulinux.it/srv/git/gapil
[gapil.git] / sources / test_timerfdfork.c
index 34ceaedd1b317c0d29bf31397a2a7e5718ad54f0..19d37b680ae7494b7b89749c0271bc4b8151e222 100644 (file)
@@ -38,7 +38,7 @@
 #include <string.h>      /* C strings library */
 #include <sys/timerfd.h> /* timerfd */
 #include <sys/epoll.h>   /* Linux epoll interface */
-// #include <time.h>
+#include <time.h>
 
 #include "macros.h"
 #include "Gapil.h"
@@ -55,16 +55,17 @@ int main(int argc, char *argv[])
  * Variables definition  
  */
     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 expirated;
+    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 
@@ -74,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();
@@ -95,13 +108,14 @@ int main(int argc, char *argv[])
     }
     /* 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;
+    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)
@@ -111,35 +125,38 @@ int main(int argc, char *argv[])
     if (epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &epev))
        die("Failing in epoll_ctl"); 
     /* main loop */
-    //while (1) {
-        if ((n=epoll_wait(epfd, events, MAX_EPOLL_EV, -1)) < 0) 
-           die("error on epoll_wait");
-       debug("Got %i events\n", n);
-       /* loop on epoll events */
-       for (i=0; i<n; i++) {
-           if (events[i].data.fd == fd) {             // if timer expired
-               printf("Timer expired:\n");
-               while(nread=read(fd, &expirated, sizeof(expirated))) {
-                   if (nread < 0) {
-                       if (errno != EAGAIN) 
-                           die("signalfd read error");
-                       else 
-                           break;
-                   }
-                   if (nread != sizeof(expirated)) {
-                       printf("Error on timer data read, '\n");
-                       continue;
-                   }
-                   printf("Expired %llu times\n", expirated);
-                   if (pid == 0) {
-                       printf("in child\n");
-                   } else {
-                       printf("in father\n");
+    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<n; i++) {
+                   if (events[i].data.fd == fd) {   // if timer expired
+                       printf("Timer expired in pid %i:\n", pid);
+                       while(nread=read(fd, &expired, sizeof(expired))) {
+                           if (nread < 0) {
+                               if (errno != EAGAIN) 
+                                   die("signalfd read error");
+                               else 
+                                   break;
+                           }
+                           if (nread != sizeof(expired)) {
+                               printf("Error on timer data read, '\n");
+                               continue;
+                           }
+                           printf("Expired %llu times in pid %i\n", 
+                                  expired, pid);
+                       }
                    }
                }
            }
        }
-    // }
+    }
     return 0;
 }
 /*
@@ -149,6 +166,8 @@ void usage(void) {
     printf("Program testtimerfdfork : test timerfd across fork  \n");
     printf("Usage:\n");
     printf("  testtimerfdfork [-h]  \n");
+    printf("  -i sec      interval for repetition\n");
+    printf("  -t sec      time to wait to expire\n");
     printf("  -h          print this help\n");
     
     exit(1);