From 24ce486813103f5f8037b4386a53fa11d1632253 Mon Sep 17 00:00:00 2001 From: Thorsten Töpper Date: Mon, 10 Jan 2011 20:45:59 +0100 Subject: Aufgabe 2: statistic implementiert, kleinere Schönheitskorrekturen. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- aufgabe2/statistic.c | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) (limited to 'aufgabe2/statistic.c') diff --git a/aufgabe2/statistic.c b/aufgabe2/statistic.c index b83776a..31230cf 100644 --- a/aufgabe2/statistic.c +++ b/aufgabe2/statistic.c @@ -1,18 +1,55 @@ /* * vim:ts=4:sw=4:expandtab - * + * */ #include #include #include +#include "queue.h" + +FILE *pipe_conv; +FILE *pipe_mon; +char *message; + void statistic() { + int current_value = 0, last_value = 0, result; + printf("statistic started\n"); - for (;;) { + + /* Die Pipe zum conv Prozess lesend öffnen, die zum monitor schreibend */ + if (((pipe_conv = fdopen(queue[D_CONV_TO_STAT][READ], "r")) == NULL) || + ((pipe_mon = fdopen(queue[D_STAT_TO_MON][WRITE], "w")) == NULL)) { + perror("statistic.c fdopen()"); + exit(EXIT_FAILURE); + } + + /* Speicher für Nachricht reservieren */ + if ((message = calloc(1, sizeof(char)*SIZE_HEX+1)) == NULL) { + perror("statistic.c calloc()"); + exit(EXIT_FAILURE); + } + + /* + * Auslesen der Nachrichten vom conv Prozess, Hälfte des Wertest mit der + * des letzten Wert addieren und an den monitor Prozess schicken. + */ + while (fgets(message, SIZE_HEX+1, pipe_conv)) { + sscanf(message, "%x\n", ¤t_value); /* HEX -> DEC */ + + result = current_value/2 + last_value/2; + fprintf(pipe_mon, "%x\n", result); + + last_value = current_value; } } void statistic_cleanup() { printf("statistic cleanup\n"); + fclose(pipe_conv); + fclose(pipe_mon); + free(message); + close(queue[D_CONV_TO_STAT][READ]); + close(queue[D_STAT_TO_MON][WRITE]); _exit(EXIT_SUCCESS); } -- cgit v1.2.3-70-g09d2