1 回答
TA贡献1801条经验 获得超8个赞
这似乎需要迭代来确定流程的顺序,因为正在“执行”的当前流程的完成时间用于确定下一个要考虑执行的可用流程。也就是说,在您的示例中,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', ArrivalTime: 2, BurstTime: 6 },
{ Process: 'P5', ArrivalTime: 3, BurstTime: 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 之前滑入......
添加回答
举报