操作系统|操作系统 作业调度的三种方式 c++实现(代码加结果)
代码实现的算法有三种,第一种是短作业优先(Short Job First)算法,第二种是先来先服务(First Come First Served)算法,第三种是高响应比优先调度(Highest Response Ration Next)算法。最下方会有代码结果截图。
代码如下,
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define MAX_SIZE 6
struct Job {
int id;
double run_time;
double arrive_time;
//ms
double wait_time;
//ms
double level;
string state;
//{ "ready","running","finished" };
};
Job job[MAX_SIZE];
Job queue[MAX_SIZE];
Job queue1[MAX_SIZE];
Job temp;
int n;
void CreateJob();
void FCFServed();
void ShortJobFirst();
void HRRN();
void Print();
int main() {
cin >> n;
CreateJob();
Print();
//FCFServed();
//ShortJobFirst();
HRRN();
system("pause");
//return 0;
}
void Print() {
for (int i = 0;
i < n;
i++) {
cout << "ID" << "" << "RUNTIME" << "" << "ARRIVINGTIME" << endl;
cout << job[i].id << "";
cout << job[i].run_time << "";
cout << job[i].arrive_time << endl;
}
cout << endl;
}
void CreateJob() {
for (int i = 0;
i < MAX_SIZE;
i++) {
job[i].id = -1;
}
for (int i = 0;
i < MAX_SIZE;
i++) {
queue[i].id = -1;
}//INIT
for (int i = 0;
i < MAX_SIZE;
i++) {
queue1[i].id = -1;
}//INIT
for (int i = 0;
i < n;
i++) {
job[i].id = i;
cin >> job[i].run_time >> job[i].arrive_time;
job[i].state = "";
}
for (int i = 0;
i < n;
i++) {//冒泡
for (int j = 0;
j job[j + 1].arrive_time) {
temp.id = job[j].id;
temp.arrive_time = job[j].arrive_time;
temp.run_time = job[j].run_time;
job[j].id = job[j + 1].id;
job[j].arrive_time = job[j + 1].arrive_time;
job[j].run_time = job[j + 1].run_time;
job[j + 1].id = temp.id;
job[j + 1].arrive_time = temp.arrive_time;
job[j + 1].run_time = temp.run_time;
}
}
}
}
void FCFServed() {
int p = 0, p1 = 0;
double starttime = 0;
double finishtime = 0;
cout << "Starting the FCFS..." << endl;
cout << endl;
cout << "JobId" << "arrive_time" << "run_time" << "start_time" << "finish_time" << endl;
Job temp;
int counting = 0;
while (pfinishtime) {
starttime = job[p].arrive_time;
}
else {
starttime = finishtime;
}
}
if (p == 0) {
starttime = job[p].arrive_time;
}
finishtime = starttime + job[p].run_time;
cout << job[p].id << "" << job[p].arrive_time << "" << job[p].run_time << "" << starttime << "" << finishtime << endl;
for (int i = 0;
i < n;
i++) {//此时进入后备序列的作业 入口
if (job[i].arrive_time <= finishtime&&job[i].state == "") {
queue[p1].id = job[i].id;
queue[p1].run_time = job[i].run_time;
queue[p1].arrive_time = job[i].arrive_time;
p1++;
//后备作业个数
}//if
}//for
int p3 = 0;
bool T = true;
while (p1 != 0) {
i = 0;
if (p1 != 0) {
while (T) {
if (queue[i].id != -1) {
temp.id = queue[i].id;
temp.arrive_time = queue[i].arrive_time;
temp.run_time = queue[i].run_time;
//
T = false;
}
i++;
}
T = true;
while (p2= queue[p2].run_time&&queue[p2].id != -1) {
temp.id = queue[p2].id;
temp.arrive_time = queue[p2].arrive_time;
temp.run_time = queue[p2].run_time;
//用于遍历
//p3 ++;
}//if
p2++;
}//while3
int c = 0;
for (int i = 0;
i < n;
i++) {
if (job[i].id == temp.id) {
c = i;
}
}
for (int i = 0;
i < n;
i++) {
if (queue[i].id == temp.id) {
p3 = i;
}
}
job[c].state = "ready";
job[c].state = "running";
Sleep(100 * job[c].run_time);
//op
job[c].state = "finished";
starttime = finishtime;
finishtime = starttime + job[c].run_time;
queue[p3].id = -1;
p1--;
cout << job[c].id << "" << job[c].arrive_time << "" << job[c].run_time << "" << starttime << "" << finishtime << endl;
p2 = 0;
for (int i = 0;
i < MAX_SIZE;
i++) {
if (queue[i].id != -1) {
queue[i].state = "ready";
for (int j = 0;
j < MAX_SIZE;
j++) {
if (job[j].id == queue[i].id) {
job[j].state = "ready";
}
}
}
}
for (int i = 0;
i < n;
i++) {//此时进入后备序列的作业
if (job[i].arrive_time < finishtime&&job[i].state == "") {
queue[p1].id = job[i].id;
queue[p1].run_time = job[i].run_time;
queue[p1].arrive_time = job[i].arrive_time;
p1++;
//后备作业个数
}//if
}//for
p3 = 0;
}//if
}
i = 0;
}//while2
p++;
}//while1
}
void HRRN() {
int p = 0, p1 = 0, p3 = 0;
double finishtime = 0, starttime = 0;
cout << "Starting the HRRN..." << endl;
cout << endl;
cout << "JobId" << "arrive_time" << "run_time" << "start_time" << "finish_time" << "priority" << endl;
while (p < n) {
if (job[p].state == "") {
job[p].state = "ready";
job[p].state == "running";
Sleep(100 * job[p].run_time);
//op
job[p].state == "finished";
job[p].state = "finished";
if (p != 0) {
if (job[p].arrive_time>finishtime) {
starttime = job[p].arrive_time;
}
else {
starttime = finishtime;
}
}
if (p == 0) {
starttime = job[p].arrive_time;
}
finishtime = starttime + job[p].run_time;
cout << job[p].id << "" << job[p].arrive_time << "" << job[p].run_time << "" << starttime << "" << finishtime << "" << job[p].level << endl;
for (int i = 0;
i < n;
i++) {//此时进入后备序列的作业 入口
if (job[i].arrive_time < finishtime&&job[i].state == "") {
queue1[p1].id = job[i].id;
queue1[p1].run_time = job[i].run_time;
queue1[p1].arrive_time = job[i].arrive_time;
p1++;
//后备作业个数
}//if
}//for
bool t = true;
int p2 = 0;
double maxl = 0;
while (p1 != 0) {
while (p2
代码结果:
FCFS:
文章图片
SJF:
文章图片
HRRN:
文章图片
【操作系统|操作系统 作业调度的三种方式 c++实现(代码加结果)】有什么不理解或者觉得可以改进的地方,请大家多多和我讨论以及指正!
推荐阅读
- 操作系统|[译]从内部了解现代浏览器(1)
- 17|17 关山松 第二课作业#公众号项目# D20
- 【同心同舵】郑友贤第八季思维导图武林计划No.15《点评作业5》
- 特殊的家庭作业。
- 作业没有完成仍坚持要开家庭会议|作业没有完成仍坚持要开家庭会议 44
- 2019年《家庭中的正面管教》作业七
- --木木--|--木木-- 第二课作业#翼丰会(每日一淘6+1实战裂变被动引流# 6+1模式)
- 2018-04-16动画练习作业
- 超级行动力第二次作业-行动大于学习的秘密
- 第一周作业复盘