generator.c 2.36 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"
Ivaylo Ivanov's avatar
Ivaylo Ivanov committed
30
#include <regex.h>
31 32

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

Ivaylo Ivanov's avatar
Ivaylo Ivanov committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52
    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);
    }

    for(int i=1; i <= argc - 1; i++) {
        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);
        }
    }

53 54 55 56 57 58 59 60
    void* terminate_shm = open_shm(TERMINATE_SHM, TERMINATE_SHM_SIZE);

    write_to_shm(terminate_shm, "1", TERMINATE_SHM_SIZE);
    puts(terminate_shm);
    close_shm(terminate_shm, TERMINATE_SHM_SIZE);
    destroy_shm(TERMINATE_SHM);
}