单处理器进程调度算法的模拟

#include <iostream>
#include <list>
#include <ctime>
#include <vector>
#include <algorithm>
#include <queue>

using namespace std;
const int MAX_PROCESS_NUMBER = 15;


class Process {
public:
	int id;
	int arrival;//到达时间
	int CPUtime;//所需执行时间
	int cost;//已执行时间
	int state;//状态码(0:就绪态,1:运行态,2:阻塞态,3:完成)
	int end;//完成时间
	int during_time;//周转时间
	double response_ratio;
	Process() {}
	void set(int id, int arrival, int cpu, int end=0, int during = 0, int state = 0, double ratio=0, int cost=0) {
		this->id = id;
		this->arrival = arrival;
		CPUtime = cpu;
		during_time = during;
		this->end = end;
		this->state = state;
		response_ratio = ratio;
		this->cost = cost;
	}
	//用于构造优先队列,这里比较的是优先级,CPUtime越小优先级越高
	bool operator <(const Process that) const {
		return (this->CPUtime > that.CPUtime);
	}
};
Process processes[MAX_PROCESS_NUMBER+1];

list<Process> rr;
//声明函数
void fcfs_algorithm();
void rr_algorithm();
void spn_algorithm();
void srt_algorithm();
void hrrn_algorithm();
//hrrn算法中的比较函数
struct cmp {
	bool operator()(Process &a, Process &b)const {
		return a.during_time / a.CPUtime < b.during_time / b.CPUtime;
	}
};

int main() {
	srand((unsigned)time(NULL));
	//生成我们模拟的进程,初始化设置到达时间0,2,4,6,8...执行时间2-10;完成时间0,周转时间0,状态就绪态。
	processes[1].set(1, 0, rand() % 10 + 1);
	for (int i = 2; i <=MAX_PROCESS_NUMBER; i++) {
		processes[i].set(i, 2*(i-1), rand() % 9 + 2);
	}

	for (int i = 1; i <= MAX_PROCESS_NUMBER; i++) {
		cout << "procedure" << i << " 到达时间 = " << processes[i].arrival << " 所需服务时间 = " << processes[i].CPUtime << endl;
	}
	//模拟RR调度
	rr_algorithm();
	//模拟SRT最短剩余时间
	srt_algorithm();
	//模拟FCFS先来先服务
	fcfs_algorithm();
	//模拟SPN
	spn_algorithm();
	//模拟HRRN
	hrrn_algorithm();

	return 0;
}

void fcfs_algorithm() {
	int cumulative_time = 0;
	//总周转时间
	int during = 0;
	cout << "FCFS:" << endl;
	for (int i = 1; i <= MAX_PROCESS_NUMBER; i++) {
		cumulative_time += processes[i].CPUtime;
		processes[i].end = cumulative_time;
		processes[i].during_time = processes[i].end - processes[i].arrival;
		during += processes[i].during_time;
		processes[i].response_ratio = (double)processes[i].during_time / processes[i].CPUtime;
		cout << "process" << i << "  完成时间:" << processes[i].end <<
			"\t周转时间:" << processes[i].during_time << 
			"\t响应比:" << processes[i].response_ratio << endl;
	}
	cout << "平均周转时间:" << (double)during / MAX_PROCESS_NUMBER<<endl;
}

void rr_algorithm() {
	cout << "RR调度算法:" << endl;
	int cumulative_time = 0;
	int during = 0;
	int next = 2;//下一个即将被push的process
	rr.push_back(processes[1]);
	list<Process>::iterator it = rr.begin();
	while (!rr.empty()) {
		++cumulative_time;
		if (cumulative_time % 2 == 0 && next <= MAX_PROCESS_NUMBER) {
			rr.push_back(processes[next++]);
		}
		if (++it == rr.end())
			it = rr.begin();
		++(it->cost);
		if (it->CPUtime == it->cost) {	
			it->end = cumulative_time;
			it->during_time = it->end - it->arrival;
			during += it->during_time;
			it->response_ratio = (double)it->during_time / it->CPUtime;
			cout << "process" << it->id << "  完成时间:" <<it->end <<
				"\t周转时间:" << it->during_time <<
				"\t响应比:" << it->response_ratio << endl;
			
			list<Process>::iterator temp = it;
			if (it != rr.begin())
				--it;
			else if(rr.size()>1) {
				while (++it != rr.end());
				--it;
			}
			temp->cost = 0;
			rr.erase(temp);
		}
	}
	cout << "平均周转时间:" << (double)during / MAX_PROCESS_NUMBER << endl;
}

void spn_algorithm() {

	int cumulative_time = 0;
	//总周转时间
	int during = 0;
	Process current;
	current = processes[1];
	int next = 2;
	cout << "SPN:" << endl;
	priority_queue<Process> spn;
	while (1) {
		++cumulative_time;
		if (cumulative_time % 2 == 0 && next <= MAX_PROCESS_NUMBER) {
			spn.push(processes[next++]);
		}
		current.cost++;
		if (current.cost == current.CPUtime) {
			current.end = cumulative_time;
			current.during_time = current.end - current.arrival;
			during += current.during_time;
			current.response_ratio = (double)current.during_time / current.CPUtime;
			cout << "process" << current.id << "  完成时间:" << current.end <<
				"\t周转时间:" << current.during_time <<
				"\t响应比:" << current.response_ratio << endl;
			current.cost = 0;
			if (!spn.empty()) {
				current = spn.top();
				spn.pop();
			}
			else
				break;
		}
	}
	
	cout << "平均周转时间:" << (double)during / MAX_PROCESS_NUMBER << endl;
}

void srt_algorithm() {
	//这个算法中的process中的cost的用法有点变化,CPUtime的用法也有变化。
	for (int i = 1; i <= MAX_PROCESS_NUMBER; i++) {
		processes[i].cost = processes[i].CPUtime;
	}
	int cumulative_time = 0;
	//总周转时间
	int during = 0;
	Process current;
	current = processes[1];
	current.cost = current.CPUtime;
	int next = 2;
	cout << "SRT:" << endl;
	priority_queue<Process> spn;
	while (1) {
		++cumulative_time;
		if (cumulative_time % 2 == 0 && next <= MAX_PROCESS_NUMBER) {
			spn.push(processes[next++]);
		}
		current.CPUtime--;
		if (current.CPUtime==0) {
			current.end = cumulative_time;
			current.during_time = current.end - current.arrival;
			during += current.during_time;
			current.response_ratio = (double)current.during_time / current.cost;
			cout << "process" << current.id << "  完成时间:" << current.end <<
				"\t周转时间:" << current.during_time <<
				"\t响应比:" << current.response_ratio << endl;
			//current.CPUtime = current.cost;
			if (!spn.empty()) {
				current = spn.top();
				spn.pop();
			}
			else
				break;
		}
		if (cumulative_time % 2 == 0 && next <= MAX_PROCESS_NUMBER) {
			spn.push(current);
			current = spn.top();
			spn.pop();
		}
	}

	cout << "平均周转时间:" << (double)during / MAX_PROCESS_NUMBER << endl;
	//这个算法中的process中的cost的用法有点变化,CPUtime的用法也有变化。
	for (int i = 1; i <= MAX_PROCESS_NUMBER; i++) {
		processes[i].cost = 0;
	}
}

void hrrn_algorithm() {
	int cumulative_time = 0;
	//总周转时间
	int during = 0;
	Process current;
	current = processes[1];
	int next = 2;
	cout << "HRRN:" << endl;

	priority_queue<Process, vector<Process>, cmp> spn;
	while (1) {
		++cumulative_time;
		if (cumulative_time % 2 == 0 && next <= MAX_PROCESS_NUMBER) {
			spn.push(processes[next++]);
		}
		current.cost++;
		if (current.cost == current.CPUtime) {
			current.end = cumulative_time;
			current.during_time = current.end - current.arrival;
			during += current.during_time;
			current.response_ratio = (double)current.during_time / current.CPUtime;
			cout << "process" << current.id << "  完成时间:" << current.end <<
				"\t周转时间:" << current.during_time <<
				"\t响应比:" << current.response_ratio << endl;
			current.cost = 0;
			if (!spn.empty()) {
				vector<Process> temp;
				while (!spn.empty()) {
					Process temp1 = spn.top();
					temp1.during_time = cumulative_time - temp1.arrival;
					temp.push_back(temp1);
					spn.pop();
				}
				while (!temp.empty()) {
					spn.push(temp.back());
					temp.pop_back();
				}
				current = spn.top();
				spn.pop();
			}
			else
				break;
		}
	}
	cout << "平均周转时间:" << (double)during / MAX_PROCESS_NUMBER << endl;
}

实验内容: 编写一个单处理机下的进程调度程序,模拟操作系统对进程调度。 要求: 能够创建指定数量的进程,每个进程由一个进程控制块表示。 实现先来先服务调度算法进程到达时间可由进程创建时间表示。 实现短作业优先调度算法:可指定进程要求的运行时间。(说明:对不可剥夺的短作业优先算法,当作业运行时间相等时,优先调度进程号小的进程执行;对可剥夺式的短作业优先算法,即选最短剩余时间的进程进行运行,在剩余时间相同的情况下,选择到达时间早的进程进行运行) 实现时间片轮转调度算法:可指定生成时间片大小。(说明:新进程到来时插入到就绪队列的队尾,当进程P运行完一个时间片时,若同时有进程Q到达,则先在就绪队列队尾插入新到达的进程Q,之后再插入进程P) 实现动态优先级调度算法:可指定进程的初始优先级(优先级与优先数成反比,优先级最高为0),优先级改变遵循下列原则:进程在就绪队列中每停留一个时间片,优先级加1,进程每运行一个时间片,优先级减3。(说明:本算法在优先级相同的情况下,选择到达时间早的进程进行运行) 测试用例格式如下: 输入:调度算法    进程号/到达时间/运行时间/优先级/时间片 输出:调度顺序/进程号/开始运行时间/结束运行时间/优先级 其中调度算法选项为:1----先来先服务,2----短作业优先,3----最短剩余时间优先,4----时间片轮转,5----动态优先级
编写一个单处理机下的进程调度程序,模拟操作系统对进程调度。 要求: 1.能够创建指定数量的进程,每个进程由一个进程控制块表示。 2.实现先来先服务调度算法进程到达时间可由进程创建时间表示。 3.实现短作业优先调度算法:可指定进程要求的运行时间。(说明:对不可剥夺的短作业优先算法,当作业运行时间相等时,优先调度进程号小的进程执行;对可剥夺式的短作业优先算法,即选最短剩余时间的进程进行运行,在剩余时间相同的情况下,选择到达时间早的进程进行运行) 4. 实现时间片轮转调度算法:可指定生成时间片大小。(说明:新进程到来时插入到就绪队列的队尾,当进程P运行完一个时间片时,若同时有进程Q到达,则先在就绪队列队尾插入新到达的进程Q,之后再插入进程P) 5.实现动态优先级调度算法:可指定进程的初始优先级(优先级与优先数成反比,优先级最高为0),优先级改变遵循下列原则:进程在就绪队列中每停留一个时间片,优先级加1,进程每运行一个时间片,优先级减3。(说明:本算法在优先级相同的情况下,选择到达时间早的进程进行运行) 测试用例格式如下: 输入:调度算法    进程号/到达时间/运行时间/优先级/时间片 输出:调度顺序/进程号/开始运行时间/结束运行时间/优先级 其中调度算法选项为:1----先来先服务,2----短作业优先,3----最短剩余时间优先,4----时间片轮转,5----动态优先级
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值