Thanks to visit codestin.com
Credit goes to www.scribd.com

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

SRTF

Uploaded by

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

SRTF

Uploaded by

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

#include <bits/stdc++.

h>
using namespace std;

int main() {
int n;
cout << "Enter the number of processes : ";
cin >> n;

vector<int> pID(n), bt(n), At(n), rbt(n), wt(n, 0), tt(n, 0), ct(n, 0);

cout << "Enter the Process IDs with their corresponding burst time and arrival time : " <<
endl;
for (int i = 0; i < n; i++) {
cin >> pID[i] >> bt[i] >> At[i];
rbt[i] = bt[i];
}

int completed = 0, current_time = 0;

while (completed != n) {
int idx = -1;

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


if (At[i] <= current_time && rbt[i] > 0) {
if (idx == -1 || rbt[i] < rbt[idx]) {
idx = i;
} else if (rbt[i] == rbt[idx]) {
if (At[i] < At[idx]) {
idx = i;
} else if (At[i] == At[idx] && pID[i] < pID[idx]) {
idx = i;
}
}
}
}

if (idx != -1) {
rbt[idx]--;
current_time++;

if (rbt[idx] == 0) {
ct[idx] = current_time;
tt[idx] = ct[idx] - At[idx];
wt[idx] = tt[idx] - bt[idx];
completed++;
}
} else {
current_time++;
}
}

int total_wt = 0, total_tt = 0;

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


total_wt += wt[i];
total_tt += tt[i];
cout << "Process ID : " << pID[i] << endl;
cout << "Burst Time : " << bt[i] << endl;
cout << "Arrival Time : " << At[i] << endl;
cout << "Completion Time : " << ct[i] << endl;
cout << "Turnaround Time : " << tt[i] << endl;
cout << "Waiting Time : " << wt[i] << endl<<endl<<endl;
}

float avg_wt = (float)total_wt / n;


float avg_tt = (float)total_tt / n;

cout << fixed << setprecision(2);


cout << "Average Waiting Time : " << avg_wt << endl;
cout << "Average Turnaround Time: " << avg_tt << endl;

You might also like