diff options
Diffstat (limited to 'parent.c')
-rw-r--r-- | parent.c | 43 |
1 files changed, 26 insertions, 17 deletions
@@ -1,11 +1,16 @@ #include <stdint.h> +#include <sys/wait.h> +#include <stdlib.h> #include "main.h" #include "ipc.h" void parent_workflow( dist_info_t *info ) { Message msg; - uint8_t receive_cnt; + uint8_t i, receive_cnt; + pid_t wpid; + int status; + int ret = 0; my_info_t me = { .id = PARENT_ID, @@ -15,29 +20,33 @@ void parent_workflow( dist_info_t *info ) { // PARENT PHASE 0: Close all redundant descriptors close_redundant_pipes( info, PARENT_ID ); - // PARENT PHASE 1: Receive all STARTED messages - receive_cnt = info->x; - while( receive_cnt ) { - Message msg; - if( receive_any( &me, &msg ) ) { - if( msg.s_header.s_type != STARTED ) - return; - - receive_cnt--; + // PARENT PHASE 1: Receive all STARTED messages from all childs + for( i = 1; i <= info->x; i++ ) { + if( !receive( &me, i, &msg ) ) { + if( msg.s_header.s_type != STARTED ) { + ret = 1; + break; + } } } // PARENT PHASE 2: Receive all DONE messages - receive_cnt = info->x; - while( receive_cnt ) { - Message msg; - if( receive_any( &me, &msg ) ) { - if( msg.s_header.s_type != DONE ) - return; + for( i = 1; i <= info->x; i++ ) { + if( !receive( &me, i, &msg ) ) { + if( msg.s_header.s_type != DONE ) { + ret = 1; + break; + } + } + } + // PARENT PHASE 3: Wait for the end of all processes + receive_cnt = info->x; + while( receive_cnt != 0 ) { + if( (wpid = wait(&status)) > 0 ) { receive_cnt--; } } - // PARENT PHASE 3: Wait for the end of all processes + exit( ret ); } |