1、进程调度算法实验报告作业调度一、 实验名称作业调度算法二、实验目标在单道环境下编写作业调度的模拟程序,以加深对作业调度的理解。单道环境的特点使被调度的作业占有所有的资源。实现的算法有先来先服务,最短作业优先,最高响应比三种作业调度算法。三、实验环境要求:1.PC机。2.Windows;3.CodeBlocks 四、实验基本原理1.本实验设计一个可指定作业个数的作业调度系统。可以输出先来先服务,最短作业优先,最高响应比三种作业调度算法的结果。2.先来先服务就是按照各个作业进入系统的自然次序进行调度。最短作业优先就是优先调度并且处理短作业。最高响应比优先就是根据在程序运行过程中的最高响应比对应的作
2、业先进行调度处理。3.在设计程序过程中,将time相关的内容封装到类中,重载了加减乘除和输入输出以及比较运算符,方便12:00这种形式的数据的加减乘除运算和比较运算, 五、数据结构设计1.时间类class time public: time(int x = 0, int y = 0) time:hour = x; time:minute = y; time& operator = (const time &t1) this-hour=t1.hour; this-minute=t1.minute; return *this; time operator + (time t2) int minut
3、es,hours; minutes = (minute + t2.minute) % 60; hours=hour+t2.hour+ (minute + t2.minute) /60; return time(hours,minutes); time operator -(time t2) int minutes,hours; minutes =minute - t2.minute; if (minute0) minutes += 60; hour-; hours = hour - t2.hour; return time(hours,minutes); friend bool operato
4、r (time t1, time t2) if (t1.hour t2.hour) return true; else if(t1.hour=t2.hour) if (t1.minute = t2.minute) return true; return false; friend ostream& operator (ostream& out, const time &t1) return out t1.hour : (istream &in, time &t1) int h, m; scanf(%d:%d, &h,&m); t1.hour = h; t1.minute = m; return
5、 in; public: int hour; int minute; 2.作业内容typedef struct JOB char name20;/Job name time in_time; int run_time; time st_time; time end_time; int round_time;/周转时间 int wait_time; double rounds_time;/带权周转 double reson_radio;/响应比job; 六、流程图 七、源代码#include#include#include#includeusing namespace std;class tim
6、e public: time(int x = 0, int y = 0) time:hour = x; time:minute = y; time& operator = (const time &t1) this-hour=t1.hour; this-minute=t1.minute; return *this; time operator + (time t2) int minutes,hours; minutes = (minute + t2.minute) % 60; hours=hour+t2.hour+ (minute + t2.minute) /60; return time(h
7、ours,minutes); time operator -(time t2) int minutes,hours; minutes =minute - t2.minute; if (minute0) minutes += 60; hour-; hours = hour - t2.hour; return time(hours,minutes); friend bool operator (time t1, time t2) if (t1.hour t2.hour) return true; else if(t1.hour=t2.hour) if (t1.minute = t2.minute)
8、 return true; return false; friend ostream& operator (ostream& out, const time &t1) return out t1.hour : (istream &in, time &t1) int h, m; scanf(%d:%d, &h,&m); t1.hour = h; t1.minute = m; return in; public: int hour; int minute;typedef struct JOB char name20;/Job name time in_time; int run_time; tim
9、e st_time; time end_time; int round_time;/周转时间 int wait_time; double rounds_time;/带权周转 double reson_radio;/响应比job;double sum_time;/全部作业周转时间和double sums_time;/全部作业带权周转时间和int n;/job counts/重载 maxtime maxn(time t1, time t2) if(t1.hourt2.hour) return t1; else if(t1.hour=t2.hour) if (t1.minute t2.hour) r
10、eturn t1; return t2;/eg: 120转化成 2:0time m_exchange_h(int run_time) time run; run.hour = run_time / 60; run.minute = run_time % 60; return run;/eg: 2:0 转化成 120int h_exchange_m(time t1) return t1.hour * 60 + t1.minute;/先来先服务;void job_input(job *x) cout 作业t进入时间t估计运行时间(分钟)t开始时间t结束时间t周转时间(分钟)t带权周转时间 endl
11、; for (int i = 0; i n; i+) cout xi.name t xi.in_timett xi.run_time ttt xi.st_time ttxi.end_time ttxi.round_time tt xi.rounds_time endl; cout 作业平均周转时间 T= sum_time/n endl; cout 作业平均带权周转时间 T= sums_time / n endl;void FCFS(job *x) sum_time = 0; sums_time = 0; time run = m_exchange_h(x0.run_time); x0.st_t
12、ime = x0.in_time; x0.end_time = x0.st_time + run; x0.round_time = h_exchange_m(x0.end_time - x0.in_time); sum_time += x0.round_time; x0.rounds_time = x0.round_time*1.0 / x0.run_time; sums_time += x0.rounds_time; for (int i = 1; i n; i+) xi.st_time = maxn(xi.in_time, xi - 1.end_time); xi.end_time = x
13、i.st_time + m_exchange_h(xi.run_time); xi.round_time = h_exchange_m(xi.end_time - xi.in_time); xi.rounds_time = xi.round_time*1.0 / xi.run_time; sum_time += xi.round_time; sums_time += xi.rounds_time; cout n【先来先服务作业调度算法计算结果】n endl; job_input(x);/短作业优先;void SJF(job *x) int flag100000 ; memset(flag, 0
14、, sizeof(flag); sum_time = 0; sums_time = 0; time run = m_exchange_h(x0.run_time); x0.st_time = x0.in_time; x0.end_time = x0.st_time + run; x0.round_time = h_exchange_m(x0.end_time - x0.in_time); sum_time += x0.round_time; x0.rounds_time = x0.round_time*1.0 / x0.run_time; sums_time += x0.rounds_time
15、; int i = 1;/已经运行的程序 int e = 0;/上一个运行的程序 flag0 = 1; while (i n) int temp = -1; int time_min = 99999999; for (int j = 1; j n; j+) if (xj.in_timexe.end_time&flagj!=1&xj.run_timetime_min) /cout小于:xj.nameendl; time_min = xj.run_time; temp = j; if (temp != -1) xtemp.st_time = xe.end_time; e = temp; else
16、e+; xe.st_time = xe.in_time; xe.end_time = xe.st_time + m_exchange_h(xe.run_time); xe.round_time = h_exchange_m(xe.end_time - xe.in_time); xe.rounds_time = xe.round_time*1.0 / xe.run_time; sum_time += xe.round_time; sums_time += xe.rounds_time; flage = 1; i+; cout n【短作业优先调度算法计算结果】n endl; job_input(x
17、);/最高响应比优先;void HRN(job *x) int flag100000; memset(flag, 0, sizeof(flag); sum_time = 0; sums_time = 0; time run = m_exchange_h(x0.run_time); x0.st_time = x0.in_time; x0.end_time = x0.st_time + run; x0.round_time = h_exchange_m(x0.end_time - x0.in_time); sum_time += x0.round_time; x0.rounds_time = x0
18、.round_time*1.0 / x0.run_time; sums_time += x0.rounds_time; int i = 1;/已经运行的程序 int e = 0;/上一个运行的程序 flag0 = 1; while (i n) int temp = -1; double radio_max = 0; for (int j = 1; j n; j+) if (xj.in_time radio_max) temp = j; radio_max=xj.reson_radio; if (temp != -1) xtemp.st_time = xe.end_time; e = temp;
19、 else e+; xe.st_time = xe.in_time; xe.end_time = xe.st_time + m_exchange_h(xe.run_time); xe.round_time = h_exchange_m(xe.end_time - xe.in_time); xe.rounds_time = xe.round_time*1.0 / xe.run_time; sum_time += xe.round_time; sums_time += xe.rounds_time; flage = 1; i+; cout n【最高响应比调度算法计算结果】n endl; job_i
20、nput(x);int main() cout n; coutendl; job *jobs = new jobn; cout 作业信息输入(请按照进入时间先后顺序输入): endl; for (int i = 0; i n; i+) coutendl; cout 正在读入第 i + 1 个作业: endl; coutendl; cout jobsi.name; cout jobsi.in_time; cout jobsi.run_time; /先来先服务; FCFS(jobs); /短作业优先; SJF(jobs); /最高响应比优先; HRN(jobs); return 0;八、运行结果 九、结果分析 三种算法根据输入得出的输出和书上的结果相符,证明算法实现正确。但是略有不足的是没有实现多道程序下的作业调度,可以进一步补充。十、本次实验体会 通过实验编写作业调度算法,对作业调度的三种算法有了更加深刻的了解。在实现单道环境之余,复习了两道环境下的作业调度。对作业调度有了更加全面的理解。 在实现代码的时候用到了类的重载,复习了C+面向对象知识。 同时实现算法的时候也发生了很多因为粗心导致的bug,在修改代码的过程中,提高了自己编写代码的能力。
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1