From 561079b325265af54c3d1d897863f21fa641e471 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Sat, 25 Nov 2017 01:31:39 -0500 Subject: CS3871/assignments/sync: Fixed arithmatic error in reader/writer Fixed assorted bugs Added makefile --- sync/reader.c | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) (limited to 'sync/reader.c') diff --git a/sync/reader.c b/sync/reader.c index 2870d15..66ee57b 100644 --- a/sync/reader.c +++ b/sync/reader.c @@ -1,33 +1,40 @@ -#include +#include +#include +#include +#include #include +#include #include -#include -#include -#include +#include #include -#include +#include + +char *mem; +void quit(signum) +int signum; +{ + shmdt(mem); + exit(1); +} int main(argc, argv) int argc; char **argv; { +printf("\tR STARTING\n"); int shmid, i, pid, id; - char *mem, filename[50]; + char filename[50]; FILE *fd; - struct timeval *s; - if (argc != 1) { + if (argc < 2) { printf("usage: reader [id]\n"); exit(1); } id = atoi(argv[1]); +printf("\tR%d. started...\n", id); - if (argc < 2) { - printf("usage: sync [number readers] [number writers]\n"); - exit(1); - } if ((shmid = shmget(52, 1<<14, IPC_CREAT | 0666)) == -1){ perror("shmget: shmget failed"); @@ -38,9 +45,12 @@ char **argv; perror("shmat"); exit(1); } + signal(SIGQUIT, quit); +printf("\tR%d. SHMID: %d\n", id, shmid); sprintf(filename, "reader.%d", id); +printf("\tR%d. opening file \"%s\"...\n", id, filename); fd = fopen(filename, "a"); if (!fd) { @@ -50,12 +60,15 @@ char **argv; srand(time(NULL)); while (1) { - s->tv_sec = rand() % id; +printf("\tR%d. reading...\n", id); for (i = 0; i < 1<<14; i++) { fprintf(fd, "%c", *(mem + i)); fflush(fd); } + fprintf(fd, "\n"); + fflush(fd); - select(0, NULL, NULL, NULL, s); +printf("\tR%d. waiting...\n", id); + sleep(rand() % (id + 1)); } } -- cgit v1.1