write assgn7
[assignments.git] / assgn7 / timer.c
CommitLineData
98cc79a1 1#include <stdio.h>
2#include <stdlib.h>
3#include <unistd.h>
4
5#include <signal.h>
6#include <time.h>
7
8struct timespec *start_time, *stop_time;
9int done;
10
11void handle_start() {
12
13 if(clock_gettime(CLOCK_MONOTONIC, start_time) == -1) {
14
15 printf("failed to get start time.\n");
16 exit(1);
17 }
18
19 pause();
20}
21
22void handle_stop() {
23
24 long int start_sec, stop_sec, delta;
25
26 if(clock_gettime(CLOCK_MONOTONIC, stop_time) == -1) {
27
28 printf("failed to get stop time.\n");
29 exit(1);
30 }
31
32 start_sec = ((long int) start_time->tv_sec);
33 stop_sec = ((long int) stop_time->tv_sec);
34
35 delta = stop_sec - start_sec;
36
37 printf("%ld seconds elapsed between signals.\n", delta);
38
39 done = 1;
40}
41
42int main(int argc, char *argv[]) {
43
44 done = 0;
45
46 start_time = (struct timespec *) calloc(1, sizeof(struct timespec));
47 stop_time = (struct timespec *) calloc(1, sizeof(struct timespec));
48
49 if(signal(SIGTSTP, handle_start) == SIG_ERR ||
50 signal(SIGCONT, handle_stop) == SIG_ERR) {
51
52 printf("could not bind signal handlers\n");
53 return 1;
54 }
55
56 terminate_check:
57
58 pause();
59
60 if(!done)
61 goto terminate_check;
62
63 free(start_time);
64 free(stop_time);
65
66 return 0;
67}