From 429b38beed0cbdda3bb84aa8322908fff9b4b8c6 Mon Sep 17 00:00:00 2001 From: Thorsten Töpper Date: Mon, 3 Jan 2011 01:22:22 +0100 Subject: Aufgabe 3: log.c Funktionen umbenannt und implementiert. --- aufgabe3/log.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 4 deletions(-) (limited to 'aufgabe3/log.c') 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 #include #include -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); } -- cgit v1.2.3-70-g09d2