-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPSJF.c
40 lines (35 loc) · 1.53 KB
/
PSJF.c
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
#include "utils.h"
#include "data_struct.h"
void PSJF_scheduler(Process proc_list[], int N)
{
int left_jobs = N;
int ready_p = 0;
int running_p = -1;
for (int time = 0; left_jobs > 0; time++) {
// start all process whose ready time <= time
while (ready_p < N && proc_list[ready_p].ready_time <= time) {
proc_start(&proc_list[ready_p]);
++ready_p;
}
int mn_p = get_min_remaining_time_p(proc_list, N);
// if CPU is available, assign a process to it
if (mn_p != -1 && running_p == -1) {
running_p = mn_p;
proc_wakeup(&proc_list[running_p]);
} else if (mn_p != -1 && proc_list[mn_p].remaining_time < proc_list[running_p].remaining_time) {
proc_block(&proc_list[running_p]);
running_p = mn_p;
proc_wakeup(&proc_list[running_p]);
}
TIME_UNIT;
// if there is process running, decrease its remaining time
if (running_p != -1) {
--proc_list[running_p].remaining_time;
if (proc_list[running_p].remaining_time == 0) {
proc_term(&proc_list[running_p]);
running_p = -1;
--left_jobs;
}
}
}
}