Example #1
0
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 );
}
Example #2
0
void parentProcess( Process* const proc ) {

	closeUnusedPipes( proc );

	// Receive STARTED
	receiveAll( proc, STARTED, proc -> total );
	sprintf( LogBuf, log_received_all_started_fmt, get_lamport_time(), proc -> localId );
	makeIPCLog( LogBuf );

	// Receive DONE
	receiveAll( proc, DONE, proc -> total );
	sprintf( LogBuf, log_received_all_done_fmt, get_lamport_time(), proc -> localId );
	makeIPCLog( LogBuf );

	waitForChildren();

	closeTheOtherPipes( proc );
}
Example #3
0
int main(int argc, char *argv[])
{
	int pid;
	dataIO_t data;
	int start_msgs, done_msgs;
	FILE *fd_pipes, *fd_events;

	if(argc < 2)
		usage();

	int c;
	opterr=0;

	int mutexfl = 0;

    const char* short_options = "p:";

    const struct option long_options[] = {
        {"mutexl", 0, &mutexfl, 1},
        {NULL,0,NULL,0}
    };

    while(1){
		c = getopt_long(argc, argv, short_options, long_options, NULL);
		if (c == -1)
			break;

		switch (c) {
			case 0:
				break;
			case 'p':
				data.processes = atoi(optarg)+1;
				break;
			case '?':
			default:
				usage();
		}
	}
	
	data.lid = 0;

	if ((fd_pipes = fopen(pipes_log, "w")) == NULL) {
		fprintf(stderr, "Error opening the file %s\n", pipes_log);
		exit(1);
	}	

	for(int i = 0; i < data.processes; i++) {		
		for(int j = 0; j < data.processes; j++) {
			if(j==i) {
				data.pipes[i][j].rdwr[0] = -1;
				data.pipes[i][j].rdwr[1] = -1;
			}
			else {
				if(pipe(data.pipes[i][j].rdwr) < 0) {
					fprintf(stderr, "Error creating the pipe\n");
					exit(1);
				}

				int mode;
				mode = fcntl(data.pipes[i][j].rdwr[0], F_GETFL);
				fcntl(data.pipes[i][j].rdwr[0], F_SETFL, mode | O_NONBLOCK);

				mode = fcntl(data.pipes[i][j].rdwr[1], F_GETFL);
				fcntl(data.pipes[i][j].rdwr[1], F_SETFL, mode | O_NONBLOCK);

				fprintf(fd_pipes, "The pipe %d ===> %d was created\n", j, i);
			}
		}
	}

	fclose(fd_pipes);

	Message msg, resMsg;
	msg.s_header.s_magic = MESSAGE_MAGIC;
	msg.s_header.s_local_time = 0;

	if ((fd_events = fopen(events_log, "a")) == NULL) {
		fprintf(stderr, "Error opening the file %s\n", events_log);
		exit(1);
	}	

	for(int i = 1; i < data.processes; i++) {
		pid = fork();

		if(pid < 0) {
			fprintf(stderr, "Error creating the child\n");
			exit(1);
		} else if (pid == 0) {

			doChild(&data, fd_events, i, 0, mutexfl);
			exit(0);			
		}
	}

	closeUnusedPipes(&data);

	timestamp_t tm = get_lamport_time();
	start_msgs = data.processes - 1;
	done_msgs = data.processes - 1;

	while(start_msgs) {				
		if(receive_any(&data, &resMsg) > -1) {
			if(resMsg.s_header.s_type == STARTED)
			{
				start_msgs--;
				lamportStamp = max(lamportStamp, resMsg.s_header.s_local_time);
				tm = get_lamport_time();
			}
			if(resMsg.s_header.s_type == DONE) {
				done_msgs--;
				lamportStamp = max(lamportStamp, resMsg.s_header.s_local_time);
				tm = get_lamport_time();
			}
		}
	}

	fprintf(fd_events, log_received_all_started_fmt, tm, data.lid);
	fflush(fd_events);
	printf(log_received_all_started_fmt, tm, data.lid);

	while(done_msgs) {				
		if(receive_any(&data, &resMsg) > -1) {
			if(resMsg.s_header.s_type == DONE){
				done_msgs--;
				lamportStamp = max(lamportStamp, resMsg.s_header.s_local_time);
				tm = get_lamport_time();
			}
		}
	}

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

	for(int i = 0; i < data.processes; i++) {
		wait(&i);
	}	

	return 0;
}
Example #4
0
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); 
}