aboutsummaryrefslogtreecommitdiff
path: root/aufgabe2/statistic.c
blob: 31230cf9f176df0de36ae72ab689479442d02f9c (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
/*
 * vim:ts=4:sw=4:expandtab
 *
 */
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

#include "queue.h"

FILE *pipe_conv;
FILE *pipe_mon;
char *message;

void statistic() {
    int current_value = 0, last_value = 0, result;

    printf("statistic started\n");

    /* Die Pipe zum conv Prozess lesend öffnen, die zum monitor schreibend */
    if (((pipe_conv = fdopen(queue[D_CONV_TO_STAT][READ], "r")) == NULL) ||
        ((pipe_mon = fdopen(queue[D_STAT_TO_MON][WRITE], "w")) == NULL)) {
        perror("statistic.c fdopen()");
        exit(EXIT_FAILURE);
    }

    /* Speicher für Nachricht reservieren */
    if ((message = calloc(1, sizeof(char)*SIZE_HEX+1)) == NULL) {
        perror("statistic.c calloc()");
        exit(EXIT_FAILURE);
    }

    /*
     * Auslesen der Nachrichten vom conv Prozess, Hälfte des Wertest mit der
     * des letzten Wert addieren und an den monitor Prozess schicken.
     */
    while (fgets(message, SIZE_HEX+1, pipe_conv)) {
        sscanf(message, "%x\n", &current_value); /* HEX -> DEC */

        result = current_value/2 + last_value/2;
        fprintf(pipe_mon, "%x\n", result);

        last_value = current_value;
    }
}

void statistic_cleanup() {
    printf("statistic cleanup\n");
    fclose(pipe_conv);
    fclose(pipe_mon);
    free(message);
    close(queue[D_CONV_TO_STAT][READ]);
    close(queue[D_STAT_TO_MON][WRITE]);
    _exit(EXIT_SUCCESS);
}