summaryrefslogtreecommitdiff
path: root/pa1.c
diff options
context:
space:
mode:
Diffstat (limited to 'pa1.c')
-rw-r--r--pa1.c71
1 files changed, 71 insertions, 0 deletions
diff --git a/pa1.c b/pa1.c
new file mode 100644
index 0000000..6bd3d59
--- /dev/null
+++ b/pa1.c
@@ -0,0 +1,71 @@
+#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 "common.h"
+
+#include "child.h"
+#include "parent.h"
+#include "dist.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);
+}
+
+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;
+}