Example #1
0
int main (int argc, char *argv[]){
	if (argc != 2){
		printf("put pid #\n");
		exit(1);
	}
	char input[100];
	int temp;		
	while(1){
		memset(input, 0, 100);
		if (argv[2] == "1"){
			unicast_receive(2, input);
			continue;
		}

		fgets(input, 100,stdin);
		input[strcspn(input, "\n")] = 0;
		if (strncmp(input, "exit", 4) == 0)
			break;
		else if (strncmp(input, "send", 4) == 0){
			unicast_send(atoi(&input[5]), &input[7]);
		}
		else 
			printf("invalid command\n");
	}	

	return 0;
}
//receive thread
//receive messages sent to owner or replicas and do operation
void * do_messages(){
	while(1){
		int sender, timestamp;
		unsigned int random_num;
		char * command = (char *)malloc(MAX_BUF_LEN * sizeof(char));
		int rec_bytes = unicast_receive(command, &sender, &timestamp, 0);
		if(rec_bytes > 0){
			random_num = rand();
			arg_struct args;
			args.command = command;
			args.sender = sender;
			args.timestamp = timestamp;
			args.random = &random_num;
			pthread_t thread;
			pthread_create(&thread, NULL, &execute_command, (void *)&args);	
		}
	}

	return 0;
}