void childProcess( Process* proc ) { closeUnusedPipes( proc ); // STARTED proc -> started += 1; incLamportTime(); Message startedMsg; fillMessage( &startedMsg, proc, STARTED ); makeIPCLog( startedMsg.s_payload ); send_multicast( proc, &startedMsg ); // Receive STARTED while( proc -> started != proc -> total ) { defaultCSExtendedCycle( proc ); } sprintf( LogBuf, log_received_all_started_fmt, get_lamport_time(), proc -> localId ); makeIPCLog( LogBuf ); // Payload int totalIterations = proc -> localId * 5; for( int i = 1; i <= totalIterations; i ++ ) { sprintf( LogBuf, log_loop_operation_fmt, proc -> localId, i, totalIterations ); if( proc -> isMutex ) request_cs( proc ); print( LogBuf ); if( proc -> isMutex ) release_cs( proc ); } // DONE proc -> done += 1; incLamportTime(); Message doneMsg; fillMessage( &doneMsg, proc, DONE ); makeIPCLog( doneMsg.s_payload ); send_multicast( proc, &doneMsg ); // Receive DONE while( proc -> done != proc -> total ) { defaultCSExtendedCycle( proc ); } sprintf( LogBuf, log_received_all_done_fmt, get_lamport_time(), proc -> localId ); makeIPCLog( LogBuf ); closeTheOtherPipes( proc ); }
void doChild(void *parentData, FILE *fd_events, int lid, int initBalance, int mutexfl) { Message msg, resMsg; dataIO_t* data = parentData; rick_t rkdata = { EMPTY_STATE, 0, {0} }; cs_t csdata = { data, &rkdata }; int start_msgs = data->processes - 2; int done_msgs = data->processes - 2; timestamp_t tm = lamportStamp; data->lid = lid; closeUnusedPipes(data); balance_t childBalance = initBalance; fprintf(fd_events, log_started_fmt, tm, data->lid, getpid(), getppid(), childBalance); fflush(fd_events); printf(log_started_fmt, tm, data->lid, getpid(), getppid(),childBalance); tm = get_lamport_time(); msg.s_header.s_type = STARTED; msg.s_header.s_magic = MESSAGE_MAGIC; sprintf(msg.s_payload, log_started_fmt, tm, data->lid, getpid(), getppid(), childBalance); msg.s_header.s_payload_len = strlen(msg.s_payload); msg.s_header.s_local_time = tm; send_multicast(data, &msg); while(start_msgs) { if(receive_any(data, &resMsg) > -1) { if(resMsg.s_header.s_type == STARTED) start_msgs--; if(resMsg.s_header.s_type == DONE) done_msgs--; } } fprintf(fd_events, log_received_all_started_fmt, tm, data->lid); fflush(fd_events); printf(log_received_all_started_fmt, tm, data->lid); int replyCounter = 0; int printIterator = 0; int printMax = data->lid * 5; while(done_msgs || printIterator < printMax) { int rPid = receive_any(data, &resMsg); if(rPid > -1) { if(resMsg.s_header.s_type == DONE) { lamportStamp = max(lamportStamp, resMsg.s_header.s_local_time); tm = get_lamport_time(); done_msgs--; } if(resMsg.s_header.s_type == CS_REQUEST) { lamportStamp = max(lamportStamp, resMsg.s_header.s_local_time); tm = get_lamport_time(); if(csdata.rick->state == EMPTY_STATE || (csdata.rick->state == WAIT_STATE && (csdata.rick->requestTime >= resMsg.s_header.s_local_time && rPid < data->lid))) { tm = get_lamport_time(); msg.s_header.s_type = CS_REPLY; msg.s_header.s_payload_len = 0; msg.s_header.s_local_time = tm; send(data, rPid, &msg); } else { csdata.rick->waitProcess[rPid] = 1; } } if(resMsg.s_header.s_type == CS_REPLY) { lamportStamp = max(lamportStamp, resMsg.s_header.s_local_time); tm = get_lamport_time(); replyCounter ++; } } if(mutexfl == 1){ if(printIterator < printMax) request_cs(&csdata); if(replyCounter == data->processes - 2) { csdata.rick->state = EXEC_STATE; char str[MAX_PAYLOAD_LEN]; sprintf(str, log_loop_operation_fmt, data->lid, printIterator+1, printMax); print(str); printIterator++; release_cs(&csdata); replyCounter = 0; } } else { if(printIterator < printMax) { char str[MAX_PAYLOAD_LEN]; sprintf(str, log_loop_operation_fmt, data->lid, printIterator+1, printMax); print(str); printIterator++; } } if(printIterator == printMax) { fprintf(fd_events, log_done_fmt, tm, data->lid, childBalance); fflush(fd_events); printf(log_done_fmt, tm, data->lid, childBalance); msg.s_header.s_type = DONE; sprintf(msg.s_payload, log_done_fmt, tm, data->lid, childBalance); msg.s_header.s_payload_len = strlen(msg.s_payload); send_multicast(data, &msg); printIterator ++; } } fprintf(fd_events, log_received_all_done_fmt, tm, data->lid); fflush(fd_events); printf(log_received_all_done_fmt, tm, data->lid); fclose(fd_events); exit(0); }