generator.c 2.89 KB
Newer Older
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
/**
 * @file generator.c
 * @author Ivaylo Ivanov 11777707
 * @date 11.01.2018
 *
 * @brief Generator program module.
 *
 * The generator program takes a graph as input. The program repeatedly generates a random solution
 * to the problem as described on the first page and writes its result to the circular buffer. It repeats this
 * procedure until it is notified by the supervisor to terminate.
 * The generator program takes as arguments the set of edges of the graph.
 * Each positional argument is one edge; at least one edge must be given. An edge is specified by a string,
 * with the indices of the two nodes it connects separated by a -. Since the graph is directed, the edge starts
 * at the first node and leads to the second node. Note that the number of nodes of the graph is implicitly
 * provided through the indices in the edges. In the example above the generator program is called with
 * the graph shown on the first page.
 * The generator uses the algorithm described on the first page to generate random feedback arc sets for the
 * given graph. It writes these feedback arc sets to the circular buffer, one at a time; therefore a feedback
 * arc set is a single element of the circular buffer. The generator may produce debug output, describing
 * the feedback arc sets which it writes to the circular buffer.
 *
 *   SYNOPSIS
 *   generator EDGE1 EDGE2 ...
 *
 *   EXAMPLE
 *   generator 0-1 1-2 1-3 1-4 2-4 3-6 4-3 4-5 6-0
 *
 **/
#include "shared/shm.c"
30 31
#include "shared/sem.c"
#include "shared/structs.c"
Ivaylo Ivanov's avatar
Ivaylo Ivanov committed
32
#include <regex.h>
33 34

int main(int argc, char *argv[]) {
Ivaylo Ivanov's avatar
Ivaylo Ivanov committed
35 36 37
    if(argc < 2) {
        fprintf(stderr, "ERROR: Command takes at least one argument.\n");
        puts("Usage: \ngenerator EDGE1 EDGE2 ...");
38 39 40
        exit(EXIT_FAILURE);
    }

41 42
    process_signal();

43
    /// Create a regex to check the input against
Ivaylo Ivanov's avatar
Ivaylo Ivanov committed
44 45 46 47 48 49
    regex_t regex;
    if(regcomp(&regex, "^[0-9]*-[0-9]*$", REG_EXTENDED|REG_NOSUB)) {
        fprintf(stderr, "ERROR: Could not compile regex\n");
        exit(EXIT_FAILURE);
    }

50 51 52 53 54 55 56
    edge_t edges[argc - 2]; ///< Array to hold the edges

    char edge[128]; ///< Variable to hold a single edge later on
    edge[127] = 0;

    for(int i=1; i < argc; i++) {
        /// Check the input and terminate if incorrect
Ivaylo Ivanov's avatar
Ivaylo Ivanov committed
57 58 59 60
        if(regexec(&regex, argv[i], 0, NULL, 0) == REG_NOMATCH) {
            fprintf(stderr, "ERROR: Incorrect input found\n");
            puts("Usage: \ngenerator EDGE1 EDGE2 ...");
            exit(EXIT_FAILURE);
61 62 63 64 65 66 67
        } else {
            /// If input is correct, split and add to the edges array
            strcpy(edge, argv[i]);
            char *u = strtok(edge, "-");
            char *v = strtok(NULL, "");
            edges[i-1].u = strtol(u, NULL, 10);
            edges[i-1].v = strtol(v, NULL, 10);
Ivaylo Ivanov's avatar
Ivaylo Ivanov committed
68 69 70
        }
    }

71 72 73 74 75
    int buffer_shmfd = create_shmfd(BUFF_SHM_NAME);
    circ_buffer = (buffer_t *)open_shm(buffer_shmfd, sizeof(buffer_t));

    int wr_pos = 0;

76
    exit(EXIT_SUCCESS);
77
}