From 7d28c089b45d465b18df1c5cda72abed3748118f Mon Sep 17 00:00:00 2001 From: Thorsten Töpper Date: Mon, 10 Jan 2011 20:02:33 +0100 Subject: Aufgabe 2: main.c, log.c und conv.c ausgebaut. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * main.c: Öffnen der Pipes * conv.c: Pseudozufallszahlen, schreiben in die Pipes zum log und statistic Prozess * log.c: log() implementiert --- aufgabe2/conv.c | 33 +++++++++++++++++++++++++++++++-- aufgabe2/log.c | 39 +++++++++++++++++++++++++++++++++++++-- aufgabe2/main.c | 17 +++++++++++++++-- 3 files changed, 83 insertions(+), 6 deletions(-) (limited to 'aufgabe2') diff --git a/aufgabe2/conv.c b/aufgabe2/conv.c index efa8200..a28f120 100644 --- a/aufgabe2/conv.c +++ b/aufgabe2/conv.c @@ -1,18 +1,47 @@ /* * vim:ts=4:sw=4:expandtab - * + * */ #include #include #include +#include + +#include "queue.h" + +FILE *pipe_log; +FILE *pipe_stat; void conv() { - printf("conv started\n"); + int random_number = 0; + + /* 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)) { + perror("conv.c fdopen()"); + exit(EXIT_FAILURE); + } + 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; + + /* Nachricht sowohl in die Queue zum log Prozess als auch + * in die Queue zum statistic Prozess schreiben. + */ + fprintf(pipe_log, "%x\n", random_number); + fprintf(pipe_stat, "%x\n", random_number); } } void conv_cleanup() { printf("conv cleanup\n"); + fclose(pipe_log); + fclose(pipe_stat); + close(queue[D_CONV_TO_LOG][WRITE]); + close(queue[D_CONV_TO_STAT][WRITE]); _exit(EXIT_SUCCESS); } diff --git a/aufgabe2/log.c b/aufgabe2/log.c index 405cdbd..96b2a4f 100644 --- a/aufgabe2/log.c +++ b/aufgabe2/log.c @@ -1,18 +1,53 @@ /* * vim:ts=4:sw=4:expandtab - * + * */ #include #include #include +#include "queue.h" + +FILE *pipe_conv; +FILE *logfile; +char *message; + void log() { + int buffer; printf("log started\n"); - for (;;) { + + /* Die Pipe zum lesen öffnen */ + if ((pipe_conv = fdopen(queue[D_CONV_TO_LOG][READ], "r")) == NULL) { + perror("log.c fdopen()"); + exit(EXIT_FAILURE); + } + + /* Logdatei öffnen */ + if ((logfile = fopen("logfile.txt", "w")) == NULL) { + perror("log.c fopen()"); + exit(EXIT_FAILURE); + } + + /* Speicher für Nachricht reservieren */ + if ((message = calloc(1, sizeof(char)*SIZE_HEX+1)) == NULL) { + perror("log.c calloc()"); + exit(EXIT_FAILURE); + } + + /* 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. + */ + while (fgets(message, SIZE_HEX+1, pipe_conv)) { + fputs(message, logfile); } } void log_cleanup() { printf("log cleanup\n"); + fclose(logfile); + fclose(pipe_conv); + free(message); + close(queue[D_CONV_TO_LOG][READ]); _exit(EXIT_SUCCESS); } diff --git a/aufgabe2/main.c b/aufgabe2/main.c index f876f54..1771a26 100644 --- a/aufgabe2/main.c +++ b/aufgabe2/main.c @@ -1,6 +1,6 @@ /* * vim:ts=4:sw=4:expandtab - * + * */ #include #include @@ -9,6 +9,7 @@ #include #include +#include "queue.h" #include "conv.h" #include "log.h" #include "monitor.h" @@ -50,7 +51,7 @@ pid_t fork_child(funcptr work, funcptr cleanup) { * die Prozessfunktion doch zurückkehrt. */ exit(EXIT_FAILURE); } - + if (pid == -1) { /* error */ perror("fork()"); @@ -69,6 +70,17 @@ pid_t fork_child(funcptr work, funcptr cleanup) { * */ int main() { + + /* + * Öffnen der Pipes. + */ + if ((pipe(queue[D_CONV_TO_LOG])!=0) || (pipe(queue[D_CONV_TO_STAT])!=0) || + (pipe(queue[D_STAT_TO_MON])!=0)) { + perror("main.c pipe()"); + exit(EXIT_FAILURE); + } + + pconv = fork_child(conv, conv_cleanup); plog = fork_child(log, log_cleanup); pstatistic = fork_child(statistic, statistic_cleanup); @@ -84,3 +96,4 @@ int main() { return 0; } + -- cgit v1.2.3-70-g09d2