Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save SomeBottle/479b4b87bb86f8ad037ae6d0214f7063 to your computer and use it in GitHub Desktop.

Select an option

Save SomeBottle/479b4b87bb86f8ad037ae6d0214f7063 to your computer and use it in GitHub Desktop.
Processor Scheduling - Priority Scheduling Algo (non-preemptive)(优先级调度算法(非抢占式))
'use strict';
// PSA,优先级调度算法(非抢占式)
let arr = [['A', 10, 3], ['B', 6, 5], ['C', 2, 2], ['D', 4, 1], ['E', 8, 4]], // [进程名,运行时间,优先级(数字越大越优先)]
overall = 0;
arr.sort((x, y) => y[2] - x[2]);
for (let i = 0, len = arr.length; i < len; i++) {
let item = arr[i],
spend = item[1],
sumBefore = 0;
for (let j = 0; j < i; j++) {
sumBefore += arr[j][1];
}
let illustration = ' '.repeat(sumBefore) + '#'.repeat(spend) + ` %c${spend}ms`;
console.log(`${item[0]}: ${illustration}`, 'color:#848484');
overall = overall + spend + sumBefore;
}
let average = overall / arr.length;
console.log(`Final result: ${average}`);
@SomeBottle
Copy link
Author

SomeBottle commented Apr 3, 2022

这个是非抢占式优先级调度算法

  • 这是一个非抢占式(也有抢占式)算法
  • 适用于进程调度作业调度I/O调度
  • 会造成进程饥饿

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment