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