如何解决最短作业优先非抢先-同时分类突发时间和到达时间
我对如何同时分类到达时间和突发时间有疑问。我的代码首先检查了较低的突发时间,然后检查了到达时间。
最短的作业优先(非抢先)CPU调度检查进程的突发时间和
如果Process
具有最短的突发时间和到达时间,则它将执行。
这是我对数组进行排序的代码段:
inputs.sort((a1,a2) => (a1.burst < a2.burst) ? 1 : (a1.burst < a2.burst) ? 1 : -1);
这是上面代码段的结果:
如果排序公式正确,这应该是输出:
解决方法
这似乎需要迭代来确定进程的顺序,因为“正在执行”的当前进程的完成时间用于确定考虑下一个执行的可用进程。也就是说,在您的示例中,P1到达0并以5的时间间隔结束。因此,所有在5的时间间隔之前到达的尚未完成的进程现在都是要执行的候选进程。接下来执行突发时间最少的候选,到达时间为决胜局。 (请注意,该平局打破者仅假设该数组按到达时间排序。)
let process = [
{ Process: 'P1',ArrivalTime: 0,BurstTime: 5 },{ Process: 'P2',ArrivalTime: 1,BurstTime: 3 },{ Process: 'P3',ArrivalTime: 2,BurstTime: 8 },{ Process: 'P4',BurstTime: 6 },{ Process: 'P5',ArrivalTime: 3,{ Process: 'P6',ArrivalTime: 15,BurstTime: 2 },];
// Get the first ArrivalTime.
let time = Math.min( ...process.map( p => p.ArrivalTime ) );
while ( process.find( p => p.Finished == null ) ) {
// Now,"execute" the process in which the BurstTime is the least
// amoung the processes which haven't finished and have an ArrivalTime
// less than or equal to the current time...
let execute = process.reduce( ( ni,p,i ) => {
if ( p.Finished == null && p.ArrivalTime <= time && (ni === -1 || p.BurstTime < process[ ni ].BurstTime ) ) {
ni = i;
}
return ni;
},-1 );
// Capture the start time...
process[ execute ].Started = time;
// ...and then calculate the finish time.
time += process[ execute ].BurstTime;
process[ execute ].Finished = time;
}
// For ease of viewing,sort by Started.
process.sort( ( a,b ) => a.Started - b.Started );
console.log( process );
我增加了一些数据点,注意到P6的出现时间较晚,但是由于它的爆发时间只有2,所以可以在P3之前滑入。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。