forked from mishrasunny174/OSPracticals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Practical7.cpp
189 lines (172 loc) · 4.67 KB
/
Practical7.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
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#ifdef __linux__
#define CLRSCR "clear"
#else
#define CLRSCR "cls"
#endif
using namespace std;
class RoundRobinScheduler;
class process
{
int processId;
int arrivalTime;
int burstTime;
int turnAroundTime;
int waitingTime;
int runTime;
bool isPresentInReadyQueue;
friend class RoundRobinScheduler;
public:
process();
bool operator<(const process &);
void show();
};
class RoundRobinScheduler
{
queue<process> jobQueue;
queue<process> readyQueue;
vector<process> tasks;
const int timeSlice;
const int totalProcesses;
int totalBurstTime;
void schedule();
void contextSwitch();
int calculateAverageWaitingTime();
int calculateAverageTurnAroundTime();
public:
RoundRobinScheduler(int timeSlice, int totalProcesses);
void simulate();
};
int main()
{
system(CLRSCR);
int numOfProcesses, timeSlice;
cout << "Enter number of processes to simulate: ";
cin >> numOfProcesses;
cout << "Enter time slice: ";
cin >> timeSlice;
RoundRobinScheduler sched(timeSlice, numOfProcesses);
sched.simulate();
cout << "Press enter to continue...";
cin.ignore();
cin.get();
return 0;
}
process::process()
{
cout << "Enter process id: ";
cin >> processId;
cout << "Enter arrival time: ";
cin >> arrivalTime;
cout << "Enter burst time: ";
cin >> burstTime;
waitingTime = 0;
runTime = 0;
turnAroundTime = 0;
isPresentInReadyQueue = false;
}
bool process::operator<(const process &p)
{
return this->arrivalTime < p.arrivalTime;
}
void process::show()
{
cout << "pid: " << processId
<< "\t burst time: " << burstTime
<< "\t waitingTime: " << waitingTime
<< "\t turn around time: " << turnAroundTime
<< endl;
}
RoundRobinScheduler::RoundRobinScheduler(int timeSlice, int totalProcesses) : timeSlice(timeSlice), totalProcesses(totalProcesses)
{
for (int i = 0; i < totalProcesses; i++)
{
cout << "Enter details of process " << i + 1 << endl;
process p;
tasks.push_back(p);
}
totalBurstTime = 0;
}
void RoundRobinScheduler::schedule()
{
sort(tasks.begin(), tasks.end());
}
void RoundRobinScheduler::simulate()
{
system(CLRSCR);
schedule();
for (auto iter = tasks.begin(); iter != tasks.end(); iter++)
{
totalBurstTime += iter->burstTime;
jobQueue.push(*iter);
}
if (jobQueue.front().arrivalTime != 0)
totalBurstTime += jobQueue.front().arrivalTime;
for (int i = 0; i < totalBurstTime; i++)
{
// loading processes in ready queue
while (!jobQueue.empty() && jobQueue.front().arrivalTime == i)
{
readyQueue.push(jobQueue.front());
for(auto iter = tasks.begin();iter<=tasks.end();iter++)
{
if (iter->processId == jobQueue.front().processId)
iter->isPresentInReadyQueue=true;
}
jobQueue.pop();
}
if (i % timeSlice == 0 || readyQueue.front().runTime == readyQueue.front().burstTime)
{
if (readyQueue.front().runTime == readyQueue.front().burstTime)
{
readyQueue.pop();
}
else
contextSwitch();
}
readyQueue.front().runTime++;
// cout<<"running "<<readyQueue.front().processId<<endl;
for (auto iter = tasks.begin(); iter != tasks.end(); iter++)
{
if (iter->runTime == iter->burstTime)
continue;
if (iter->processId != readyQueue.front().processId)
{
if(iter->isPresentInReadyQueue)
iter->waitingTime++;
}
else
iter->runTime++;
}
}
for (auto iter = tasks.begin(); iter != tasks.end(); iter++)
{
iter->turnAroundTime = iter->waitingTime + iter->burstTime;
iter->show();
}
cout << endl
<< "average waiting time: " << calculateAverageWaitingTime() << endl
<< "average turn around time: " << calculateAverageTurnAroundTime() << endl;
}
void RoundRobinScheduler::contextSwitch()
{
readyQueue.push(readyQueue.front());
readyQueue.pop();
}
int RoundRobinScheduler::calculateAverageWaitingTime()
{
int sum=0;
for (auto iter = tasks.begin(); iter != tasks.end(); iter++)
sum+=iter->waitingTime;
return sum/totalProcesses;
}
int RoundRobinScheduler::calculateAverageTurnAroundTime()
{
int sum=0;
for (auto iter = tasks.begin(); iter != tasks.end(); iter++)
sum+=iter->turnAroundTime;
return sum/totalProcesses;
}