-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpu.c
148 lines (124 loc) · 6.41 KB
/
cpu.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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <time.h>
#include "oslabs.h"
// Shortest-Remaining-Time-Next Preemptive Scheduler
struct PCB handle_process_arrival_srtp(struct PCB ready_queue[QUEUEMAX], int *queue_cnt,
struct PCB current_process, struct PCB new_process, int time_stamp) {
// If there is no currently-running process (i.e., the third argument is the NULLPCB), then the method returns the
// PCB of the newly-arriving process, indicating that it is the process to execute next.
if (pcb_is_null(current_process)) {
// In this case, the PCB of the new process is modified so that:
// the execution start time set to the current timestamp,
// the execution end time is set to the sum of the current timestamp and the total burst time
// and the remaining burst time is set to the total burst time.
new_process.execution_starttime = time_stamp;
new_process.execution_endtime = time_stamp + new_process.total_bursttime;
new_process.remaining_bursttime = new_process.total_bursttime;
return new_process;
}
// If there is a currently-running process, the method compares the remaining burst time of the
// currently-running process and the total burst time of the newly-arriving process. If the new
// process does not have a shorter burst time, then its PCB is simply added to the ready queue
// and the return value is the PCB of the currently running process. As the newly-arriving process
// is added to the ready queue, its execution start time and execution end time are set to 0, and
// the remaining burst time is set to the total burst time.
if (current_process.remaining_bursttime <= new_process.total_bursttime) {
new_process.execution_starttime = 0;
new_process.execution_endtime = 0;
new_process.remaining_bursttime = new_process.total_bursttime;
ready_queue[*queue_cnt] = new_process;
(*queue_cnt)++;
return current_process;
}
// If the new process has a shorter burst time, then the PCB of the currently-running process is
// added to the ready queue and the return value is the PCB of the new process.
//
// In this case, the PCB of the new process is modified so that:
// the execution start time is set to the current timestamp,
// the execution end time is set to the sum of the current timestamp and the total burst time
// and the remaining burst time is set to the total burst time.
//
// Also, the PCB of the currently-running process is added to the ready queue,
// after marking its execution start time and execution end time as 0, and adjusting its remaining burst time.
else {
current_process.execution_starttime = 0;
current_process.execution_endtime = 0;
current_process.remaining_bursttime = current_process.total_bursttime;
ready_queue[*queue_cnt] = current_process;
(*queue_cnt)++;
new_process.execution_starttime = time_stamp;
new_process.execution_endtime = time_stamp + new_process.total_bursttime;
new_process.remaining_bursttime = new_process.total_bursttime;
return new_process;
}
}
// Shortest-Remaining-Time Preemptive Scheduler
struct PCB handle_process_completion_srtp(struct PCB ready_queue[QUEUEMAX], int *queue_cnt, int timestamp) {
struct PCB next_process = init_pcb(0, 0, 0, 0, 0, 0, 0);
// If the ready queue is empty, the method returns the NULLPCB, indicating that there is no process to execute next.
if (*queue_cnt == 0) {
return next_process;
}
// Otherwise, the method finds the PCB of the process in the ready queue with the smallest remaining burst time,
// removes this PCB from the ready queue and returns it.
int smallest_rbt = 999;
int smallest_rbt_index = 0;
for (int i = 0; i < *queue_cnt; i++) {
if (ready_queue[i].remaining_bursttime < smallest_rbt) {
smallest_rbt = ready_queue[i].remaining_bursttime;
smallest_rbt_index = i;
}
}
next_process = ready_queue[smallest_rbt_index];
for (int i = smallest_rbt_index; i < *queue_cnt - 1; i++) {
ready_queue[i] = ready_queue[i + 1];
}
(*queue_cnt)--;
// Before returning the PCB of the next process to execute, it is modified to set the execution start time as the
// current timestamp and the execution end time as the sum of the current timestamp and the remaining burst time.
next_process.execution_starttime = timestamp;
next_process.execution_endtime = timestamp + next_process.remaining_bursttime;
return next_process;
}
// Round-Robin Scheduler
struct PCB handle_process_arrival_rr(struct PCB ready_queue[QUEUEMAX], int *queue_cnt,
struct PCB current_process, struct PCB new_process, int timestamp,
int time_quantum) {
if (pcb_is_null(current_process)) {
new_process.execution_starttime = timestamp;
new_process.execution_endtime = timestamp + MIN(time_quantum, new_process.total_bursttime);
new_process.remaining_bursttime = new_process.total_bursttime;
return new_process;
}
if (current_process.remaining_bursttime <= new_process.total_bursttime) {
new_process.execution_starttime = 0;
new_process.execution_endtime = 0;
new_process.remaining_bursttime = new_process.total_bursttime;
ready_queue[*queue_cnt] = new_process;
(*queue_cnt)++;
return current_process;
}
}
// Round-Robin Scheduler
struct PCB handle_process_completion_rr(struct PCB ready_queue[QUEUEMAX], int
*queue_cnt, int timestamp, int time_quantum) {
struct PCB next_process = init_pcb(0, 0, 0, 0, 0, 0, 0);
if (*queue_cnt == 0) {
return next_process;
}
int earliest_ats = 999;
int earliest_ats_index = 0;
for (int i = 0; i < *queue_cnt; i++) {
if (ready_queue[i].arrival_timestamp < earliest_ats) {
earliest_ats = ready_queue[i].arrival_timestamp;
earliest_ats_index = i;
}
}
next_process = ready_queue[earliest_ats_index];
for (int i = earliest_ats_index; i < *queue_cnt - 1; i++) {
ready_queue[i] = ready_queue[i + 1];
}
(*queue_cnt)--;
next_process.execution_starttime = timestamp;
next_process.execution_endtime = timestamp + MIN(time_quantum, next_process.remaining_bursttime);
return next_process;
}