Esempio n. 1
0
bool compareCPU(Sim a, Sim b)
{
	return a.getcTime() < b.getcTime();
}
Esempio 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);
}