diff options
| author | Thorsten Töpper <atsutane@freethoughts.de> | 2011-01-03 02:45:18 +0100 |
|---|---|---|
| committer | Thorsten Töpper <atsutane@freethoughts.de> | 2011-01-03 02:45:18 +0100 |
| commit | 7f51f84716a3a7a7633ec439e1ee93a0515a16b8 (patch) | |
| tree | ba99785df18d47995271a8319a18f5a8b074c3e1 | |
| parent | 3bc36622e01fae96154f1d63cfa94748538d6e59 (diff) | |
| download | prozesskommunikation-7f51f84716a3a7a7633ec439e1ee93a0515a16b8.tar.gz prozesskommunikation-7f51f84716a3a7a7633ec439e1ee93a0515a16b8.tar.bz2 | |
Aufgabe 3: statistic implementiert, Wertebereich der Zufallszahlen verkleinert.
| -rw-r--r-- | aufgabe3/conv.c | 9 | ||||
| -rw-r--r-- | aufgabe3/statistic.c | 75 |
2 files changed, 80 insertions, 4 deletions
diff --git a/aufgabe3/conv.c b/aufgabe3/conv.c index 6f4890c..28c5d91 100644 --- a/aufgabe3/conv.c +++ b/aufgabe3/conv.c @@ -6,6 +6,7 @@ #include <stdio.h> #include <unistd.h> #include <string.h> +#include <limits.h> #include "queue.h" @@ -37,7 +38,11 @@ void conv() { } for (;;) { - random_number = rand(); + /* Generierung der Zufallszahl, um Fehler im + * statistic Prozess zu vermeiden mit geringerem + * Wertebereich [0, SHRT_MAX] (quasi als short). + */ + random_number = rand()%SHRT_MAX; /* Zufallszahl ins Hexadezimalformat konvertieren. */ sprintf(message, "%x", random_number); @@ -46,7 +51,7 @@ void conv() { * in die Queue zum statistic Prozess schreiben. */ if ((mq_send(log, message, MQ_MSG_SIZE_SEND, 0) == -1) || - mq_send(statistic, message, MQ_MSG_SIZE_SEND, 0) == -1){ + mq_send(statistic, message, MQ_MSG_SIZE_SEND, 0) == -1) { perror("conv() mq_send"); exit(EXIT_FAILURE); } diff --git a/aufgabe3/statistic.c b/aufgabe3/statistic.c index b83776a..ef4e1bc 100644 --- a/aufgabe3/statistic.c +++ b/aufgabe3/statistic.c @@ -1,18 +1,89 @@ /* * vim:ts=4:sw=4:expandtab - * + * */ #include <stdlib.h> #include <stdio.h> #include <unistd.h> +#include "queue.h" + +#define VALUES_STORED 20 + +char *message; + void statistic() { - printf("statistic started\n"); + int random_number = 0, values[VALUES_STORED]; + int i = 0, mi=0; /* Indexe für das Array und die Mittelwertsberechnung */ + int mittelwert = 0; + mqd_t statistic, monitor; + + /* + * Message Queue zur Kommunikation mit dem conv Prozess öffnen. + */ + if ((statistic = mq_open(MQ_TO_STATISTIC, O_RDONLY)) == -1) { + perror("statistic() mq_open(MQ_TO_STATISTIC)"); + exit(EXIT_FAILURE); + } + + /* + * Message Queue zur Kommunikation mit dem monitor Prozess öffnen. + */ + if ((monitor = mq_open(MQ_TO_MONITOR, O_WRONLY)) == -1) { + perror("statistic() mq_open(MQ_TO_MONITOR)"); + exit(EXIT_FAILURE); + } + + /* + * Speicher für Nachricht allokieren. + */ + if ((message = calloc(1, MQ_MSG_SIZE_RCV)) == NULL) { + perror("statistic()"); + exit(EXIT_FAILURE); + } + for (;;) { + /* Nachricht empfangen */ + if (mq_receive(statistic, message, MQ_MSG_SIZE_RCV, NULL) == -1) { + perror("statistic() mq_receive"); + exit(EXIT_FAILURE); + } + + /* Zahl aus dem String zurück zur Dezimalzahl konvertieren */ + sscanf(message, "%x", &random_number); + + /* + * Berechnung des Mittelwertes der letzten VALUES_STORED Werten. + * Bis das Array erstmals gefüllt ist, wird + * der Wert nur näherungsweise berechnet. + */ + values[i] = random_number; + + /* Werte in mittelwert aufsummieren, unsichere Methode, + * Pufferüberlauf bei zu vielen Werten. + */ + for (mi=0; mi<VALUES_STORED; mi++) { + mittelwert += values[mi]; + } + mittelwert /= VALUES_STORED; + + /* Berechneten Wert ins Hexadezimalformat konvertieren. */ + sprintf(message, "%x", mittelwert); + + /* Nachricht sowohl in die Queue zum monitor Prozess schreiben. + */ + if ((mq_send(monitor, message, MQ_MSG_SIZE_SEND, 0)) == -1) { + perror("statistic() mq_send(MQ_TO_MONITOR)"); + exit(EXIT_FAILURE); + } + i = (i+1)%VALUES_STORED; } } void statistic_cleanup() { printf("statistic cleanup\n"); + free(message); + mq_unlink(MQ_TO_STATISTIC); + mq_unlink(MQ_TO_MONITOR); _exit(EXIT_SUCCESS); } |
