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/conv.c | 10 ++++------ aufgabe2/log.c | 4 ++-- aufgabe2/statistic.c | 41 +++++++++++++++++++++++++++++++++++++++-- 3 files changed, 45 insertions(+), 10 deletions(-) (limited to 'aufgabe2') diff --git a/aufgabe2/conv.c b/aufgabe2/conv.c index a28f120..1c2e1b5 100644 --- a/aufgabe2/conv.c +++ b/aufgabe2/conv.c @@ -5,7 +5,6 @@ #include #include #include -#include #include "queue.h" @@ -15,6 +14,8 @@ FILE *pipe_stat; void conv() { int random_number = 0; + printf("conv started\n"); + /* Die beiden Pipes zum schreiben öffnen */ if (((pipe_log = fdopen(queue[D_CONV_TO_LOG][WRITE], "w")) == NULL) || ((pipe_stat = fdopen(queue[D_CONV_TO_STAT][WRITE], "w")) == NULL)) { @@ -23,11 +24,8 @@ void conv() { } for (;;) { - /* Generierung der Zufallszahl, um Fehler im - * statistic Prozess zu vermeiden mit geringerem - * Wertebereich [0, SHRT_MAX] (quasi als short). - */ - random_number = rand()%SHRT_MAX; + /* Generierung der Zufallszahl */ + random_number = rand(); /* Nachricht sowohl in die Queue zum log Prozess als auch * in die Queue zum statistic Prozess schreiben. diff --git a/aufgabe2/log.c b/aufgabe2/log.c index 96b2a4f..14684ce 100644 --- a/aufgabe2/log.c +++ b/aufgabe2/log.c @@ -13,7 +13,6 @@ FILE *logfile; char *message; void log() { - int buffer; printf("log started\n"); /* Die Pipe zum lesen öffnen */ @@ -36,7 +35,8 @@ void log() { /* Nachricht aus der Pipe lesen, als Endian abhängiger * HEX-Wert in die Log Datei schreiben. Für portable - * Rückkonvertierung ins Dezimalsystem siehe Aufgabe 3. + * Rückkonvertierung ins Dezimalsystem siehe Aufgabe 3, + * oder bei Aufgabe 2 in statistic.c und monitor.c */ while (fgets(message, SIZE_HEX+1, pipe_conv)) { fputs(message, logfile); 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