0% found this document useful (0 votes)
0 views1 page

SJF.cpp

Uploaded by

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

SJF.cpp

Uploaded by

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

#include <iostream>

using namespace std;

int main() {
int n, bt[20], p[20], wt[20], tat[20], total_wt = 0, total_tat = 0;

cout << "Enter number of processes: ";


cin >> n;

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


cout << "p" << i + 1 << ": ";
cin >> bt[i];
p[i] = i + 1;
}

// Sort by burst time (SJF)


for (int i = 0; i < n - 1; i++)
for (int j = i + 1; j < n; j++)
if (bt[j] < bt[i]) {
swap(bt[i], bt[j]);
swap(p[i], p[j]);
}

wt[0] = 0;
for (int i = 1; i < n; i++)
wt[i] = wt[i - 1] + bt[i - 1];

cout << "\nProcess\tBT\tWT\tTAT\n";


for (int i = 0; i < n; i++) {
tat[i] = bt[i] + wt[i];
total_wt += wt[i];
total_tat += tat[i];
cout << "p" << p[i] << "\t" << bt[i] << "\t" << wt[i] << "\t" << tat[i] <<
'\n';
}

cout << "\nAverage Waiting Time = " << (float)total_wt / n;


cout << "\nAverage Turnaround Time = " << (float)total_tat / n << '\n';

return 0;
}

You might also like