summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'main.c')
-rw-r--r--main.c86
1 files changed, 86 insertions, 0 deletions
diff --git a/main.c b/main.c
new file mode 100644
index 0000000..d069c1f
--- /dev/null
+++ b/main.c
@@ -0,0 +1,86 @@
+#include <stdio.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++) {
+ // don't close all pipes ( ID -> Y )
+ if( id == i )
+ continue;
+
+ uint8_t base = i * (info->proccnt-1); // base of block of pipes for this process
+ uint8_t idx; // index number in block of pipes for ( X -> ID )
+
+ if( id < i )
+ idx = id - 1;
+ else
+ idx = 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 )
+ } else {
+ 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, j;
+ dist_info_t info;
+ app_name = argv[0];
+
+ if( argc != 2 || 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++ )
+ pipe(info.pipes[i]);
+
+// 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;
+}