Materiale su timerfd
[gapil.git] / sources / test_timerfdfork.c
1 /* test_timerfdfork.c
2  * 
3  * Copyright (C) 2011 Simone Piccardi
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or (at
8  * your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 /****************************************************************
20  *
21  * Program test_timerfdfork.c: 
22  * Program to test timerfd behaviour across fork's
23  *
24  * Author: Simone Piccardi
25  * Apr. 2011
26  *
27  * Usage: testtimerfdfork -h give all info's
28  *
29  ****************************************************************/
30 /* 
31  * Include needed headers
32  */
33 #define _GNU_SOURCE
34 #include <errno.h>       /* error definitions and routines */ 
35 #include <stdlib.h>      /* C standard library */
36 #include <unistd.h>      /* unix standard library */
37 #include <stdio.h>       /* standard I/O library */
38 #include <string.h>      /* C strings library */
39 #include <sys/timerfd.h> /* timerfd */
40 // #include <time.h>
41
42 /* Help printing routine */
43 void usage(void);
44
45 int main(int argc, char *argv[])
46 {
47 /* 
48  * Variables definition  
49  */
50     int i, fd;
51     struct itimerspec expiring;
52     /*
53      * Input section: decode command line parameters 
54      * Use getopt function
55      */
56     opterr = 0;  /* don't want writing to stderr */
57     while ( (i = getopt(argc, argv, "h")) != -1) {
58         switch (i) {
59         /* 
60          * Handling options 
61          */ 
62         case 'h':   /* help option */
63             printf("Wrong -h option use\n");
64             usage();
65             return -1;
66             break;
67         case '?':   /* unrecognized options */
68             printf("Unrecognized options -%c\n",optopt);
69             usage();
70         default:    /* should not reached */
71             usage();
72         }
73     }
74     /* ***********************************************************
75      * 
76      *           Options processing completed
77      *
78      *                Main code beginning
79      * 
80      * ***********************************************************/
81     /* There must be 0 remaing arguments */
82     if ( (argc-optind) != 0 )  {
83         printf("From %d arguments, removed %d options\n", argc, optind);
84         usage();
85     }
86
87     fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
88     expiring.it_interval.tv_sec=1;
89     expiring.it_interval.tv_nsec=0;
90     expiring.it_value.tv_sec=5;
91     expiring.it_value.tv_nsec=0;
92     if (timerfd_settime(fd, 0, expiring, NULL)) {
93         perror("Cannot set timer");
94     }
95     return 0;
96 }
97 /*
98  * routine to print usage info and exit
99  */
100 void usage(void) {
101     printf("Program testtimerfdfork : test timerfd across fork  \n");
102     printf("Usage:\n");
103     printf("  testtimerfdfork [-h]  \n");
104     printf("  -h           print this help\n");
105     
106     exit(1);
107 }
108