-
Notifications
You must be signed in to change notification settings - Fork 1
/
non-preemptive-priority.js
102 lines (78 loc) · 2.69 KB
/
non-preemptive-priority.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import Queue from '../queue.js';
function arrivedProcesses(readyQueue, timer) {
// create a new array containing the process that are arrived and also not terminated
const arrived = readyQueue.list.filter((process) => {
if (process.arrivalTime <= timer && !process.terminated) {
return process;
}
});
return arrived;
}
function maximumPriority(readyQueue) {
// find the maximum priority of all process currently arrived
const maximum = Math.max(...readyQueue.map((process) => process.priority));
// return the process having the maximum priority (calculated above)
for (let index = 0; index < readyQueue.length; index++) {
const process = readyQueue[index];
if (process.priority === maximum) {
return process;
}
}
}
function priority(processes, contextSwitchingTime = 0) {
// timer will keep track of the time each process is ran
// timer will start from 0
let timer = 0;
// create a copy of original data
let tempProcesses = processes.map((process) => {
return { ...process };
});
// creating a ready queue
// sort the processes according to the arival times
let readyQueue = new Queue(tempProcesses);
// output array is used to display the output
let output = [];
// iterating over each process
while (!readyQueue.isEmpty()) {
// get the process from the ready queue
let process = readyQueue.peek();
// check if the process arrived or not
if (process.arrivalTime > timer) {
timer++;
continue;
}
// check for context switching time and increment the timer
timer += contextSwitchingTime;
process = maximumPriority(arrivedProcesses(readyQueue, timer));
if (!process) {
timer++;
continue;
}
// check if the process arrived or not
if (process.arrivalTime > timer) {
timer++;
continue;
}
// give the response time
if (process.responseTime === null || process.responseTime === undefined) {
process.responseTime = timer - process.arrivalTime;
}
// run the process for the CPU Burst time
timer += process.estimateCPUBurstTime;
// process completion time
process.completionTime = timer;
// turn-around time
process.turnAroundTime = process.completionTime - process.arrivalTime;
// waiting time
process.waitingTime = process.turnAroundTime - process.estimateCPUBurstTime;
// terminate the process
process.terminated = true;
// remove from ready queue
readyQueue.removeProcess(process);
// put in the output array
output.push(process);
}
console.table(output.sort((a, b) => a.arrivalTime - b.arrivalTime));
return output.sort((a, b) => a.arrivalTime - b.arrivalTime);
}
export default priority;