Ejemplo n.º 1
0
void Starter::decideAlgorithm(){
	printf("\nIn decideAlgorithm() of Node\n");
	communication com;
	int serfd;
	int clifd = com.OpenListener(serfd,LISTEN_PORT2);
	//char buffer[4095]={'\0',};
	int intbuf;
	com.readFromSocket(clifd,&intbuf,sizeof(int));
	printf("recvd: %d\n",intbuf);
	puts("Getting ID to IP map\n");
	//assigining memory to mapIDtoIP
	mapIDtoIP = new char*[NumNodes];
	for(int i = 0; i < NumNodes; ++i){
		mapIDtoIP[i] = new char[MAXLENGTH_IP_ADDR];
	}

	char recvdMsg[4096] = {'\0',};
	strcpy(mapIDtoIP[0],"hello");
	com.readFromSocket(clifd,recvdMsg,4095);
	string recvdStr = recvdMsg;
	vector<string> recvdValues;
	string delimiter = ":";
	parseMsg(recvdStr,delimiter,recvdValues);
	for(int i=0;i<NumNodes;i++){
		strcpy(mapIDtoIP[i],recvdValues[i].c_str());
		printf("%d\t%s\n",i,mapIDtoIP[i]);
	}
	com.readFromSocket(clifd,CS_FileName,25);
	printf("File Name of Critical section resource is %s\n",CS_FileName);
	shutdown(clifd,2);
	com.closeSocket(clifd);
	com.closeSocket(serfd);
	int algo = intbuf;
	if(algo == 1){
		Algorithm1();
	}else if(algo == 2){
		Algorithm2();

	}else
		printf("Invalid input\n");

}
Ejemplo n.º 2
0
int main()
{
	LONGLONG x;
	int menu;
	int listSize = 0;
	cout << "Enter the list size -> " << flush;
	cin >> listSize;
	while( true )
	{
		menu = getMenu();
		switch( menu )
		{
		case 1: // Algorithm #1
			// timer not included when building the list
			list.element = listSize;
			list.buildList();

			// Timer starts
			QueryPerformanceFrequency( &Frequency );
			QueryPerformanceCounter( &PerformanceCount1 );

			// Reverse the list by pushing items on a stack and putting them
			// back onto the list
			Algorithm1( listSize );

			// Timer stops
			QueryPerformanceCounter( &PerformanceCount2 );

			// subtract timer from start to end
			x = PerformanceCount2.QuadPart - PerformanceCount1.QuadPart;

			// convert to nanosecond unit
			x = 1000000000*x/Frequency.QuadPart;

			// empty the list for the next run
			list.MakeEmpty();

			// output the time result
			printf("Execute time: %I64d\n", x );
			break;
		case 2: // Algorithm #2
			// timer not included when building the list
			list.element = listSize;
			list.buildList();

			// Timer starts
			QueryPerformanceFrequency( &Frequency );
			QueryPerformanceCounter( &PerformanceCount1 );

			// Reverse the list on a SINGLE list recursively
			Algorithm2();

			// Timer stops
			QueryPerformanceCounter( &PerformanceCount2 );

			// subtract timer from start to end
			x = PerformanceCount2.QuadPart - PerformanceCount1.QuadPart;

			// convert to nanosecond unit
			x = 1000000000*x/Frequency.QuadPart;

			// output the time result
			printf("Execute time: %I64d\n", x );

			// empty the list for the next run
			list.MakeEmpty();

			break;
		case 3:
			// Users define a list size for different test cases
			cout << "Enter the list size -> " << flush;
			cin >> listSize;
			break;
		case 4:
			// quit
			cout << "Quit" << endl;
			return 0;
		default:
			// Wrong input!
			cout << "Invalid Input" << endl;
			break;
		}
	}

	return 0;
}