SJF
SJF
SJF
#include <cstdlib>
#include <iostream>
#include <queue>
using namespace std;
class process {
public:
pid_t p_no = 0;
time_t start_AT = 0, AT = 0,
BT_left = 0, BT = 0, temp_BT = 0,
CT = 0, TAT = 0, WT = 0, RT = 0;
int priority = 0;
while (!ready_queue.empty()) {
while (clock < ready_queue.top().AT) {
p.temp_BT++;
clock++;
}
if (p.temp_BT > 0) {
p.p_no = -1;
p.CT = clock;
(*gantt).push(p);
}
p = pop_index(&ready_queue, min_BT_index(ready_queue, clock));
if (p.AT == p.start_AT)
p.set_RT(clock);
while (p.BT_left > 0 && (ready_queue.empty()
|| clock < ready_queue.top().AT
|| p.BT_left <= min_BT(ready_queue,
clock))) {
p.BT_left--;
p.temp_BT++;
clock++;
}
if (p.BT_left == 0) {
p.AT = p.start_AT;
p.set_CT(clock);
(*gantt).push(p);
p.temp_BT = 0;
completion_queue.push(p);
}
else {
p.AT = clock;
p.CT = clock;
(*gantt).push(p);
p.temp_BT = 0;
ready_queue.push(p);
}
}
return completion_queue;
}
while (!processes.empty()) {
total += processes.top().RT;
processes.pop();
}
return total;
}
while (!spaces.empty()) {
cout << string(to_string(spaces.front().p_no).length() +
(spaces.front().p_no != -1) + 2 * spaces.front().temp_BT, '-') << "+";
spaces.pop();
}
cout << "\n|";
spaces = gantt;
while (!spaces.empty()) {
cout << string(spaces.front().temp_BT, ' ');
if (spaces.front().p_no == -1)
cout << "IS" << string(spaces.front().temp_BT, ' ') << '|';
else
cout << "P" << spaces.front().p_no
<< string(spaces.front().temp_BT, ' ') << '|';
spaces.pop();
}
spaces = gantt;
cout << "\n+";
while (!spaces.empty()) {
cout << string(to_string(spaces.front().p_no).length() +
(spaces.front().p_no != -1) + 2 * spaces.front().temp_BT, '-')
<< "+";
spaces.pop();
}
spaces = gantt;
cout << "\n0";
while (!spaces.empty()) {
temp = to_string(spaces.front().CT).length();
cout << string(to_string(spaces.front().p_no).length() +
(spaces.front().p_no != -1) + 2 * spaces.front().temp_BT
- temp / 2 - prev,
' ')
<< spaces.front().CT;
prev = temp / 2 - temp % 2 == 0;
spaces.pop();
}
// Driver Code
int main()
{
// Initialize Ready and Completion Queue
priority_queue<process> ready_queue, completion_queue;
OUTPUT :-
+-------------+--------------+------------+-----------------+-----------------
+--------------+---------------+
| Process No. | Arrival Time | Burst Time | Completion Time | Turnaround Time |
Waiting Time | Response Time |
+-------------+--------------+------------+-----------------+-----------------
+--------------+---------------+
| 1 | 0 | 4 | 7 | 7 |
3 | 0 |
| 2 | 1 | 2 | 3 | 2 |
0 | 0 |
| 3 | 2 | 3 | 10 | 8 |
5 | 5 |
| 4 | 3 | 5 | 19 | 16 |
11 | 11 |
| 5 | 4 | 1 | 5 | 1 |
0 | 0 |
| 6 | 5 | 4 | 14 | 9 |
5 | 5 |
| 7 | 6 | 6 | 25 | 19 |
13 | 13 |
+-------------+--------------+------------+-----------------+-----------------
+--------------+---------------+
+----+------+----+----+------+--------+----------+------------+--------------+
| P1 | P2 | P1 | P5 | P1 | P3 | P6 | P4 | P7 |
+----+------+----+----+------+--------+----------+------------+--------------+
0 1 3 4 5 7 10 14 19 25