aboutsummaryrefslogtreecommitdiff
path: root/aufgabe3/monitor.c
blob: 02eee15ad60b2b173a61f7aa2b9d34b2031a86fd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
 * vim:ts=4:sw=4:expandtab
 *
 */
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

#include "queue.h"

char *message;

void monitor() {
    int random_number = 0;
    mqd_t monitor;

    /*
     * Message Queue zur Kommunikation mit dem statistic Prozess öffnen.
     */
    if ((monitor = mq_open(MQ_TO_MONITOR, O_RDONLY)) == -1) {
        perror("monitor() mq_open");
        exit(EXIT_FAILURE);
    }

    /*
     * Speicher für Nachricht allokieren.
     */
    if ((message = calloc(1, MQ_MSG_SIZE_RCV)) == NULL) {
        perror("monitor()");
        exit(EXIT_FAILURE);
    }

    for (;;) {
        /* Nachricht über monitor Queue vom statistic Prozess empfangen */
        if (mq_receive(monitor, message, MQ_MSG_SIZE_RCV, NULL) == -1) {
            perror("monitor() mq_receive");
            exit(EXIT_FAILURE);
        }

        /* Zufallszahl aus dem String zurück zur Dezimalzahl konvertieren */
        sscanf(message, "%x", &random_number);

        /* Die konvertierte Zahl auf der stdout ausgeben */
        printf("%d\n", random_number);
    }
}

void monitor_cleanup() {
    printf("monitor cleanup\n");
    free(message);
    mq_unlink(MQ_TO_MONITOR);
    _exit(EXIT_SUCCESS);
}