diff options
| author | Thorsten Töpper <atsutane@freethoughts.de> | 2011-01-03 01:22:22 +0100 |
|---|---|---|
| committer | Thorsten Töpper <atsutane@freethoughts.de> | 2011-01-03 01:22:22 +0100 |
| commit | 429b38beed0cbdda3bb84aa8322908fff9b4b8c6 (patch) | |
| tree | 7c19566e556c32a47e3ec8d41b476543893cbd4f /aufgabe3/log.c | |
| parent | 037ccb68c0a2f5462dc06c40ddaf3f7604725cfb (diff) | |
| download | prozesskommunikation-429b38beed0cbdda3bb84aa8322908fff9b4b8c6.tar.gz prozesskommunikation-429b38beed0cbdda3bb84aa8322908fff9b4b8c6.tar.bz2 | |
Aufgabe 3: log.c Funktionen umbenannt und implementiert.
Diffstat (limited to 'aufgabe3/log.c')
| -rw-r--r-- | aufgabe3/log.c | 52 |
1 files changed, 48 insertions, 4 deletions
diff --git a/aufgabe3/log.c b/aufgabe3/log.c index 405cdbd..d38adfd 100644 --- a/aufgabe3/log.c +++ b/aufgabe3/log.c @@ -1,18 +1,62 @@ /* * vim:ts=4:sw=4:expandtab - * + * */ #include <stdlib.h> #include <stdio.h> #include <unistd.h> -void log() { - printf("log started\n"); +#include "queue.h" + +FILE *logfile; +char *message; + +void logmsg() { + int random_number = 0; + mqd_t log; + + /* Message Queue zur Kommunikation mit dem conv Prozess öffnen. + */ + if ((log = mq_open(MQ_TO_LOG, O_RDWR)) == -1) { + perror("logmsg() mq_open"); + exit(EXIT_FAILURE); + } + + /* Logdatei öffnen */ + if ((logfile = fopen("logfile.txt", "w")) == NULL) { + perror("logmsg() fopen"); + exit(EXIT_FAILURE); + } + + /* + * Speicher für Nachricht allokieren. + */ + if ((message = calloc(1, MQ_MSG_SIZE_SEND)) == NULL) { + perror("logmsg()"); + exit(EXIT_FAILURE); + } + for (;;) { + + /* Nachricht über log Queue empfangen */ + if (mq_receive(log, message, MQ_MSG_SIZE_RCV, NULL) == -1) { + perror("logmsg() mq_receive"); + exit(EXIT_FAILURE); + } + + /* Zufallszahl aus dem String zurück zur Dezimalzahl konvertieren */ + sscanf(message, "%x", &random_number); + + /* Die konvertierte Zahl in die Log datei schreiben */ + fprintf(logfile, "%d\n", random_number); + } } -void log_cleanup() { +void logmsg_cleanup() { printf("log cleanup\n"); + free(message); + mq_unlink(MQ_TO_LOG); + fclose(logfile); _exit(EXIT_SUCCESS); } |
