OS作業調度FCFS,SJF,HRRN算法的C++實現

FCFS(first come first served):先來先服務,根據到達時間依次執行

SJF(short job first):根據作業的運行時間從小到大依次執行

HRRN(highest response ratio next):根據響應比從大到小依次執行,響應比動態計算


週轉時間 = 完成時間 - 到達時間

帶權週轉時間 = 週轉時間 / 運行時間

響應比 = (運行時間+已經等待時間) / 運行時間 = 1+已經等待時間 / 運行時間

響應比可能表達方法不同,但是意義是一樣的。

注:計算時響應比只用來比較大小,故我直接把+1省略了。如果題目要求計算其值,記得加一。


源代碼如下:

#include <iostream>
#include <algorithm>
using namespace std;
#define MAXSIZE 5		//作業數
struct process
{
	int pid;		//作業號
	double come_time;	//到達時
	double run_time;	//運行時
	double begin_time;	//開始時
	double over_time;	//完成時
	double round_time;	//週轉時
	double avg_time;	//帶權週轉時
	double HRR;			//響應比
}pc[MAXSIZE];		//作業數
bool CmpByComeTime(process p1, process p2) {		//按到達時間正序排序
	return p1.come_time<p2.come_time;
}
bool CmpByPid(process p1, process p2) {		//按id號正序排序
	return p1.pid<p2.pid;
}
bool CmpByRunTime(process p1, process p2) {		//按運行時長正序排序

	return p1.run_time == p2.run_time ? p1.come_time<p2.come_time : p1.run_time<p2.run_time;
}
bool CmpByHRR(process p1, process p2) {		//按響應比逆序排序
	return p1.HRR>p2.HRR;
}
void get_beginAndOver_time() {
	for (int i = 0; i<MAXSIZE; i++) {
		if (i == 0) {
			pc[i].begin_time = pc[i].come_time;		//第一個作業的開始時即爲其到達時

		}
		else {
			pc[i].begin_time = pc[i - 1].over_time;	//否則後一作業的開始時爲前一個作業的完成時
		}
		pc[i].over_time = pc[i].begin_time + pc[i].run_time;		//作業完成時 = 開始時+運行時
	}
}
void get_roundAndAvg_time() {
	for (int i = 0; i < MAXSIZE; ++i)
	{
		pc[i].round_time = pc[i].over_time - pc[i].come_time;		//週轉時 = 完成時 - 到達時
		pc[i].avg_time = pc[i].round_time*1.0 / pc[i].run_time;		//平均週轉時= 週轉時 / 運行時
	}
}
void get_HRR(int beg) {
	for (int i = beg; i<MAXSIZE; i++) {
		pc[i].HRR = (pc[beg - 1].over_time - pc[i].come_time) * 1.0 / pc[i].run_time;
	}
}
void FCFS() {
	sort(pc, pc + MAXSIZE, CmpByComeTime);
	get_beginAndOver_time();
	get_roundAndAvg_time();
}
void SJF() {
	sort(pc, pc + MAXSIZE, CmpByComeTime);		//先按到達時排序
	sort(pc + 1, pc + MAXSIZE, CmpByRunTime);		//再按運行時排序
	get_beginAndOver_time();
	get_roundAndAvg_time();

}
void HRRN() {
	sort(pc, pc + MAXSIZE, CmpByComeTime);		//先按到達時排序
	for (int i = 1; i < MAXSIZE; ++i)
	{
		get_HRR(i);
		sort(pc + i, pc + MAXSIZE, CmpByHRR);
	}
	get_beginAndOver_time();
	get_roundAndAvg_time();
}
void print() {
	cout << "執行順序:" << endl;
	for (int i = 0; i < MAXSIZE; ++i)
	{
		/* code */
		cout << pc[i].pid << " ";
	}
	cout << endl;
	cout << "作業號" << '\t' << "到達時" << '\t' << "運行時" << '\t' << "開始時" << '\t' << "完成時" << '\t' << "週轉時" << '\t' << "帶權週轉時" << '\t' << endl;
	sort(pc, pc + MAXSIZE, CmpByPid);
	double sum_round_time = 0.0;
	double avg_sum_round_time = 0.0;
	double sum_avg_time = 0.0;
	double avg_sum_avg_time = 0.0;
	for (int i = 0; i < MAXSIZE; ++i)
	{
		sum_round_time += pc[i].round_time;
		sum_avg_time += pc[i].avg_time;

		cout << pc[i].pid << '\t' << pc[i].come_time << '\t'
			<< pc[i].run_time << '\t' << pc[i].begin_time << '\t'
			<< pc[i].over_time << '\t' << pc[i].round_time << '\t'
			<< pc[i].avg_time << endl;
	}
	avg_sum_round_time = sum_round_time * 1.0 / MAXSIZE;
	avg_sum_avg_time = sum_avg_time * 1.0 / MAXSIZE;
	cout << "平均週轉時: " << avg_sum_round_time << endl
		<< "平均帶權週轉時: " << avg_sum_avg_time << endl;
}
int main() {
	for (int i = 0; i<MAXSIZE; i++) {
		cin >> pc[i].pid >> pc[i].come_time >> pc[i].run_time;
	}
	FCFS();
	cout << "the results of FCFS are:" << endl;
	print();

	SJF();
	cout << "the results of SJF are:" << endl;
	print();

	HRRN();
	cout << "the results of HRRN are:" << endl;
	print();
	system("pause");
	return 0;
}

運行結果如下:





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章