1. 程式人生 > >作業系統 實驗五 程序的排程

作業系統 實驗五 程序的排程

實驗目的:

實現先來先服務FCFS、短作業優先SJF以及時間片輪轉排程演算法。

實驗內容:

①實現FCFS演算法:根據程序的到達時間的先後次序來完成對若干程序的排程。

②實現SJF演算法:根據當前時間已經到達程序的需要執行時間選取其中時間最小的程序最先執行。

③實現時間片輪轉演算法:首先要求確定時間片的大小,依據程序的到達時間依次加入佇列,每次分配一個時間片大小的時間,如果沒有完成參與下一次的競爭,當最後需要一個小於等於時間片的時間時本程序完成,同時退出佇列。

④計算每種演算法排程後,系統的平均週轉時間和平均帶權週轉時間。

演算法實現:

void FCFS()
{
		int				i;
		float			sumT = 0.0;
		float			sumW = 0.0;
		float			last ;
		process[0].start = process[0].arrive;
		process[0].finish = process[0].start + process[0].service;
		
		last = process[0].finish;

		i = 1;
		while(i != N)
		{
				if(process[i].arrive > last)
						last = process[i].arrive;
				process[i].start = last;
				process[i].finish = last + process[i].service;
				last += process[i].service;
				i++;
		}
		
		for(i = 0 ; i < N; i++)
		{
				process[i].T = process[i].finish - process[i].arrive;
				process[i].W = process[i].T / (float)process[i].service;
				sumT += process[i].T;
				sumW += process[i].W;
		}
		FCFS_T = sumT / N;
		FCFS_W = sumW / N;
}


int getmin(float t)
{
		int				i;
		int				addr = -1;
		float			min=10000.0;

		for(i = 0 ; i < N ; i++)
		{
				if(process[i].state == 0 && process[i].service < min 
						&& process[i].arrive <= t)
				{
						addr = i;
						min = process[i].service;
				}
		}

		process[addr].finish = t + process[addr].service;

		return addr;
}
void SJF()
{
		int				i;
		float			sumT = 0.0;
		float			sumW = 0.0;

		process[0].finish = process[0].arrive + process[0].service;
		process[0].state  = 1;
		
		int				addr = 0;
		int				sign = 0; 
		float			last = process[0].finish;

		while(sign != N-1)
		{
				addr = getmin(last);
				if(addr == -1)
				{
						last = process[getmin(1000)].arrive;
						continue;
				}
				process[addr].start = last;
				process[addr].state = 1;
				last = process[addr].finish;
				sign++;
		}

		for(i = 0 ; i < N; i++)
		{
				process[i].T = process[i].finish - process[i].arrive;
				process[i].W = process[i].T / (float)process[i].service;
				sumT += process[i].T;
				sumW += process[i].W;
		}

		SJF_T = sumT / N;
		SJF_W = sumW / N;
}

void RR()
{
		int				i = 0;
		int				j;
		int				t = process[0].arrive;
		float			sumT = 0.0;
		float			sumW = 0.0;
		for(j = 0 ; j < N ; j++)
		{
				process[j].state = 0;
				process[j].start = -1;
				process[j].left = process[j].service;
		}
		process[0].start = t;
		while(1)
		{
				for(j = 0 ; j < N ; j++)
						if(process[(i+j)% N].state == 0 && t >= process[(i+j)% N].arrive)
								break;
				i = (i+j)% N;

				if( process[i].state == 1)
				{
						break;
				}

				if(process[i].start == -1)
						process[i].start = t;

				for(j = 0 ; j < min(process[i].left,q) ; j++)
						cout <<process[i].name;
				if(process[i].left > q)
				{	
						t += q;
						process[i].left -= q;
				}
				else
				{
						t += process[i].left;
						process[i].left = 0;
						process[i].finish = t;
						process[i].state = 1;
				}
				i = (++i) %N;
		}

		for(i = 0 ; i < N ; i++)
		{
				process[i].T = process[i].finish - process[i].arrive;
				process[i].W = process[i].T / (float)process[i].service;
				sumT += process[i].T;
				sumW += process[i].W;
		}

		RR_T = sumT / N;
		RR_W = sumW / N; 
}