summaryrefslogtreecommitdiff
path: root/child.c
blob: 62256e91bfe88ef20683b008964ea4e1a7e25f76 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <stdint.h>

#include "main.h"
#include "ipc.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 ) {
  uint8_t receive_cnt;
  Message msg;

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

  Message start_msg = {
    .s_header = {
      .s_magic = MESSAGE_MAGIC,
      .s_payload_len = 0,
      .s_type = STARTED,
      .s_local_time = 0
    }
  };

  send_multicast(&me, &start_msg);

  receive_cnt = info->x;
  while( receive_cnt ) {
    if( !receive_any( &me, &msg ) ) {
      if( msg.s_header.s_type != STARTED )
        _exit(1);

      receive_cnt--;
    }
  }
}

void child_phase2( dist_info_t *info, uint8_t id ) {
}

void child_phase3( dist_info_t *info, uint8_t id ) {
  uint8_t receive_cnt;
  Message msg;

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

  Message done_msg = {
    .s_header = {
      .s_magic = MESSAGE_MAGIC,
      .s_payload_len = 0,
      .s_type = DONE,
      .s_local_time = 0
    }
  };

  send_multicast(&me, &done_msg);
  receive_cnt = info->x;
  while( receive_cnt ) {
    if( !receive_any( &me, &msg ) ) {
      if( msg.s_header.s_type != DONE )
        _exit(1);

      receive_cnt--;
    }
  }
}

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