summaryrefslogtreecommitdiff
path: root/child.c
blob: 796684474d68cb495f7aa00862df5de3098084fa (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <stdint.h>
#include <string.h>

#include "ipc.h"
#include "pa2345.h"
#include "lamport.h"

#include "dist.h"

#define MAX_LOOP_STR 1024

void child_phase0( dist_info_t *info, uint8_t id ) {
  lamport_init( &gl_lamport_time );
  close_redundant_pipes( info, id );
}

void child_phase1( dist_info_t *info, uint8_t id ) {
  pid_t child_pid = getpid();
  char buf[MAX_MESSAGE_LEN];
  uint8_t i;

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

  int size = snprintf( buf, sizeof(buf), log_started_fmt, get_lamport_time(),
                                                          id, child_pid, 
                                                          info->parent_pid, 
                                                          0 );
  fputs( buf, info->events_log );
  fflush( info->events_log );
  fputs( buf, stdout );

  lamport_increase( &gl_lamport_time );

  Message msg = {
    .s_header = {
      .s_magic = MESSAGE_MAGIC,
      .s_payload_len = size + 1,
      .s_type = STARTED,
      .s_local_time = get_lamport_time()
    }
  };
  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 ) ) {
      lamport_message_handler( &gl_lamport_time, msg.s_header.s_local_time );
      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, get_lamport_time(), id ); 
  fputs( buf, info->events_log );
  fflush( info->events_log );
  fputs( buf, stdout );
  fflush( info->events_log );
}

void child_phase2( dist_info_t *info, uint8_t id ) {
  my_info_t me = {
    .id = id,
    .dist_info = info
  };

  if( info->mutex )
    request_cs(&me);
  
  uint8_t i, loop_cnt;
  char buf[ MAX_LOOP_STR ];

  loop_cnt = id * 5;
  for( i = 1; i <= loop_cnt; i++ ) {
    snprintf( buf, sizeof(buf), log_loop_operation_fmt, id, i, loop_cnt );
    print( buf );
  }

  if( info->mutex )
    release_cs( &me );
}

void child_phase3( dist_info_t *info, uint8_t id ) {
  char buf[MAX_MESSAGE_LEN];

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

  int size = snprintf( buf, sizeof(buf), log_done_fmt,
                                         get_lamport_time(), id, 
                                         0 );
  fputs( buf, info->events_log );
  fflush( info->events_log );
  fputs( buf, stdout );

  lamport_increase( &gl_lamport_time );

  Message msg = {
    .s_header = {
      .s_magic = MESSAGE_MAGIC,
      .s_payload_len = size + 1,
      .s_type = DONE,
      .s_local_time = get_lamport_time()
    }
  };
  strncpy( msg.s_payload, buf, sizeof( msg.s_payload ) );
  send_multicast( &me, &msg );
  info->workers--;

  while( info->workers ) {
    if( !receive_any( &me, &msg ) ) {
      lamport_message_handler( &gl_lamport_time, msg.s_header.s_local_time );
      if( msg.s_header.s_type == DONE )
        info->workers--;
    }
  }

  snprintf( buf, sizeof(buf), log_received_all_done_fmt,
                              get_lamport_time(), id ); 
  fputs( buf, info->events_log );
  fflush( 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);
}