summaryrefslogtreecommitdiff
path: root/parent.c
blob: dc11c65723faa727fcc327dd4fcf6334b60d16e6 (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
#include <stdint.h>
#include <sys/wait.h>
#include <stdlib.h>

#include "dist.h"
#include "ipc.h"

void parent_workflow( dist_info_t *info ) {
  Message msg;
  uint8_t i, receive_cnt;
  pid_t wpid; 
  int status;
  int ret = 0;

  my_info_t me = {
    .id = PARENT_ID,
    .dist_info = info
  };

  // PARENT PHASE 0: Close all redundant descriptors
  close_redundant_pipes( info, PARENT_ID );

  // 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
  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--;
    }
  }

  exit( ret );
}