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