write assgn7
authorkremlin <ian@kremlin.cc>
Wed, 29 Oct 2014 03:00:26 +0000 (23:00 -0400)
committerkremlin <ian@kremlin.cc>
Wed, 29 Oct 2014 03:00:26 +0000 (23:00 -0400)
assgn7/Makefile [new file with mode: 0644]
assgn7/timer.c [new file with mode: 0644]

diff --git a/assgn7/Makefile b/assgn7/Makefile
new file mode 100644 (file)
index 0000000..2dd17ea
--- /dev/null
@@ -0,0 +1,12 @@
+.PHONY: all
+
+CC=/usr/bin/gcc
+CARGS=-Wall -Werror -Wextra -pedantic -Wno-unused-parameter -Wno-unused -std=gnu99
+SRC=timer.c -o timer
+
+all:
+       $(CC) $(CARGS) $(SRC) 
+
+debug:
+       $(CC) $(CARGS) $(SRC) -g -O0
+
diff --git a/assgn7/timer.c b/assgn7/timer.c
new file mode 100644 (file)
index 0000000..6d35ff5
--- /dev/null
@@ -0,0 +1,67 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <signal.h>
+#include <time.h>
+
+struct timespec *start_time, *stop_time;
+int done;
+
+void handle_start() {
+
+       if(clock_gettime(CLOCK_MONOTONIC, start_time) == -1) {
+
+               printf("failed to get start time.\n");
+               exit(1);
+       }
+
+       pause();
+}
+
+void handle_stop() {
+
+       long int start_sec, stop_sec, delta;
+
+       if(clock_gettime(CLOCK_MONOTONIC, stop_time) == -1) {
+
+               printf("failed to get stop time.\n");
+               exit(1);
+       }
+
+       start_sec = ((long int) start_time->tv_sec);
+       stop_sec  = ((long int) stop_time->tv_sec);
+
+       delta = stop_sec - start_sec;
+
+       printf("%ld seconds elapsed between signals.\n", delta);
+
+       done = 1;
+}
+
+int main(int argc, char *argv[]) {
+
+       done = 0;
+
+       start_time = (struct timespec *) calloc(1, sizeof(struct timespec));
+       stop_time  = (struct timespec *) calloc(1, sizeof(struct timespec));
+
+       if(signal(SIGTSTP, handle_start) == SIG_ERR ||
+          signal(SIGCONT, handle_stop)  == SIG_ERR) {
+
+               printf("could not bind signal handlers\n");
+               return 1;
+       } 
+
+       terminate_check:
+
+       pause();
+
+       if(!done)
+               goto terminate_check;
+
+       free(start_time);
+       free(stop_time);
+
+       return 0;
+}