-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ4.cpp
More file actions
117 lines (116 loc) · 1.87 KB
/
Copy pathQ4.cpp
File metadata and controls
117 lines (116 loc) · 1.87 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <iostream>
using namespace std;
class Process
{
public:
string name;
int at,bt,tat,wt,rt,prio;
bool flag;
void input();
void display();
};
void Process::input()
{
flag=true;
cout<<"\nEnter the process name, burst time and arrival time(integers)\n";
cin>>name>>bt>>at;
}
void Process::display()
{
cout<<endl<<name<<"\t"<<tat<<"\t"<<wt<<"\t"<<rt<<"\n";
}
bool notEnded(Process* p,int n)
{
for(int i=0;i<n;i++)
{
if(p[i].bt!=0)
return true;
}
return false;
}
int main()
{
int flag;
Process p[20];
int n=0;
do
{
p[n++].input();
cout<<"\nDo you want to add another process?(0 to stop)";
cin>>flag;
}while(flag);
int ts;
cout<<"\nEnter the time slice:";
cin>>ts;
Process p1;
for(int i=0;i<n;i++)
{
for(int j=0;j<n-i-1;j++)
{
if(p[j].at>p[j+1].at)
{
p1=p[j];
p[j]=p[j+1];
p[j+1]=p1;
}
}
}
int current=0;
double tat=0,rt=0,wt=0;
for(int i=0;notEnded(p,n);i++)
{
if(p[i].bt==0)
{
continue;
}
if(i>=n)//if list has ended, go to 0
{
i=0;
continue;
}
if(p[i].at>current&¬Ended(p,i))//if cpu has to wait for another process start another round
{
i=0;
continue;
}
if(p[i].at>current&&!notEnded(p,i))//if cpu has to idle while waiting for a process
{
cout<<"\ni="<<i<<endl;
current=p[i].at;
i--;
continue;
}
if(p[i].flag)//if its the first time cpu is given
{
p[i].wt=-p[i].bt;
p[i].rt=current-p[i].at;
p[i].flag=false;
}
if(ts<p[i].bt)
{
current+=ts;
p[i].bt-=ts;
}
else
{
current+=p[i].bt;
p[i].bt=0;
}
if(p[i].bt==0)
{
cout<<"\nCurrent ="<<current;
cout<<"\nP"<<(i+1)<<endl;
p[i].tat=current-p[i].at;
p[i].wt+=p[i].tat;
tat+=p[i].tat;
rt+=p[i].rt;
wt+=p[i].wt;
}
}
cout<<"\nName\tTAT\tWT\tRT\n";
for(int i=0;i<n;i++)
{
p[i].display();
}
cout<<"\nAverage Times:-\nTAT ="<<tat/n<<"\nWT ="<<wt/n<<"\nRT ="<<rt/n<<endl;
}