Example #1
0
void Tester::wakeup()
{ 
  if (g_callback_counter < g_tester_length) {
    // Try to perform an action or check
    Check* check_ptr = m_checkTable.getRandomCheck();
    assert(check_ptr != NULL);
    check_ptr->initiate();
    
    checkForDeadlock();
    
    g_eventQueue_ptr->scheduleEvent(this, 2);
  }
}
Example #2
0
void main(int argc,char* argv[])
{
	semID = semget((key_t)SEMAPHORE_KEY,6,IPC_CREAT|0666);
	float probability;

	if(argc!=2)
	{
		printf("Please provide probability as argument ! \n");
		exit(1);
	}
	else
	{
		sscanf(argv[1],"%f",&probability);
		if(probability < 0 || probability > 1)
		{
			printf("Please run the program with valid probability value : [0-1] \n");
			exit(1);
		}
	}

	char train_sequence[MAX_SEQUENCE_LENGTH];

	int N,M=4;

	if((N=parseInputFile(train_sequence))==-1)
	{
		printf("Error : Cannot open sequence.txt !\n");
		exit(0);
	}

	printf("The sequence scanned from file : %s, N = %d\n",train_sequence,N);

	initializeAllSubSemaphoreValues();

	initializeMatrixFile(N,M);

	srand(time(NULL));

	int sequence_counter = 0,flag=0;

	while(1)
	{
		if(flag)
		{
			sleep(1);
			if(checkForDeadlock(N,M))
				break;
			continue;
		}

		if( ( (float)(rand()%1000) / 1000.0) < probability )
		{
			if(checkForDeadlock(N,M))
				break;
		}
		else
		{
			if(sequence_counter==N)
				flag = 1;
			else
			{
				char c = train_sequence[sequence_counter];
				sequence_counter++;
				if(c!='N' && c!='S' && c!='W' && c!='E')
				{
					printf("Invalid Sequence in sequence.txt !\n");
					exit(1);
				}
				else
				{
					/*
					char command[50];
					sprintf(command,"./train %c %d &",c,sequence_counter-1);
					pid_arr[sequence_counter-1] = pid;
					direction_arr[sequence_counter-1] = getDirString(c);
					system(command);
					*/
					
					int pid = fork();
					if(pid == 0)
					{
						//char command[50];
						//sprintf(command,"./train %d %c %d",N,c,sequence_counter-1);
						
						char** argv = malloc(4*sizeof(char*));
						int y;
						for(y=0;y<4;y++) argv[y] = malloc(10*sizeof(char));
						strcpy(argv[0],"./train");
						sprintf(argv[1],"%d",N);
						sprintf(argv[2],"%c",c);
						sprintf(argv[3],"%d",sequence_counter-1);

						int retv = execvp(*argv,argv);
						if(retv == -1) {perror("Error in execvp ! \n"); exit(1);}
						exit(0);
					}
					printf("Train %d: %s Train started\n",pid,getDirString(c));
					pid_arr[sequence_counter-1] = pid;
					//printf("pid_arr[%d] = %d\n",sequence_counter-1,pid );
					direction_arr[sequence_counter-1] = getDirString(c);
					//printf("direction_arr[%d] = %s\n",sequence_counter-1,getDirString(c) );
					
				}
			}
			
		}
	}

}
Example #3
0
void main(int argc,char* argv[])
{
	int DPP;
	float probability = 0.2;

	if(argc!=3)
	{
		printf("Please run the program with 2 arguments : <DPP_toggle (0 or 1)> <probability> \n"); // DPP --> Deadlock Prevention Protocol
		exit(1);
	}
	else
	{
		sscanf(argv[1],"%d",&DPP);
		sscanf(argv[2],"%f",&probability);
		if((DPP != 0 && DPP != 1)||(probability<0 || probability >1))
		{
			printf("Please run the program with valid arguments : <DPP_toggle (0 or 1)> <probability>\n"); // DPP --> Deadlock Prevention Protocol
			exit(1);
		}
	}

	semID = semget((key_t)SEMAPHORE_KEY,7,IPC_CREAT|0666);

	initializeAllSubSemaphoreValues();

	acquireMutexLock(MATRIX_MUTEX);
	initializeMatrixFile();
	releaseMutexLock(MATRIX_MUTEX);

	int mssgQ0_ID=msgget((key_t)MSSG_Q0_KEY,IPC_CREAT|0666);
	int mssgQ1_ID=msgget((key_t)MSSG_Q1_KEY,IPC_CREAT|0666);

	int i;
	for(i=0;i<NUM_PRODUCERS;i++)
	{
		char command[50];
		sprintf(command,"xterm -hold -e ./producer %d %d %d &",MSSG_Q0_KEY,MSSG_Q1_KEY,i);
		system(command);
		sleep(1);	// otherwise the item to be first inserted becomes the same for all the Queues due to the timeseeding : srand(time(NULL)) 
	}

	for(i=0;i<NUM_CONSUMERS;i++)
	{
		char command[50];
		sprintf(command,"xterm -hold -e ./consumer %d %d %d %f %d &",MSSG_Q0_KEY,MSSG_Q1_KEY,NUM_PRODUCERS+i,probability,DPP);
		system(command);
	}


	while(1)
	{
		int m,n;	// if Deadlock is found, then Consumer m (Cm) and Consumer n (Cn) are such that Q0 --> Cm --> Q1 --> Cn --> Q0, in the Resource Allocation Graph 

		int found_deadlock = 0;
		printf("Checking for Deadlock...\n");

		acquireMutexLock(MATRIX_MUTEX);
		found_deadlock = checkForDeadlock(&m,&n);
		releaseMutexLock(MATRIX_MUTEX);

		if(found_deadlock)
		{
			printf("\n\nDeadlock !!!\n\nDeadlocked Cycle : Q0 --> C%d --> Q1 --> C%d --> Q0 \n\nKilling all Processes...",m,n);
			printf("\n\nPress a key to kill all Processes\n");
			getch();
			killAllProcesses();
			appendResults(probability);
			exit(0);
		}
		sleep(2);
	}
}