-
Notifications
You must be signed in to change notification settings - Fork 0
/
task3.cpp
216 lines (188 loc) · 4.38 KB
/
task3.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include<iostream>
#include<fstream>
#include<string>
#include<queue>
using namespace std;
template <class T>
void showQueue(queue <T> gq)
{
queue <T> g = gq;
while (!g.empty())
{
cout << g.front()<<"\n";
g.pop();
}
cout << '\n';
}
void emptyFile()
{
ofstream file("readylist.txt", ios::trunc);
}
void sortInDescOrder(int count_of_processes,queue<string>&queueList,queue<int>&exeTimeList )
{
int* arrNum=new int[count_of_processes];
string* arrString=new string[count_of_processes];
for (int i = 0; i < count_of_processes; i++)
{
arrNum[i]=exeTimeList.front();
exeTimeList.pop();
arrString[i]=queueList.front();
queueList.pop();
}
for (int i = 0; i < count_of_processes-1; i++)
{
for(int j=0;j< count_of_processes-i-1 ;j++)
{
if(arrNum[j]<arrNum[j+1])
{
int tempNum=arrNum[j];
arrNum[j]=arrNum[j+1];
arrNum[j+1]=tempNum;
string tempString=arrString[j];
arrString[j]=arrString[j+1];
arrString[j+1]=tempString;
}
}
}
for (int i = 0; i < count_of_processes; i++)
{
exeTimeList.push(arrNum[i]);
queueList.push(arrString[i]);
}
delete []arrNum;delete []arrString;
}
int readFile(queue<string>&queueList, queue<int>&exeTimeList ,int &sum)
{
int count_of_processes=0;
string str;
int var=0;
ifstream infile;
infile.open("readylist.txt");
if(!infile)
cout<<"File Creation Failed ";
else
{
while(!infile.eof())
{
getline(infile,str);
count_of_processes++;
queueList.push(str);
ofstream outf("sample.txt");
outf<<str;
outf.close();
ifstream inf("sample.txt");
if(inf.is_open())
{
while(!inf.eof())
{
inf.ignore(30,',');
inf>>var;
sum=sum+var;
exeTimeList.push(var);
}
}
inf.close();
}
}
infile.close();
return count_of_processes;
}
int main()
{
queue<string> queueList;
queue<int> exeTimeList;
int sum=0;
int count_of_processes=0;
//=======READ FROM FILE AND MAKE IT EMPTY
count_of_processes=readFile(queueList,exeTimeList,sum);
emptyFile();
//++++++++++++SHOW INITIALLY LISTS
//showQueue(queueList); showQueue(exeTimeList);
//========SORT IN DESENDING ORDER
sortInDescOrder(count_of_processes,queueList,exeTimeList);
showQueue(queueList);
//=======DEFINE OUR UNIT (1 unit=10 iterations)
int unit=0 , arrivalTime=0 ,turnaroundTime=0 ,waitingTime=0 ,count=0;
int executionTime=exeTimeList.front();
int executionStartTime=0 ,executionEndTime=exeTimeList.front();
int total=sum*10;int var1=0;
int avgWaitTime=0 ,avgTurnAroundTime=0;
cout<<"ExecutionTime"<<"\t"<<"WaitingTime"<<"\t"<<"TurnaroundTime"<<"\n";
for(int i=0; i<=(total) ;i++,var1++)
{
count++;
if(count%10==0)
unit++;
if(var1==(executionTime*10))
{
waitingTime=executionStartTime-arrivalTime;
avgWaitTime=avgWaitTime+waitingTime;
turnaroundTime=executionEndTime-arrivalTime;
avgTurnAroundTime=avgTurnAroundTime+turnaroundTime;
cout<<executionTime<<"\t\t"<<waitingTime<<"\t\t"<<turnaroundTime<<"\n";
exeTimeList.pop();
queueList.pop();
var1=0;
executionTime=0;
executionStartTime=turnaroundTime;
if(!exeTimeList.empty())
{
executionTime=exeTimeList.front();
}
executionEndTime=executionStartTime+executionTime;
}
if(unit==15)
{
cout<<"\nPresses <ENTER> to continue\n";
getchar();
unit=0;
string str15; int var15=0;
ifstream infile;
infile.open("readylist.txt");
if(infile.is_open())
{
infile.seekg (0, ios::end);
int length = infile.tellg();
infile.close();
if (length == 0)
{
continue;
}
else
{
ifstream inputfile;
inputfile.open("readylist.txt");
while(!inputfile.eof())
{
getline(inputfile,str15);
count_of_processes++;
queueList.push(str15);
ofstream outf("sample.txt");
outf<<str15;
outf.close();
ifstream inf("sample.txt");
if(inf.is_open())
{
while(!inf.eof())
{
inf.ignore(30,',');
inf>>var15;
sum=sum+var15;
exeTimeList.push(var15);
}
}
inf.close();
}
total=sum*10;
emptyFile();
sortInDescOrder(count_of_processes,queueList,exeTimeList);
executionTime=exeTimeList.front();
inputfile.close();
}
}
}
}
cout<<"\nAverage Waiting Time = "<<(avgWaitTime/count_of_processes)<<endl;
cout<<"\nAverage Turn Around Time = "<<(avgTurnAroundTime/count_of_processes)<<endl;;
return 0;
}