0% found this document useful (0 votes)
5 views4 pages

OS Ass2

The document describes a scheduling algorithm for three processes (P1, P2, P3) with their respective burst and arrival times. It details the step-by-step execution of the processes, calculates average waiting and turnaround times, and provides a C++ program to implement the scheduling. The average turnaround time is 8.6 ms and the average waiting time is 3.33 ms.

Uploaded by

maymanalansare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

OS Ass2

The document describes a scheduling algorithm for three processes (P1, P2, P3) with their respective burst and arrival times. It details the step-by-step execution of the processes, calculates average waiting and turnaround times, and provides a C++ program to implement the scheduling. The average turnaround time is 8.6 ms and the average waiting time is 3.33 ms.

Uploaded by

maymanalansare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Consider the following table of arrival time and burst time for three

processes P1, P2 and P3.


Process Burst Time Arrival Time

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

 Average Turnaround time = (9 + 14 + 3)/3 = 8.6 ms


 Average waiting time = (3 + 0 + 7 )/3 = 10/3 = 3.33 ms
Use the online compiler (https://www.onlinegdb.com/online_c++_compiler) to run the
following C++ program.

Use the previous example


Use your own at least two examples and solve them using the program:

#include <iostream>

#include <vector>

#include <algorithm>

using namespace std;

struct Process {

int id, arrivalTime, burstTime, remainingTime, waitingTime, turnaroundTime, completionTime;

};

int main() {

int n, currentTime = 0, completed = 0;

cout << "Enter number of processes: ";

cin >> n;

vector<Process> p(n);

for (int i = 0; i < n; i++) {

p[i].id = i + 1;

cin >> p[i].arrivalTime >> p[i].burstTime;

p[i].remainingTime = p[i].burstTime;

while (completed < n) {

int idx = -1;

for (int i = 0; i < n; i++) {


if (p[i].arrivalTime <= currentTime && p[i].remainingTime > 0 && (idx == -1 ||
p[i].remainingTime < p[idx].remainingTime)) {

idx = i;

if (idx != -1) {

p[idx].remainingTime--;

currentTime++;

if (p[idx].remainingTime == 0) {

p[idx].completionTime = currentTime;

p[idx].turnaroundTime = currentTime - p[idx].arrivalTime;

p[idx].waitingTime = p[idx].turnaroundTime - p[idx].burstTime;

completed++;

} else {

currentTime++;

double totalWT = 0, totalTAT = 0;

for (auto &proc : p) {

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;

You might also like