function Pcb(name, priority, time) {
this.name = name;
this.priority = priority;
this.time = time;
this.state = true;
}
Pcb.prototype.run = function() {
this.priority -= 1;
this.time -= 1;
if (this.time === 0) {
this.state = false;
}
}
pcb.prototype.allowRun = function() {
return this.state;
}
const System = {
queue: [],
add(name, priority, time) {
let pcb = new Pcb(name, priority, time);
this.queue.push(pcb);
return this.queue;
},
sort() {
this.queue.sort((a, b) => {
return b.priority - a.priority;
})
},
isEmpty() {
if (this.queue.length) {
return false;
}
return true;
},
getPcb() {
if (isEmpty) {
return null;
}
this.sort();
let pcb = this.queue[0];
return pcb;
},
removePcb() {
return this.queue.shift();
},
displayChange() {
this.queue.forEach(pcb => {
console.log(`进程名: ${pcb.name}, 优先级: ${pcb.priority}, 运行时间: ${pcb.time}`);
})
console.log('-----------------------------------------');
},
run() {
let pcb = this.getPcb();
if (!pcb) {
return false;
}
pcb.run();
if (!pcb.allowRun) {
this.removePcb();
}
return true;
},
start() {
let isNext = true;
while (isNext) {
isNext = this.run();
}
}
}
System.add('张一铭', 3, 2);
System.add('赵浪', 1, 3);
System.add('杨恒', 4, 1);
System.add('张力', 1, 1);
System.add('太学了', 1, 2);