#include <bits/stdc++.h>
using namespace std;
struct process {
int pid;
int burst_time;
int start_time;
int completion_time;
int turnaround_time;
int waiting_time;
};
bool compareBurst(process p1, process p2)
{
return p1.burst_time < p2.burst_time;
}
bool compareID(process p1, process p2)
{
return p1.pid < p2.pid;
}
int main() {
int n;
struct process p[100];
float avg_turnaround_time;
float avg_waiting_time;
int total_turnaround_time = 0;
int total_waiting_time = 0;
cout<<"Enter the number of processes: ";
cin>>n;
for(int i = 0; i < n; i++) {
cout<<"Enter burst time of process "<<i+1<<": ";
cin>>p[i].burst_time;
p[i].pid = i+1;
}
sort(p,p+n,compareBurst);
for(int i = 0; i < n; i++) {
p[i].start_time = (i == 0)? 0 :p[i-1].turnaround_time;
p[i].turnaround_time = p[i].start_time + p[i].burst_time;
p[i].waiting_time = p[i].turnaround_time - p[i].burst_time;
total_turnaround_time += p[i].turnaround_time;
total_waiting_time += p[i].waiting_time;
}
avg_turnaround_time = (float) total_turnaround_time / n;
avg_waiting_time = (float) total_waiting_time / n;
sort(p,p+n,compareID);
cout<<endl;
cout<<"#P\t"<<"BT\t"<<"ST\t"<<"TAT\t"<<"WT\t"<<"\n"<<endl;
for(int i = 0; i < n; i++) {
cout<<p[i].pid<<"\t"<<p[i].burst_time<<"\t"<<p[i].start_time<<"\t"<<p[i].turnaround_time<<"\t"<<p[i].waiting_time<<"\t\t"<<"\n"<<endl;
}
cout<<"Average Turnaround Time = "<<avg_turnaround_time<<endl;
cout<<"Average Waiting Time = "<<avg_waiting_time<<endl;
}
Replies to Untitled
Title |
Name |
Language |
UNIX |
When |
Re: Untitled |
Perl Agouti |
cpp |
1669387345 |
1 Year ago. |
{"html5":"htmlmixed","css":"css","javascript":"javascript","php":"php","python":"python","ruby":"ruby","lua":"text\/x-lua","bash":"text\/x-sh","go":"go","c":"text\/x-csrc","cpp":"text\/x-c++src","diff":"diff","latex":"stex","sql":"sql","xml":"xml","apl":"apl","asterisk":"asterisk","c_loadrunner":"text\/x-csrc","c_mac":"text\/x-csrc","coffeescript":"text\/x-coffeescript","csharp":"text\/x-csharp","d":"d","ecmascript":"javascript","erlang":"erlang","groovy":"text\/x-groovy","haskell":"text\/x-haskell","haxe":"text\/x-haxe","html4strict":"htmlmixed","java":"text\/x-java","java5":"text\/x-java","jquery":"javascript","mirc":"mirc","mysql":"sql","ocaml":"text\/x-ocaml","pascal":"text\/x-pascal","perl":"perl","perl6":"perl","plsql":"sql","properties":"text\/x-properties","q":"text\/x-q","scala":"scala","scheme":"text\/x-scheme","tcl":"text\/x-tcl","vb":"text\/x-vb","verilog":"text\/x-verilog","yaml":"text\/x-yaml","z80":"text\/x-z80"}