/* example of a timer */ /* sets a timer that triggers every 10 seconds for 4 times */ /* ECE 264 Yung-Hsiang Lu, Purdue Spring 2009 */ #include #include #include #include int handleCounter = 0; struct itimerval tval; void handleTimer(int v) { printf("handleTimer\n"); handleCounter ++; if (handleCounter < 4) { setitimer(ITIMER_REAL, &tval,0); signal(SIGALRM, handleTimer); } } int main(int argc, char * argv []) { FILE * fptr = NULL; int curX = 0; int curY = 0; int ID = 0; int width = 0; int height = 0; char oneLine[1024]; /* should be long enough */ if (argc < 2) { printf("need the input file\n"); return -1; } fptr = fopen(argv[1], "r"); if (fptr == NULL) { printf("cannot open file\n"); return -1; } while (feof(fptr) == 0) { /* reset */ strcpy(oneLine, ""); fgets(oneLine, 1023, fptr); oneLine[1023] = '\0'; /* terminate the string */ if (sscanf(oneLine, "%d %d %d", & ID, & width, & height) == 3) { /* ignore area */ printf("%d %d %d 0\n", ID, curX, curY); /* never rotate */ curX += (width * 2); curY += (height * 2); } } fclose (fptr); tval.it_interval.tv_sec = 0; tval.it_interval.tv_usec = 0; tval.it_value.tv_sec = 10; /* wait for 10 seconds */ tval.it_value.tv_usec = 0; setitimer(ITIMER_REAL, &tval,0); signal(SIGALRM, handleTimer); while (handleCounter < 4) { /* wait for the timer */ } return 0; }