forked from vedant781999/Array_operation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1- FCFS.cpp
76 lines (59 loc) · 1.81 KB
/
1- FCFS.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include<iostream>
#include<algorithm>
#include<iomanip>
using namespace std;
struct process{
int pid,AT,BT,ST,CT,TAT,WT,RT;
};
bool compareArrival(process p1, process p2)
{
return p1.AT<p2.AT;
}
bool compareID(process p1, process p2)
{
return p1.pid<p2.pid;
}
int main() {
int n;
struct process p[100];
float avg_TAT;
float avg_WT;
float avg_RT;
int total_TAT = 0;
int total_WT = 0;
int total_RT = 0;
cout << setprecision(2) << fixed;
cout<<" Please Enter the number of processes: ";
cin>>n;
for(int i = 0; i < n; i++) {
cout<<"Enter the arrival time of process "<<i+1<<": ";
cin>>p[i].AT;
cout<<"Enter the burst time of process "<<i+1<<": ";
cin>>p[i].BT;
p[i].pid = i+1;
cout<<endl;
}
sort(p,p+n,compareArrival);
for(int i = 0; i < n; i++) {
p[i].ST = (i == 0)?p[i].AT:max(p[i-1].CT,p[i].AT);
p[i].CT = p[i].ST + p[i].BT;
p[i].TAT = p[i].CT - p[i].AT;
p[i].WT= p[i].TAT - p[i].BT;
p[i].RT = p[i].ST - p[i].AT;
total_TAT += p[i].TAT;
total_WT += p[i].WT;
total_RT += p[i].RT;
}
avg_TAT = (float) total_TAT / n;
avg_WT = (float) total_WT / n;
avg_RT = (float) total_RT / n;
sort(p,p+n,compareID);
cout<<endl;
cout<<"Pid\t"<<"AT\t"<<"BT\t"<<"ST\t"<<"CT\t"<<"TAT\t"<<"WT\t"<<"RT\t"<<"\n"<<endl;
for(int i = 0; i < n; i++) {
cout<<p[i].pid<<"\t"<<p[i].AT<<"\t"<<p[i].BT<<"\t"<<p[i].ST<<"\t"<<p[i].CT<<"\t"<<p[i].TAT<<"\t"<<p[i].WT<<"\t"<<p[i].RT<<"\t"<<"\n"<<endl;
}
cout<<"Average Turnaround Time = "<<avg_TAT<<endl;
cout<<"Average Waiting Time = "<<avg_WT<<endl;
cout<<"Average Response Time = "<<avg_RT<<endl;
}