aboutsummaryrefslogtreecommitdiff
path: root/aufgabe3/conv.c
blob: 28c5d9117f8e9d9a2200082f5676b5c427828220 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
 * vim:ts=4:sw=4:expandtab
 *
 */
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <limits.h>

#include "queue.h"

char *message;

void conv() {
    int random_number = 0;
    mqd_t log;
    mqd_t statistic;


    /*
     * Message Queues zur Kommunikation mit dem log
     * und dem statstic Prozess öffnen.
     */
    log = mq_open(MQ_TO_LOG, O_WRONLY);
    statistic = mq_open(MQ_TO_STATISTIC, O_WRONLY);
    if ((log == -1) || (statistic == -1)) {
        perror("conv() mq_open");
        exit(EXIT_FAILURE);
    }

    /*
     * Speicher für Nachricht allokieren.
     */
    if ((message = calloc(1, MQ_MSG_SIZE_SEND)) == NULL) {
        perror("conv()");
        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;

        /* Zufallszahl ins Hexadezimalformat konvertieren. */
        sprintf(message, "%x", random_number);

        /* Nachricht sowohl in die Queue zum log Prozess als auch
         * 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) {
            perror("conv() mq_send");
            exit(EXIT_FAILURE);
        }

    }
}

void conv_cleanup() {
    printf("conv cleanup\n");
    free(message);
    mq_unlink(MQ_TO_LOG);
    mq_unlink(MQ_TO_STATISTIC);
    _exit(EXIT_SUCCESS);
}