操作系统-先来先服务FCFS, 最短作业优先SJF调度算法

main.cpp

#include<iostream>
#include"Scheduling.h"
using namespace std;
int main()
{
	Scheduling scheduling;
	scheduling.simulate();
}

Scheduling.h

#include<iostream>
using namespace std;
const int maxNum = 50;
class Scheduling {
private:
	int processNum=16;
	int arrivalTime[maxNum] = {0,1,2,3,6,7,9,11,12,13,14,20,23,24,25,26};
	int serviceTime[maxNum] = {1,35,10,5,9,21,35,23,42,1,7,5,3,22,31,1};
	int finishTime[maxNum];
	int wholeTime[maxNum];//周转时间
	double weightWholeTime[maxNum];//带权周转时间
	double aveWT_FCFS, aveWT_SJF, aveWWT_FCFS, aveWWT_SJF;//平均周转时间,平均带权周转时间
	int startTime=0;
	int currentTime;
	int finished[maxNum] = {0};
	int n = 0;
public:
	Scheduling() {
		for (int i = 0; i < processNum; i++)
		{
			finishTime[i] = 0;
			wholeTime[i] = 0;
			weightWholeTime[i] = 0;
		}
		aveWT_FCFS = 0;
		aveWT_SJF = 0;
		aveWWT_FCFS = 0;
		aveWWT_SJF = 0;
	}
	void simulate() {
		queue();
		//output();
		FCFS();
		SJF();
		cout << "aveWT_FCFS" << aveWT_FCFS << endl;
		cout << "aveWT_SJF" << aveWT_SJF << endl;
		cout << " aveWWT_FCFS=" << aveWWT_FCFS << endl;
		cout << " aveWWT_SJF=" << aveWWT_SJF << endl;
	}
	void queue()//根据到达时间先后排序,使数组序号和先后到达时间对应。
	{
		for(int i=0;i<processNum-1;i++)
			for (int j = 0; j < processNum - i - 1; j++)
			{
				if (arrivalTime[j] > arrivalTime[j + 1])
				{
					int temp1;
					int temp2;
					temp1 = arrivalTime[j];
					arrivalTime[j] = arrivalTime[j + 1];
					arrivalTime[j + 1] = temp1;
					temp2 = serviceTime[j];
					serviceTime[j] = serviceTime[j + 1];
					serviceTime[j + 1] = temp2;
				}
			}
	}
	void FCFS()
	{
		currentTime = startTime;
		//cout << "current=" << currentTime<<endl;
		for (int i = 0; i < processNum; i++)
		{
			time(i);
			//cout << "current=" << currentTime<<endl;
		}
		averageTime();
	}
	int come_SJF()
	{
		int process=0;
		//cout << "current" << currentTime<<endl;
		for (int i = 0; i < processNum; i++)
		{
			if (finished[i] == 0 && arrivalTime[i] <= currentTime)
			{
				process = i;
				break;
			}
		}
		for (int i = 0; i < processNum; i++)
		{
			if (finished[i] == 0 && arrivalTime[i] <= currentTime && serviceTime[i] < serviceTime[process])
				process = i;
		}
		//递归调用
		finished[process] = 1;
		//cout << process << endl;
		return process;
	}
	void SJF()
	{
		startTime = 0;
		currentTime = startTime;
		for (int i = 0; i < processNum; i++)
		{
			finishTime[i] = 0;
			wholeTime[i] = 0;
			weightWholeTime[i] = 0;
		}
		int currentProcesss = come_SJF();
		for (int i = 0; i < processNum; i++)
		{
			time(currentProcesss);
			currentProcesss = come_SJF();
		}
		averageTime();
	}
	void time(int i)
	{
		finishTime[i] = currentTime + serviceTime[i];
		wholeTime[i] = finishTime[i] - arrivalTime[i];
		weightWholeTime[i] = wholeTime[i] / serviceTime[i];//计算相关时间
		currentTime = finishTime[i];
	}
	void averageTime()
	{
		double sumTime=0;
		for (int i = 0; i < processNum; i++)
		{
			sumTime += wholeTime[i];
		}
		double sumWTime = 0;
		for (int i = 0; i < processNum; i++)
		{
			sumWTime += weightWholeTime[i];
		}
		//cout << "sumTime" << sumTime << endl;
		//cout << "sumWTime" << sumWTime << endl;
		if (n == 0)
		{
			aveWT_FCFS = sumTime / processNum;
			aveWWT_FCFS = sumWTime / processNum;
			n++;
		}
		else
		{
			aveWT_SJF = sumTime / processNum;
			aveWWT_SJF = sumWTime / processNum;
		}
	}
};

posted @ 2021-09-05 16:57  -Rocky-  阅读(122)  评论(0编辑  收藏  举报