Class which implements the SJF scheduling algorithm.
More...
|
void | addProcess (S id, T arrival, E burst) |
| Adds the process to the ready queue if it isn't already there.
|
|
vector< tuple< S, T, E, double, double, double > > | scheduleForSJF () |
| Algorithm for scheduling CPU processes according to the Shortest Job First (SJF) scheduling algorithm.
|
|
void | printResult (const vector< tuple< S, T, E, double, double, double > > &processes) |
| Utility function for printing the status of each process after execution.
|
|
|
priority_queue< tuple< S, T, E, double, double, double >, vector< tuple< S, T, E, double, double, double > >, Compare< S, T, E > > | schedule |
|
vector< tuple< S, T, E, double, double, double > > | result |
|
unordered_set< S > | idList |
|
template<typename S, typename T, typename E>
class SJF< S, T, E >
Class which implements the SJF scheduling algorithm.
- Template Parameters
-
S | Data type of Process ID |
T | Data type of Arrival time |
E | Data type of Burst time |
Definition at line 96 of file non_preemptive_sjf_scheduling.cpp.
◆ addProcess()
template<typename S , typename T , typename E >
void SJF< S, T, E >::addProcess |
( |
S | id, |
|
|
T | arrival, |
|
|
E | burst ) |
|
inline |
Adds the process to the ready queue if it isn't already there.
- Parameters
-
id | Process ID |
arrival | Arrival time of the process |
burst | Burst time of the process |
- Returns
- void
Definition at line 127 of file non_preemptive_sjf_scheduling.cpp.
127 {
128
129 if (idList.find(id) == idList.end()) {
130 tuple<S, T, E, double, double, double> t =
131 make_tuple(id, arrival, burst, 0, 0, 0);
133 idList.insert(id);
134 }
135 }
priority_queue< tuple< S, T, E, double, double, double >, vector< tuple< S, T, E, double, double, double > >, Compare< S, T, E > > schedule
◆ printResult()
template<typename S , typename T , typename E >
void SJF< S, T, E >::printResult |
( |
const vector< tuple< S, T, E, double, double, double > > & | processes | ) |
|
|
inline |
Utility function for printing the status of each process after execution.
- Returns
- void
Definition at line 197 of file non_preemptive_sjf_scheduling.cpp.
198 {
199 cout << std::setw(17) << left << "Process ID" << std::setw(17) << left
200 << "Arrival Time" << std::setw(17) << left << "Burst Time"
201 << std::setw(17) << left << "Completion Time" << std::setw(17)
202 << left << "Turnaround Time" << std::setw(17) << left
203 <<
"Waiting Time" <<
endl;
204
205 for (const auto& process : processes) {
206 cout << std::setprecision(2) << std::fixed << std::setw(17) << left
207 << get<0>(process) << std::setw(17) << left << get<1>(process)
208 << std::setw(17) << left << get<2>(process) << std::setw(17)
209 << left << get<3>(process) << std::setw(17) << left
210 << get<4>(process) << std::setw(17) << left << get<5>(process)
212 }
213 }
◆ scheduleForSJF()
template<typename S , typename T , typename E >
vector< tuple< S, T, E, double, double, double > > SJF< S, T, E >::scheduleForSJF |
( |
| ) |
|
|
inline |
Algorithm for scheduling CPU processes according to the Shortest Job First (SJF) scheduling algorithm.
Non pre-emptive SJF is an algorithm that schedules processes based on the length of their burst times. The process with the smallest burst time is executed first.In a non-preemptive scheduling algorithm, once a process starts executing,it runs to completion without being interrupted.
I used a min priority queue because it allows you to efficiently pick the process with the smallest burst time in constant time, by maintaining a priority order where the shortest burst process is always at the front.
- Returns
- void
Definition at line 154 of file non_preemptive_sjf_scheduling.cpp.
154 {
155
156 double timeElapsed = 0;
157
159 tuple<S, T, E, double, double, double> cur =
schedule.top();
160
161
162
163 if (get<1>(cur) > timeElapsed) {
164 timeElapsed += get<1>(cur) - timeElapsed;
165 }
166
167
168 timeElapsed += get<2>(cur);
169
170
171
172 get<3>(cur) = timeElapsed;
173
174
175 get<4>(cur) = get<3>(cur) - get<1>(cur);
176
177
178 get<5>(cur) = get<4>(cur) - get<2>(cur);
179
180
181 assert(get<4>(cur) >= get<2>(cur));
182
183
184 assert(get<5>(cur) >= 0);
185
186 result.push_back(cur);
188 }
189 return result;
190 }
◆ idList
template<typename S , typename T , typename E >
unordered_set<S> SJF< S, T, E >::idList |
|
private |
◆ result
template<typename S , typename T , typename E >
vector<tuple<S, T, E, double, double, double> > SJF< S, T, E >::result |
|
private |
◆ schedule
template<typename S , typename T , typename E >
priority_queue<tuple<S, T, E, double, double, double>, vector<tuple<S, T, E, double, double, double> >, Compare<S, T, E> > SJF< S, T, E >::schedule |
|
private |
Priority queue of schedules(stored as tuples) of processes. In each tuple
- Template Parameters
-
1st | element: Process ID |
2nd | element: Arrival Time |
3rd | element: Burst time |
4th | element: Completion time |
5th | element: Turnaround time |
6th | element: Waiting time |
Definition at line 110 of file non_preemptive_sjf_scheduling.cpp.
The documentation for this class was generated from the following file: