TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
SJF< S, T, E > Class Template Reference

Class which implements the SJF scheduling algorithm. More...

Public Member Functions

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.
 

Private Attributes

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
 

Detailed Description

template<typename S, typename T, typename E>
class SJF< S, T, E >

Class which implements the SJF scheduling algorithm.

Template Parameters
SData type of Process ID
TData type of Arrival time
EData type of Burst time

Definition at line 96 of file non_preemptive_sjf_scheduling.cpp.

Member Function Documentation

◆ 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
idProcess ID
arrivalArrival time of the process
burstBurst time of the process
Returns
void

Definition at line 127 of file non_preemptive_sjf_scheduling.cpp.

127 {
128 // Add if a process with process ID as id is not found in idList.
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);
132 schedule.push(t);
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)
211 << endl;
212 }
213 }
#define endl

◆ 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 // Variable to keep track of time elapsed so far
156 double timeElapsed = 0;
157
158 while (!schedule.empty()) {
159 tuple<S, T, E, double, double, double> cur = schedule.top();
160
161 // If the current process arrived at time t2, the last process
162 // completed its execution at time t1, and t2 > t1.
163 if (get<1>(cur) > timeElapsed) {
164 timeElapsed += get<1>(cur) - timeElapsed;
165 }
166
167 // Add Burst time to time elapsed
168 timeElapsed += get<2>(cur);
169
170 // Completion time of the current process will be same as time
171 // elapsed so far
172 get<3>(cur) = timeElapsed;
173
174 // Turnaround time = Completion time - Arrival time
175 get<4>(cur) = get<3>(cur) - get<1>(cur);
176
177 // Waiting time = Turnaround time - Burst time
178 get<5>(cur) = get<4>(cur) - get<2>(cur);
179
180 // Turnaround time >= Burst time
181 assert(get<4>(cur) >= get<2>(cur));
182
183 // Waiting time is never negative
184 assert(get<5>(cur) >= 0);
185
186 result.push_back(cur);
187 schedule.pop();
188 }
189 return result;
190 }

Member Data Documentation

◆ idList

template<typename S , typename T , typename E >
unordered_set<S> SJF< S, T, E >::idList
private

Definition at line 116 of file non_preemptive_sjf_scheduling.cpp.

◆ result

template<typename S , typename T , typename E >
vector<tuple<S, T, E, double, double, double> > SJF< S, T, E >::result
private

Definition at line 113 of file non_preemptive_sjf_scheduling.cpp.

◆ 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
1stelement: Process ID
2ndelement: Arrival Time
3rdelement: Burst time
4thelement: Completion time
5thelement: Turnaround time
6thelement: Waiting time

Definition at line 110 of file non_preemptive_sjf_scheduling.cpp.


The documentation for this class was generated from the following file: