C/C++实现操作系统进程调度算法,FCFS, RR, SPN, SRT, HRRN

程序实现了5种进程调度算法,分别是:FCFS、RR(分别令时间片等于1、4)、SPN、SRT、HRRN。

其中,SPN、SRT、HRRN这三种调度算法,需要对进程列表里的进程进行排序,排序之后,再选择列表里的第一个进程执行。不同的调度算法,sort方法的排序规则不同,可以在重载<运算符时,指定这些排序规则。

本程序的优点在于,一旦实现了其中一个调度算法,比如实现了最基本的FCFS之后,只要增加一个排序逻辑,即在重载<运算符时,增加新的排序规则(一个case分支),立马可以实现一种新的调度算法。

进程信息定义在方法InitProcedureInfo中,包括:procedureID(进程ID), arriveTime(进程到达时间), serviceTime(服务时间), eclapsedTime(已服务时间), remainTime(剩余服务时间), accumulateTime(累计服务时间)。

#include 
#include 
using namespace std;

#define PROCEDURENUM 5	
#define MAXTIME 30		

enum scheduleFun { FCFS, RR, SPN, SRT, HRRN };

int currentTime = 0;
int timeSlice = 4;
int scheduleFun;

class Procedure
{
public:
	int procedureID;
	int arriveTime;
	int serviceTime;
	int eclapsedTime;
	int remainTime;
	int accumulateTime;

public:
	Procedure(int _procedureID = 0, int _arriveTime = 0, int _serviceTime = 0, int _eclapsedTime = 0, int _remainTime = 0, int _accumulateTime = 0)
	{
		procedureID = _procedureID;
		arriveTime = _arriveTime;
		serviceTime = _serviceTime;
		eclapsedTime = _eclapsedTime;
		remainTime = _remainTime;
		accumulateTime = _accumulateTime;
	}

	bool operator<(const Procedure& p)
	{
		switch (scheduleFun)
		{
		case SPN:	//SPN:最短进程优先(最短作业优先,非抢占式)
			return this->remainTime - 9999 * this->eclapsedTime < p.remainTime - 9999 * p.eclapsedTime;

		case SRT:	//SRT:最短剩余时间优先(最短作业优先,抢占式)
			return this->remainTime < p.remainTime;  //升序排序
			break;

		case HRRN:	//HRRN:最高响应比优先,R = (w + s) / s = 1 + w / s,其中R表示响应比,w表示已经等待的时间,s表示期待服务的时间(降序排序)
			return 1 + (currentTime - this->arriveTime) / this->serviceTime > 1 + (currentTime - p.arriveTime) / p.serviceTime;
			break;
		}
	}
};

Procedure procedure[PROCEDURENUM];
list procedureList;
int scheduleResult[PROCEDURENUM][MAXTIME];
Procedure departProcedure;
bool departProcedureExsit = false;

void InitProcedureInfo();			//初始化进程信息、进程调度结果、当前时间
void PutArriveProcedureIntoList();	//某个进程到达,将此进程扔进List
void FindProcedure();				//遍历进程List,判断当前执行哪个进程,直接取List中的第一个进程(因为进程List已按要求排序)
void PrintScheduleResult();			//输出进程调度结果
void ProcedureSchedule();			//进程调度算法

int main()
{
	scheduleFun = FCFS;
	ProcedureSchedule();

	scheduleFun = RR;
	timeSlice = 1;
	ProcedureSchedule();

	scheduleFun = RR;
	timeSlice = 4;
	ProcedureSchedule();

	scheduleFun = SPN;
	ProcedureSchedule();

	scheduleFun = SRT;
	ProcedureSchedule();

	scheduleFun = HRRN;
	ProcedureSchedule();

	return 0;
}

//初始化:进程信息、进程调度结果、当前时间
void InitProcedureInfo()
{
	//procedureID, arriveTime, serviceTime, eclapsedTime, remainTime, accumulateTime
	Procedure p1(1, 0, 3, 0, 3, 0);
	Procedure p2(2, 2, 6, 0, 6, 0);
	Procedure p3(3, 4, 4, 0, 4, 0);
	Procedure p4(4, 6, 5, 0, 5, 0);
	Procedure p5(5, 8, 2, 0, 2, 0);
	procedure[0] = p1;
	procedure[1] = p2;
	procedure[2] = p3;
	procedure[3] = p4;
	procedure[4] = p5;

	for (int i = 0; i < PROCEDURENUM; i++)
	{
		for (int j = 0; j < MAXTIME; j++)
		{
			scheduleResult[i][j] = 0;
		}
	}

	currentTime = 0;
}

//输出进程调度结果
void PrintScheduleResult()
{
	for (int i = 0; i < PROCEDURENUM; i++)
	{
		for (int j = 0; j < MAXTIME; j++)
		{
			if (scheduleResult[i][j] == 1)
			{
				cout << "■";
			}
			else
			{
				cout << "  ";
			}
		}
		cout << endl;
	}
}

//某个进程到达,将此进程扔进List
void PutArriveProcedureIntoList()
{
	for (int i = 0; i < PROCEDURENUM; i++)
	{
		if (procedure[i].arriveTime == currentTime)
		{
			procedureList.push_back(procedure[i]);
		}
	}
	if (departProcedureExsit == true)
	{
		procedureList.push_back(departProcedure);
		departProcedureExsit = false;
	}
}

//遍历进程List,判断当前执行哪个进程,直接取List中的第一个进程(因为进程List已按要求排序)
void FindProcedure()
{
	Procedure p;

	if (!procedureList.empty())
	{
		p = procedureList.front();
		procedureList.pop_front();

		if (scheduleFun == FCFS || scheduleFun == SPN || scheduleFun == SRT || scheduleFun == HRRN)
		{
			p.eclapsedTime++;
			p.remainTime--;
			scheduleResult[p.procedureID - 1][currentTime] = 1;

			if (p.remainTime != 0)	//如果当前进程未执行完毕,将其放回List最前面
			{
				procedureList.push_front(p);
			}
		}

		if (scheduleFun == RR)
		{
			if (p.remainTime > 0 && p.accumulateTime < timeSlice)
			{
				scheduleResult[p.procedureID - 1][currentTime] = 1;
				p.eclapsedTime++;
				p.remainTime--;
				p.accumulateTime++;

				if (p.remainTime > 0 && p.accumulateTime < timeSlice)
				{
					procedureList.push_front(p);
				}
				else if (p.remainTime > 0 && p.accumulateTime == timeSlice)
				{
					int flag = 0;

					for (int i = 0; i < PROCEDURENUM; i++)
					{
						if (currentTime == procedure[i].arriveTime - 1)
						{
							flag = 1;
						}
					}

					if (flag == 0)
					{
						p.accumulateTime = 0;
						procedureList.push_back(p);
					}
					else
					{
						p.accumulateTime = 0;
						departProcedure = p;
						departProcedureExsit = true;
					}
				}
			}
		}
	}
}

void ProcedureSchedule()
{
	switch (scheduleFun)
	{
	case FCFS:
		cout << "FCFS算法" << endl << endl;
		break;
	case RR:
		cout << "RR算法 q=" << timeSlice << endl << endl;
		break;
	case SPN:
		cout << "SPN算法" << endl << endl;
		break;
	case SRT:
		cout << "SRT算法" << endl << endl;
		break;
	case HRRN:
		cout << "HRRN算法" << endl << endl;
		break;
	}

	InitProcedureInfo();

	while (currentTime < MAXTIME)
	{
		PutArriveProcedureIntoList();

		if (scheduleFun == SPN || scheduleFun == SRT || scheduleFun == HRRN)
		{
			if (!procedureList.empty())
			{
				procedureList.sort();
			}
		}

		FindProcedure();
		currentTime++;
	}

	PrintScheduleResult();
}

运行结果如下,一次性显示结果,下次做个能动态显示中间过程的。

C/C++实现操作系统进程调度算法,FCFS, RR, SPN, SRT, HRRN_第1张图片

你可能感兴趣的:(数据结构)