#include #include #include "dist.h" #include "ipc.h" #include "pa1.h" void child_phase0( dist_info_t *info, uint8_t id ) { close_redundant_pipes( info, id ); } void child_phase1( dist_info_t *info, uint8_t id ) { pid_t child_pid = getpid(); char buf[512]; uint8_t i; my_info_t me = { .id = id, .dist_info = info }; int size = snprintf( buf, sizeof(buf), log_started_fmt, id, child_pid, info->parent_pid ); Message msg = { .s_header = { .s_magic = MESSAGE_MAGIC, .s_payload_len = size + 1, .s_type = STARTED, .s_local_time = 0 } }; fputs( buf, info->events_log ); fputs( buf, stdout ); strncpy( msg.s_payload, buf, sizeof( msg.s_payload ) ); send_multicast( &me, &msg ); for( i = 1; i <= info->x; i++ ) { if( i == me.id ) continue; if( !receive( &me, i, &msg ) ) { if( msg.s_header.s_type != STARTED ) { fprintf( stderr, "Message type INVALID (not STARTED)!\n" ); break; } } } snprintf( buf, sizeof(buf), log_received_all_started_fmt, id ); fputs( buf, info->events_log ); fputs( buf, stdout ); } void child_phase2( dist_info_t *info, uint8_t id ) { } void child_phase3( dist_info_t *info, uint8_t id ) { char buf[MAX_MESSAGE_LEN]; uint8_t i; my_info_t me = { .id = id, .dist_info = info }; int size = snprintf( buf, sizeof(buf), log_done_fmt, id ); Message msg = { .s_header = { .s_magic = MESSAGE_MAGIC, .s_payload_len = size + 1, .s_type = DONE, .s_local_time = 0 } }; fputs( buf, info->events_log ); fputs( buf, stdout ); strncpy( msg.s_payload, buf, sizeof( msg.s_payload ) ); send_multicast( &me, &msg ); for( i = 1; i <= info->x; i++ ) { if( i == me.id ) continue; if( !receive( &me, i, &msg ) ) { if( msg.s_header.s_type != DONE ) { fprintf( stderr, "Message type INVALID (not DONE)!\n" ); break; } } } snprintf( buf, sizeof(buf), log_received_all_done_fmt, id ); fputs( buf, info->events_log ); fputs( buf, stdout ); } void child_workflow( dist_info_t *info, uint8_t id ) { /* CHILD PHASE 0: Close all redundant descriptors */ child_phase0(info, id); /* CHILD PHASE 1: Send STARTED message for all, receive STARTED * messages from other childs */ child_phase1(info, id); /* CHILD PHASE 2: Do some useful work */ child_phase2(info, id); /* CHILD PHASE 3: Send DONE message for all, receive DONE messages from * other childs and exit */ child_phase3(info, id); }