OS Ass2
OS Ass2
P1 6 ms 0 ms
P2 3 ms 1 ms
P3 7 ms 2 ms
Step-by-Step Execution:
1. Time 0-1 (P1): P1 runs for 1 ms (total time left: 5 ms) as it has
shortest remaining time left.
2. Time 1-4 (P2): P2 runs for 3 ms (total time left: 0 ms) as it has
shortest remaining time left among P1 and P2.
3. Time 4-9 (P1): P1 runs for 5 ms (total time left: 0 ms) as it has
shortest remaining time left among P1 and P3.
4. Time 9-16 (P3): P3 runs for 7 ms (total time left: 0 ms) as it has
shortest remaining time left.
Gantt chart :
1/5
Now, lets calculate average waiting time and turn around time:
Arrival Burst
Time Time Completion Turn Around Waiting
Process (AT) (BT) Time (CT) Time (TAT) Time (WT)
P1 0 6 9 9-0 = 9 9-6 = 3
P2 1 3 4 4-1 = 3 3-3 = 0
P3 2 7 16 16-2 = 14 14-7 = 7
#include <iostream>
#include <vector>
#include <algorithm>
struct Process {
};
int main() {
cin >> n;
vector<Process> p(n);
p[i].id = i + 1;
p[i].remainingTime = p[i].burstTime;
idx = i;
if (idx != -1) {
p[idx].remainingTime--;
currentTime++;
if (p[idx].remainingTime == 0) {
p[idx].completionTime = currentTime;
completed++;
} else {
currentTime++;
totalWT += proc.waitingTime;
totalTAT += proc.turnaroundTime;
cout << "P" << proc.id << " CT: " << proc.completionTime << " WT: " << proc.waitingTime <<
" TAT: " << proc.turnaroundTime << endl;
}
cout << "Avg WT: " << totalWT / n << " Avg TAT: " << totalTAT / n << endl;