summaryrefslogtreecommitdiff
path: root/main.c
blob: 8c8ec083ac056bafdd331b22fbbdbd9c6d815399 (plain)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#define _GNU_SOURCE             /* See feature_test_macros(7) */
#include <fcntl.h>              /* Obtain O_* constant definitions */
#include <unistd.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "main.h"
#include "child.h"
#include "parent.h"
#include "common.h"

static char* app_name;

void usage( FILE* output ) {
  fprintf(output, "usage: %s -p <x>\n", app_name);
  fprintf(output, "`x` must be int between 1 and %d\n", MAX_X);
}

void close_redundant_pipes( dist_info_t *info, uint8_t id ) {
  uint8_t i, j;
  for(i = 0; i < info->proccnt; i++) {
    uint8_t base = i * (info->proccnt-1);     // base of block of pipes for this process

    if( i == id ) {
      for(j = 0; j < info->proccnt - 1; j++) {
      //  fprintf( stderr, "Read descriptor %d closed for id %d\n", info->pipes[base + j][0], id );
        close( info->pipes[base + j][0] );    // close only read descriptor of ( ID -> Y )
      }
    } else {
      uint8_t idx = ( id > i ? id - 1: id ); // index number in block of pipes for ( X -> ID )
      for(j = 0; j < info->proccnt - 1; j++) {
        if( j == idx ) {
          close( info->pipes[base + j][1] );  // close only write descriptor of ( X -> ID )
       //   fprintf( stderr, "Write descriptor %d of pipe (%d -> %d) closed for id %d\n", info->pipes[base + j][1], i, id, id );
        } else {
       //   fprintf( stderr, "LOL\n\n\n");
          close( info->pipes[base + j][0] );  // close read descriptor of ( X -> Y, where Y != ID )
          close( info->pipes[base + j][1] );  // close write descriptor of ( X -> Y, where Y != ID )
        }
      }
    }
  }
}

int main(int argc, char* argv[]) {
  uint8_t i;
  dist_info_t info;
  app_name = argv[0];

  if( argc != 3 || strcmp( argv[1], "-p") != 0 ) {
    usage(stdout);
    return 1;
  }

  // fill info about distributed network
  info.x = atoi(argv[2]);
  if( info.x < 0 || info.x > MAX_X ) {
    usage(stdout);
    return 1;
  }

  info.proccnt    = info.x + 1;
  info.parent_pid = getpid();
  info.events_log = fopen( events_log, "w" );
  info.pipes_log  = fopen( pipes_log, "w" );

  // open all (N * (N-1)) pipes for a full mesh topology
  for( i = 0; i < info.proccnt * (info.proccnt - 1); i++ ) {
    pipe2( info.pipes[i], O_NONBLOCK | O_DIRECT );
    fprintf( info.pipes_log, "Opened pipe #%d (%d;%d)\n", i, info.pipes[i][0], info.pipes[i][1] );
  }
  fclose( info.pipes_log );

  for( i = 1; i < info.proccnt; i++ ) {
    pid_t pid = fork();
    switch(pid) {
      case 0:
        /* Child branch */
        child_workflow( &info, i );
        exit(0);
        break;
      case -1:
        /* Error */
        perror("fork");
        return 1;
      default:
        break;
    }
  }

  parent_workflow( &info );
  return 0;
}