1 |
- (window.webpackJsonp=window.webpackJsonp||[]).push([[59],{349:function(t,s,a){"use strict";a.r(s);var n=a(14),r=Object(n.a)({},(function(){var t=this,s=t._self._c;return s("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[s("h1",{attrs:{id:"非抢占式最短作业优先算法"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#非抢占式最短作业优先算法"}},[t._v("#")]),t._v(" 非抢占式最短作业优先算法")]),t._v(" "),s("h2",{attrs:{id:"调度策略与算法原理"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#调度策略与算法原理"}},[t._v("#")]),t._v(" 调度策略与算法原理")]),t._v(" "),s("p",[t._v("\tSJF(Shortest Job First,最短作业优先)是一种进程调度策略,它选择执行时间最短的进程来优先执行。在SJF调度策略下,操作系统会根据进程的执行时间长度来决定调度顺序,短作业将被优先执行。")]),t._v(" "),s("p",[t._v("基本原则和策略:")]),t._v(" "),s("ol",[s("li",[t._v("作业长度:SJF调度策略假设我们可以预测每个进程的执行时间。因此,每个进程都被分配了一个执行时间长度。")]),t._v(" "),s("li",[t._v("最短作业优先:在就绪队列中,选择具有最短执行时间的进程来执行。这意味着进程的执行时间越短,越容易获得CPU的执行机会。")]),t._v(" "),s("li",[t._v("非抢占式调度:一旦进程开始执行,它将一直运行到完成,除非它自愿释放CPU或阻塞。")]),t._v(" "),s("li",[t._v("先来先服务:当多个进程具有相同的执行时间时,采用先到先服务(FIFO)原则,即按照进程到达的顺序来决定执行顺序。")])]),t._v(" "),s("p",[t._v("\tSJF调度策略旨在减少平均等待时间,因为短作业往往能够更快地完成并释放CPU资源,从而提高系统的吞吐量和响应时间。然而,SJF调度策略需要提前了解每个进程的执行时间,而在实际情况中,通常无法准确预测进程的执行时间。因此,SJF调度策略往往用作一种参考,而非实际应用的主要调度算法。")]),t._v(" "),s("p",[t._v("\t扩展:抢占式最短作业优先算法是:最短完成时间优先(Shortest Time to Complet First)调度策略。")]),t._v(" "),s("h2",{attrs:{id:"实验步骤"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#实验步骤"}},[t._v("#")]),t._v(" 实验步骤")]),t._v(" "),s("p",[t._v("\t接上文,在这里继续完成调度算法的实现")]),t._v(" "),s("p",[t._v("\t你只需要实现一个SJF算法类,然后实现schedule函数,比如下面的方法:")]),t._v(" "),s("div",{staticClass:"language-cpp extra-class"},[s("pre",{pre:!0,attrs:{class:"language-cpp"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("bool")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("compareByBurstTime")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("const")]),t._v(" Process"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v(" p1"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("const")]),t._v(" Process"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v(" p2"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" p1"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("burst_time "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("<")]),t._v(" p2"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("burst_time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("void")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("schedule")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("Process processes"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("int")]),t._v(" n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 按照执行时间排序进程")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("sort")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("processes"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" processes "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("+")]),t._v(" n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" compareByBurstTime"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("int")]),t._v(" current_time "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("0")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("for")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("int")]),t._v(" i "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("0")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v(" i "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("<")]),t._v(" n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v(" i"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("++")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n Process currentProcess "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" processes"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("i"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n\n cout "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("<<")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"执行进程 "')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("<<")]),t._v(" currentProcess"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("pid "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("<<")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('",执行时间 "')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("<<")]),t._v(" currentProcess"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("burst_time "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("<<")]),t._v(" endl"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n\n current_time "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("+=")]),t._v(" currentProcess"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("burst_time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n\n cout "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("<<")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"进程 "')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("<<")]),t._v(" currentProcess"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("pid "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("<<")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('" 执行完毕,完成时间 "')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("<<")]),t._v(" current_time "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("<<")]),t._v(" endl"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"实验结果"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#实验结果"}},[t._v("#")]),t._v(" 实验结果")])])}),[],!1,null,null,null);s.default=r.exports}}]);
|