#include <stdio.h> /* standard I/O library */
#include <string.h> /* C strings library */
#include <sys/timerfd.h> /* timerfd */
+#include <sys/epoll.h> /* Linux epoll interface */
// #include <time.h>
+#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;
+ pid_t pid;
+ struct epoll_event epev, events[MAX_EPOLL_EV];
struct itimerspec expiring;
+ uint64_t expirated;
/*
* Input section: decode command line parameters
* Use getopt function
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");
+ if (timerfd_settime(fd, 0, &expiring, NULL)) {
+ die("Cannot set timer");
}
+ 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) {
+ 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");
+ }
+ }
+ }
+ }
+ // }
return 0;
}
/*
exit(1);
}
+/*
+ * Print error message and exit routine
+ */
+void die(char * mess) {
+ perror(mess);
+ exit(-1);
+}