Ejemplo n.º 1
0
// Preemptive Priority (PP) 
void pp(Sim* p, int size)
{
	int tempWait = 0, minTurn = p[0].getTurnTime(), maxTurn = 0, turnT = 0, minInitial = p[0].getITime(), 
		maxInitial = 0, initialT = 0, minWait = p[0].getWaitTime(), maxWait = 0, totalW = 0;  

	vector <Sim> arrived;	//vector to store processes that has arrived
	int counter = 0;
	int store = 0;
	bool firstTime = true;

	elapsedTime = 0;

	cout << "\n\n\nPreemptive-Priority | Send Processes to CPU and run: \n";
	//push process into the vector if the arrival time is 0
	for (int i = 0; i < size; i++)
	{
		if (p[i].getATime() == 0)
		{
			arrived.push_back(p[i]);
		}
	}

	//Sort "arrived" by priority
	sortPriority(arrived);

	while(counter != size)
	{
		int checkArrival; 
		int timeShortest = 8000;
		bool found = false; 
		Sim temp = arrived[0];
		for (int i = 0; i < size; i++)
		{	
			checkArrival = arrived[0].getTimeRemain() + elapsedTime; 
			if (checkArrival > p[i].getATime())
			{
				if(p[i].getATime() < timeShortest)
				{
					timeShortest = p[i].getATime(); 
				}
				found = true;
			}
		}
		if(found == true)
		{ 
			arrived[0].setTimeRemain(elapsedTime - arrived[0].getcTime());	// set the remain time for current process
			elapsedTime = elapsedTime - arrived[0].getTimeRemain(); 
			for(int i = 0; i < size; i ++)
			{ 
				if(p[i].getATime() == timeShortest) 
				{
					arrived.push_back(p[i]);
				}
			}
			sortPriority(arrived);
		}
		else // the process has terminated before any other process entered 
		{ 
			arrived[0].setTimeRemain(0); 
			temp = arrived[0]; 
			arrived.erase(arrived.begin());
			sortPriority(arrived); 
			elapsedTime+=temp.getTimeRemain();  // final elapsed time after this process terminates
			temp.setTurnTime(elapsedTime);
			printTerminate(elapsedTime, temp.getpId(), temp.getTurnTime(), temp.getWaitTime()); 
			counter++; 	
		}	
		if (!firstTime && !arrived.empty( ))
		{
			elapsedTime = totalElapsedTime(elapsedTime); 
			printContextSwitch(elapsedTime, temp.getpId(), arrived[0].getpId());			
			elapsedTime = totalElapsedTime(elapsedTime);  
		} 
		
		firstTime = false; 
	}
}
Ejemplo n.º 2
0
/* Shortest-Job First (SFS)
 * This will sort the processes that are sent in based on the 
 * shortest job in the array. */ 
void sjf(Sim* p, int size)
{
	int tempWait = 0, minTurn = p[0].getTurnTime(), maxTurn = 0, 
		turnT = 0, minInitial = p[0].getITime(), maxInitial = 0, 
		initialT = 0, minWait = p[0].getWaitTime(), maxWait = 0, 
		totalW = 0;  

	elapsedTime = 0; 
	vector <Sim> arrived;

	int check = 1; 

	//push it into vector if it arrives at time 0
	for(int i = 0; i < size; i++)
	{
		if (p[i].getATime() == 0)
		{
			arrived.push_back(p[i]);
			check = i + 1;
		}
	}

	sort(arrived.begin(), arrived.end(), compareCPU); 

	cout << "\n\n\nShortest Job First without Preemption | Send Processes to CPU and run: \n"; 
	
	//for(int j = 0; j < size; j++) 
	int counter = 0;
	bool firstTime = true; 
	Sim temp; 
	while(counter != size)
	{ 
		for(int i = check; i < size; i++)
		{
			int totalTime = arrived[0].getcTime() + elapsedTime;
			if (totalTime > p[i].getATime())
			{
				arrived.push_back(p[i]);
				check = i + 1;
			}
		}
		temp = arrived[0];
		arrived.erase(arrived.begin()); // this process will have terminated so it can be removed from the vector
		sort(arrived.begin(), arrived.end(), compareCPU); // sorts all the processes that will be in the vector after the one terminates

		temp.setWaitTime(elapsedTime);
		printFirst(elapsedTime, temp.getpId(), temp.getWaitTime());	
		elapsedTime+=temp.getcTime(); 
		temp.setTurnTime(elapsedTime);
		printTerminate(elapsedTime, temp.getpId(), temp.getTurnTime(), temp.getWaitTime()); 
		firstTime = false;
		counter ++; 

		if (!firstTime && !arrived.empty( ))
		{
			elapsedTime = totalElapsedTime(elapsedTime); 
			printContextSwitch(elapsedTime, temp.getpId(), arrived[0].getpId());			
			elapsedTime = totalElapsedTime(elapsedTime);  
		} 
		
	} 

	dataToCollect(p, size, minTurn, maxTurn, turnT, minInitial, maxInitial, initialT, minWait, maxWait, totalW);
}