forked from mishrasunny174/OSPracticals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Practical10.cpp
197 lines (180 loc) · 4.92 KB
/
Practical10.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
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#ifdef __linux__
#define CLRSCR "clear"
#else
#define CLRSCR "cls"
#endif
using namespace std;
class PreemptivePriorityScheduler;
class process
{
int processId;
int arrivalTime;
int burstTime;
int turnAroundTime;
int waitingTime;
int runTime;
int priority;
bool isPresentInReadyQueue;
friend class PreemptivePriorityScheduler;
public:
process();
bool operator<(const process &);
void show();
};
class PreemptivePriorityScheduler
{
queue<process> jobQueue;
queue<process> readyQueue;
vector<process> tasks;
const int totalProcesses;
int totalBurstTime;
void sortJobs();
void schedule();
int calculateAverageWaitingTime();
int calculateAverageTurnAroundTime();
public:
PreemptivePriorityScheduler(int totalProcesses);
void simulate();
};
int main()
{
system(CLRSCR);
int numOfProcesses;
cout << "Enter number of processes to simulate: ";
cin >> numOfProcesses;
PreemptivePriorityScheduler sched(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;
cout << "Enter priority: ";
cin >> priority;
waitingTime = 0;
runTime = 0;
turnAroundTime = 0;
isPresentInReadyQueue = false;
}
bool process::operator<(const process &p)
{
if(this->arrivalTime==p.arrivalTime)
return this->priority<p.priority;
return this->arrivalTime < p.arrivalTime;
}
void process::show()
{
cout << "pid: " << processId
<< "\t\t burst time: " << burstTime
<< "\t\t waiting time: " << waitingTime
<< "\t\t turn around time: " << turnAroundTime
<< endl;
}
PreemptivePriorityScheduler::PreemptivePriorityScheduler(int totalProcesses) : 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 PreemptivePriorityScheduler::sortJobs()
{
sort(tasks.begin(), tasks.end());
}
void PreemptivePriorityScheduler::simulate()
{
system(CLRSCR);
sortJobs();
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();
schedule();
}
if (readyQueue.front().runTime == readyQueue.front().burstTime)
{
readyQueue.pop();
schedule();
}
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;
}
int PreemptivePriorityScheduler::calculateAverageWaitingTime()
{
int sum = 0;
for (auto iter = tasks.begin(); iter != tasks.end(); iter++)
sum += iter->waitingTime;
return sum / totalProcesses;
}
int PreemptivePriorityScheduler::calculateAverageTurnAroundTime()
{
int sum = 0;
for (auto iter = tasks.begin(); iter != tasks.end(); iter++)
sum += iter->turnAroundTime;
return sum / totalProcesses;
}
void PreemptivePriorityScheduler::schedule()
{
vector<process> temp;
while (!readyQueue.empty())
{
temp.push_back(readyQueue.front());
readyQueue.pop();
}
sort(temp.begin(), temp.end(), [](process p1, process p2) {
return p1.priority < p2.priority;
});
for (auto iter = temp.begin(); iter != temp.end(); iter++)
readyQueue.push(*iter);
}