-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCPU Scheduler Simulator.cpp
110 lines (95 loc) · 2.58 KB
/
CPU Scheduler Simulator.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
#include<iostream>
#include<queue>
#include<string>
#include<fstream>
#include<iomanip>
using namespace std;
struct processInfo
{
string processName;
int executionTime;
int arrivalTime;
};
void oneTimeUnit()
{
for (int i = 0; i < 10000; i++);
}
int ArrivalTimeGlobal = 0;
int readFile(queue<processInfo>& dummy)
{
int added = 0;
ifstream input("readylist.txt");
if (!input)
{
cout << "File not Found\n ";
exit(1);
}
cout << "\n\nThe Contents of Ready Queue are: \n";
while (!input.eof())
{
string s;
int n;
processInfo temp;
input >> s;
input >> n;
if (n > 0 && n < 10000000) //To check, if it is not a garbage value then it should proceed
{
temp.processName = s;
temp.executionTime = n;
temp.arrivalTime = ArrivalTimeGlobal;
dummy.push(temp);
added++;
}
}
queue<processInfo> t=dummy;
for (int i = 0; i < dummy.size(); i++)
{
cout <<"-> "<<t.front().processName << " " <<t.front().executionTime << endl;
t.pop();
}
input.close();
ofstream output("readylist.txt");
output.close();
return added;
}
int main()
{
cout << "\t\t\t\t\t***WELCOME TO SIMULATED CPU SCHEDULER***\n";
queue<processInfo> readyQueue;
readFile(readyQueue);
cout << "\n(Now Executing Processes)\n";
int waitingTime=0;
int turnaroundTime = 0;
int noOfElements = readyQueue.size();
int sumofWaitingTime = 0;
int sumofTurnaroundTime = 0;
int noOfTimeUnitsExecuted=0;
for (int i = 0; i < noOfElements ; i++)
{
for (int j = 0; j < readyQueue.front().executionTime; j++)
{
oneTimeUnit();
noOfTimeUnitsExecuted++;
if ((noOfTimeUnitsExecuted % 20)==0)
{
ArrivalTimeGlobal+=20;
cout << endl;
system("pause");
noOfElements += readFile(readyQueue);
}
}
cout << "\n\nFollowing process has Finished Execution\n";
turnaroundTime += readyQueue.front().executionTime;
cout << readyQueue.front().processName << " " << readyQueue.front().executionTime;
cout<<" "<<waitingTime - readyQueue.front().arrivalTime<<" "<<turnaroundTime - readyQueue.front().arrivalTime;
sumofWaitingTime += waitingTime - readyQueue.front().arrivalTime;
sumofTurnaroundTime += turnaroundTime - readyQueue.front().arrivalTime;
waitingTime += readyQueue.front().executionTime;
readyQueue.pop();
}
cout << "\n\nAll Processes Ended\n";
cout << "\nAverage Waiting Time: " << (sumofWaitingTime / (double)noOfElements);
cout << "\nAverage Turnaround Time: " << (sumofTurnaroundTime / (double)noOfElements);
cout << endl;
return 0;
}