From: kremlin Date: Wed, 29 Oct 2014 03:00:26 +0000 (-0400) Subject: write assgn7 X-Git-Url: https://uglyman.kremlin.cc/gitweb/gitweb.cgi?a=commitdiff_plain;h=98cc79a1a2aaf0c08ac817b50b0a1e923a651484;p=assignments.git write assgn7 --- diff --git a/assgn7/Makefile b/assgn7/Makefile new file mode 100644 index 0000000..2dd17ea --- /dev/null +++ b/assgn7/Makefile @@ -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 index 0000000..6d35ff5 --- /dev/null +++ b/assgn7/timer.c @@ -0,0 +1,67 @@ +#include +#include +#include + +#include +#include + +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; +}