diff --git a/annotated.html b/annotated.html index 224eeeee5..02b6c8334 100644 --- a/annotated.html +++ b/annotated.html @@ -296,16 +296,17 @@ $(function(){initNavTree('annotated.html',''); initResizable(true); });  Cqueue  CRBtree  CSegmentIntersection - CSolution - CstackFor std::invalid_argument - Cstack_linkedList - CTestCaseSingle example inputs and expected output of the function longest_common_string_length - CTestCasesClass encapsulating the necessary test cases - Ctower - CTrie - CTrieNode - Cuint128_tClass for 128-bit unsigned integer - Cuint256_tClass for 256-bit unsigned integer + CSJFClass which implements the SJF scheduling algorithm + CSolution + CstackFor std::invalid_argument + Cstack_linkedList + CTestCaseSingle example inputs and expected output of the function longest_common_string_length + CTestCasesClass encapsulating the necessary test cases + Ctower + CTrie + CTrieNode + Cuint128_tClass for 128-bit unsigned integer + Cuint256_tClass for 256-bit unsigned integer diff --git a/annotated_dup.js b/annotated_dup.js index b1f9f695c..39cef68bd 100644 --- a/annotated_dup.js +++ b/annotated_dup.js @@ -221,6 +221,7 @@ var annotated_dup = [ "queue", "db/da9/classqueue.html", "db/da9/classqueue" ], [ "RBtree", "d8/d72/class_r_btree.html", null ], [ "SegmentIntersection", "d4/db4/struct_segment_intersection.html", "d4/db4/struct_segment_intersection" ], + [ "SJF", "d7/d9f/class_s_j_f.html", "d7/d9f/class_s_j_f" ], [ "Solution", "dd/d4f/class_solution.html", null ], [ "stack", "d1/dc2/classstack.html", "d1/dc2/classstack" ], [ "stack_linkedList", "d2/dc4/classstack__linked_list.html", null ], diff --git a/classes.html b/classes.html index 9bbbc3651..302b4ec0d 100644 --- a/classes.html +++ b/classes.html @@ -176,7 +176,7 @@ $(function(){initNavTree('classes.html',''); initResizable(true); });
RBtree
RootedTree (graph)
S
-
SegmentIntersection
SegmentTree (data_structures)
SG (range_queries::heavy_light_decomposition)
SkipList (data_structures)
Solution
Sparse_table (data_structures::sparse_table)
Stack (data_structures)
Stack (data_structures::stack_using_queue)
Stack (others::postfix_expression)
stack
stack_linkedList
stats_computer1 (statistics)
stats_computer2 (statistics)
+
SegmentIntersection
SegmentTree (data_structures)
SG (range_queries::heavy_light_decomposition)
SJF
SkipList (data_structures)
Solution
Sparse_table (data_structures::sparse_table)
Stack (data_structures)
Stack (data_structures::stack_using_queue)
Stack (others::postfix_expression)
stack
stack_linkedList
stats_computer1 (statistics)
stats_computer2 (statistics)
T
TestCase
TestCases
Tnode (operations_on_datastructures::trie_operations)
tower
Treap (data_structures::treap)
Tree (range_queries::heavy_light_decomposition)
Tree234 (data_structures::tree_234)
Trie (data_structures::trie_using_hashmap)
Trie
trie (data_structures)
Trie::TrieNode
diff --git a/d2/dba/non__preemptive__sjf__scheduling_8cpp__incl.map b/d2/dba/non__preemptive__sjf__scheduling_8cpp__incl.map new file mode 100644 index 000000000..085975543 --- /dev/null +++ b/d2/dba/non__preemptive__sjf__scheduling_8cpp__incl.map @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + + + diff --git a/d2/dba/non__preemptive__sjf__scheduling_8cpp__incl.md5 b/d2/dba/non__preemptive__sjf__scheduling_8cpp__incl.md5 new file mode 100644 index 000000000..309da587f --- /dev/null +++ b/d2/dba/non__preemptive__sjf__scheduling_8cpp__incl.md5 @@ -0,0 +1 @@ +ba4f96624d26113bb997e93dedb40676 \ No newline at end of file diff --git a/d2/dba/non__preemptive__sjf__scheduling_8cpp__incl.svg b/d2/dba/non__preemptive__sjf__scheduling_8cpp__incl.svg new file mode 100644 index 000000000..f03d01274 --- /dev/null +++ b/d2/dba/non__preemptive__sjf__scheduling_8cpp__incl.svg @@ -0,0 +1,249 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +cpu_scheduling_algorithms/non_preemptive_sjf_scheduling.cpp + + +Node1 + + +cpu_scheduling_algorithms +/non_preemptive_sjf_scheduling.cpp + + + + + +Node2 + + +algorithm + + + + + +Node1->Node2 + + + + + + + + +Node3 + + +cassert + + + + + +Node1->Node3 + + + + + + + + +Node4 + + +iomanip + + + + + +Node1->Node4 + + + + + + + + +Node5 + + +iostream + + + + + +Node1->Node5 + + + + + + + + +Node6 + + +queue + + + + + +Node1->Node6 + + + + + + + + +Node7 + + +random + + + + + +Node1->Node7 + + + + + + + + +Node8 + + +unordered_set + + + + + +Node1->Node8 + + + + + + + + +Node9 + + +vector + + + + + +Node1->Node9 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/d2/dba/non__preemptive__sjf__scheduling_8cpp__incl_org.svg b/d2/dba/non__preemptive__sjf__scheduling_8cpp__incl_org.svg new file mode 100644 index 000000000..9bdb1a658 --- /dev/null +++ b/d2/dba/non__preemptive__sjf__scheduling_8cpp__incl_org.svg @@ -0,0 +1,166 @@ + + + + + + +cpu_scheduling_algorithms/non_preemptive_sjf_scheduling.cpp + + +Node1 + + +cpu_scheduling_algorithms +/non_preemptive_sjf_scheduling.cpp + + + + + +Node2 + + +algorithm + + + + + +Node1->Node2 + + + + + + + + +Node3 + + +cassert + + + + + +Node1->Node3 + + + + + + + + +Node4 + + +iomanip + + + + + +Node1->Node4 + + + + + + + + +Node5 + + +iostream + + + + + +Node1->Node5 + + + + + + + + +Node6 + + +queue + + + + + +Node1->Node6 + + + + + + + + +Node7 + + +random + + + + + +Node1->Node7 + + + + + + + + +Node8 + + +unordered_set + + + + + +Node1->Node8 + + + + + + + + +Node9 + + +vector + + + + + +Node1->Node9 + + + + + + + + diff --git a/d7/d1a/non__preemptive__sjf__scheduling_8cpp.html b/d7/d1a/non__preemptive__sjf__scheduling_8cpp.html new file mode 100644 index 000000000..589c0c135 --- /dev/null +++ b/d7/d1a/non__preemptive__sjf__scheduling_8cpp.html @@ -0,0 +1,419 @@ + + + + + + + + +TheAlgorithms/C++: cpu_scheduling_algorithms/non_preemptive_sjf_scheduling.cpp File Reference + + + + + + + + + + + + + + + + + + + + + +
+
+ + + + + + + +
+
TheAlgorithms/C++ 1.0.0 +
+
All the algorithms implemented in C++
+
+
+ + + + + + + + +
+
+ +
+
+
+ +
+ +
+
+ + +
+
+
+
+
+
Loading...
+
Searching...
+
No Matches
+
+
+
+
+ +
+ +
non_preemptive_sjf_scheduling.cpp File Reference
+
+
+ +

Implementation of SJF CPU scheduling algorithm. +More...

+
#include <algorithm>
+#include <cassert>
+#include <iomanip>
+#include <iostream>
+#include <queue>
+#include <random>
+#include <unordered_set>
+#include <vector>
+
+Include dependency graph for non_preemptive_sjf_scheduling.cpp:
+
+
+
+
+

Go to the source code of this file.

+ + + + + + + + +

+Classes

class  Compare< S, T, E >
 Comparator class for priority queue. More...
 
class  SJF< S, T, E >
 Class which implements the SJF scheduling algorithm. More...
 
+ + + + + + + + + + + + + + + +

+Functions

template<typename S , typename T , typename E >
bool sortcol (tuple< S, T, E > &t1, tuple< S, T, E > &t2)
 Comparator function for sorting a vector.
 
template<typename S , typename T , typename E >
vector< tuple< S, T, E, double, double, double > > get_final_status (vector< tuple< S, T, E > > input)
 Computes the final status of processes after applying non-preemptive SJF scheduling.
 
static void test ()
 Self-test implementations.
 
int main ()
 Main function.
 
+

Detailed Description

+

Implementation of SJF CPU scheduling algorithm.

+

shortest job first (SJF), also known as shortest job next (SJN), is a scheduling policy that selects for execution the waiting process with the smallest execution time. SJN is a non-preemptive algorithm. Shortest remaining time is a preemptive variant of SJN. detailed description on SJF scheduling Author : Lakshmi Srikumar

+ +

Definition in file non_preemptive_sjf_scheduling.cpp.

+

Function Documentation

+ +

◆ get_final_status()

+ +
+
+
+template<typename S , typename T , typename E >
+ + + + + + + +
vector< tuple< S, T, E, double, double, double > > get_final_status (vector< tuple< S, T, E > > input)
+
+ +

Computes the final status of processes after applying non-preemptive SJF scheduling.

+
Template Parameters
+ + + + +
SData type of Process ID
TData type of Arrival time
EData type of Burst time
+
+
+
Parameters
+ + +
inputA vector of tuples containing Process ID, Arrival time, and Burst time
+
+
+
Returns
A vector of tuples containing Process ID, Arrival time, Burst time, Completion time, Turnaround time, and Waiting time
+ +

Definition at line 228 of file non_preemptive_sjf_scheduling.cpp.

+
229 {
+
230 // Sort the processes based on Arrival time and then Burst time
+
231 sort(input.begin(), input.end(), sortcol<S, T, E>);
+
232
+
233 // Result vector to hold the final status of each process
+
234 vector<tuple<S, T, E, double, double, double>> result(input.size());
+
235 double timeElapsed = 0;
+
236
+
237 for (size_t i = 0; i < input.size(); i++) {
+
238 // Extract Arrival time and Burst time
+
239 T arrival = get<1>(input[i]);
+
240 E burst = get<2>(input[i]);
+
241
+
242 // If the CPU is idle, move time to the arrival of the next process
+
243 if (arrival > timeElapsed) {
+
244 timeElapsed = arrival;
+
245 }
+
246
+
247 // Update timeElapsed by adding the burst time
+
248 timeElapsed += burst;
+
249
+
250 // Calculate Completion time, Turnaround time, and Waiting time
+
251 double completion = timeElapsed;
+
252 double turnaround = completion - arrival;
+
253 double waiting = turnaround - burst;
+
254
+
255 // Store the results in the result vector
+
256 result[i] = make_tuple(get<0>(input[i]), arrival, burst, completion,
+
257 turnaround, waiting);
+
258 }
+
259
+
260 return result;
+
261}
+
uint64_t result(uint64_t n)
+
bool sortcol(tuple< S, T, E > &t1, tuple< S, T, E > &t2)
Comparator function for sorting a vector.
+
+
+
+ +

◆ main()

+ +
+
+ + + + + + + +
int main (void )
+
+ +

Main function.

+
Returns
0 on successful exit
+ +

Definition at line 313 of file non_preemptive_sjf_scheduling.cpp.

+
313 {
+
314 test();
+
315 return 0;
+
316}
+
static void test()
Self-test implementations.
+
+
+
+ +

◆ sortcol()

+ +
+
+
+template<typename S , typename T , typename E >
+ + + + + + + + + + + +
bool sortcol (tuple< S, T, E > & t1,
tuple< S, T, E > & t2 )
+
+ +

Comparator function for sorting a vector.

+
Template Parameters
+ + + + +
SData type of Process ID
TData type of Arrival time
EData type of Burst time
+
+
+
Parameters
+ + + +
t1First tuple<S,T,E>t1
t2Second tuple<S,T,E>t2
+
+
+
Returns
true if t1 and t2 are in the CORRECT order
+
+false if t1 and t2 are in the INCORRECT order
+ +

Definition at line 45 of file non_preemptive_sjf_scheduling.cpp.

+
45 {
+
46 if (get<1>(t1) < get<1>(t2) ||
+
47 (get<1>(t1) == get<1>(t2) && get<0>(t1) < get<0>(t2))) {
+
48 return true;
+
49 }
+
50 return false;
+
51}
+
+
+
+ +

◆ test()

+ +
+
+ + + + + +
+ + + + + + + +
static void test ()
+
+static
+
+ +

Self-test implementations.

+
Returns
void
+ +

Definition at line 267 of file non_preemptive_sjf_scheduling.cpp.

+
267 {
+
268 // A vector to store the results of all processes across all test cases.
+
269 vector<tuple<uint32_t, uint32_t, uint32_t, double, double, double>>
+
270 finalResult;
+
271
+
272 for (int i{}; i < 10; i++) {
+
273 std::random_device rd; // Seeding
+
274 std::mt19937 eng(rd());
+
275 std::uniform_int_distribution<> distr(1, 10);
+
276
+
277 uint32_t n = distr(eng);
+ +
279 vector<tuple<uint32_t, uint32_t, uint32_t, double, double, double>>
+
280 input(n);
+
281
+
282 // Generate random arrival and burst times
+
283 for (uint32_t i{}; i < n; i++) {
+
284 get<0>(input[i]) = i;
+
285 get<1>(input[i]) = distr(eng); // Random arrival time
+
286 get<2>(input[i]) = distr(eng); // Random burst time
+
287 }
+
288
+
289 // Print processes before scheduling
+
290 cout << "Processes before SJF scheduling:" << endl;
+
291 readyQueue.printResult(input);
+
292
+
293 // Add processes to the queue
+
294 for (uint32_t i{}; i < n; i++) {
+
295 readyQueue.addProcess(get<0>(input[i]), get<1>(input[i]),
+
296 get<2>(input[i]));
+
297 }
+
298
+
299 // Perform SJF schedulings
+
300 auto finalResult = readyQueue.scheduleForSJF();
+
301
+
302 // Print processes after scheduling
+
303 cout << "\nProcesses after SJF scheduling:" << endl;
+
304 readyQueue.printResult(finalResult);
+
305 }
+
306 cout << "All the tests have successfully passed!" << endl;
+
307}
+
Class which implements the 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.
+
vector< tuple< S, T, E, double, double, double > > scheduleForSJF()
Algorithm for scheduling CPU processes according to the Shortest Job First (SJF) scheduling algorithm...
+
void addProcess(S id, T arrival, E burst)
Adds the process to the ready queue if it isn't already there.
+
#define endl
+
+
+
+
+
+ + + + diff --git a/d7/d1a/non__preemptive__sjf__scheduling_8cpp.js b/d7/d1a/non__preemptive__sjf__scheduling_8cpp.js new file mode 100644 index 000000000..728635715 --- /dev/null +++ b/d7/d1a/non__preemptive__sjf__scheduling_8cpp.js @@ -0,0 +1,9 @@ +var non__preemptive__sjf__scheduling_8cpp = +[ + [ "Compare< S, T, E >", "de/d4a/class_compare.html", "de/d4a/class_compare" ], + [ "SJF< S, T, E >", "d7/d9f/class_s_j_f.html", "d7/d9f/class_s_j_f" ], + [ "get_final_status", "d7/d1a/non__preemptive__sjf__scheduling_8cpp.html#a23667f16acc4035988e7cd7ae80b6ff3", null ], + [ "main", "d7/d1a/non__preemptive__sjf__scheduling_8cpp.html#ae66f6b31b5ad750f1fe042a706a4e3d4", null ], + [ "sortcol", "d7/d1a/non__preemptive__sjf__scheduling_8cpp.html#a18920aa331faf4476b251c8cdb2c2bec", null ], + [ "test", "d7/d1a/non__preemptive__sjf__scheduling_8cpp.html#aa8dca7b867074164d5f45b0f3851269d", null ] +]; \ No newline at end of file diff --git a/d7/d1a/non__preemptive__sjf__scheduling_8cpp_source.html b/d7/d1a/non__preemptive__sjf__scheduling_8cpp_source.html new file mode 100644 index 000000000..3d0b7efc1 --- /dev/null +++ b/d7/d1a/non__preemptive__sjf__scheduling_8cpp_source.html @@ -0,0 +1,376 @@ + + + + + + + + +TheAlgorithms/C++: cpu_scheduling_algorithms/non_preemptive_sjf_scheduling.cpp Source File + + + + + + + + + + + + + + + + + + + + + +
+
+ + + + + + + +
+
TheAlgorithms/C++ 1.0.0 +
+
All the algorithms implemented in C++
+
+
+ + + + + + + + +
+
+ +
+
+
+ +
+ +
+
+ + +
+
+
+
+
+
Loading...
+
Searching...
+
No Matches
+
+
+
+
+ +
+
non_preemptive_sjf_scheduling.cpp
+
+
+Go to the documentation of this file.
1
+
14#include <algorithm>
+
15#include <cassert>
+
16#include <iomanip>
+
17#include <iostream>
+
18#include <queue>
+
19#include <random>
+
20#include <unordered_set>
+
21#include <vector>
+
22
+
23using std::cin;
+
24using std::cout;
+
25using std::endl;
+
26using std::get;
+
27using std::left;
+
28using std::make_tuple;
+
29using std::priority_queue;
+
30using std::tuple;
+
31using std::unordered_set;
+
32using std::vector;
+
33
+
44template <typename S, typename T, typename E>
+
+
45bool sortcol(tuple<S, T, E>& t1, tuple<S, T, E>& t2) {
+
46 if (get<1>(t1) < get<1>(t2) ||
+
47 (get<1>(t1) == get<1>(t2) && get<0>(t1) < get<0>(t2))) {
+
48 return true;
+
49 }
+
50 return false;
+
51}
+
+
52
+
60template <typename S, typename T, typename E>
+
61class Compare {
+
62 public:
+
+
74 bool operator()(tuple<S, T, E, double, double, double>& t1,
+
75 tuple<S, T, E, double, double, double>& t2) {
+
76 // Compare burst times for SJF
+
77 if (get<2>(t2) < get<2>(t1)) {
+
78 return true;
+
79 }
+
80 // If burst times are the same, compare arrival times
+
81 else if (get<2>(t2) == get<2>(t1)) {
+
82 return get<1>(t2) < get<1>(t1);
+
83 }
+
84 return false;
+
85 }
+
+
86};
+
87
+
95template <typename S, typename T, typename E>
+
+
96class SJF {
+
107 priority_queue<tuple<S, T, E, double, double, double>,
+
108 vector<tuple<S, T, E, double, double, double>>,
+ + +
111
+
112 // Stores final status of all the processes after completing the execution.
+
113 vector<tuple<S, T, E, double, double, double>> result;
+
114
+
115 // Stores process IDs. Used for confirming absence of a process while it.
+
116 unordered_set<S> idList;
+
117
+
118 public:
+
+
127 void addProcess(S id, T arrival, E burst) {
+
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 }
+
+
136
+
+
154 vector<tuple<S, T, E, double, double, double>> scheduleForSJF() {
+
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 }
+
+
+ +
198 const vector<tuple<S, T, E, double, double, double>>& processes) {
+
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 }
+
+
214};
+
+
215
+
227template <typename S, typename T, typename E>
+
+
228vector<tuple<S, T, E, double, double, double>> get_final_status(
+
229 vector<tuple<S, T, E>> input) {
+
230 // Sort the processes based on Arrival time and then Burst time
+
231 sort(input.begin(), input.end(), sortcol<S, T, E>);
+
232
+
233 // Result vector to hold the final status of each process
+
234 vector<tuple<S, T, E, double, double, double>> result(input.size());
+
235 double timeElapsed = 0;
+
236
+
237 for (size_t i = 0; i < input.size(); i++) {
+
238 // Extract Arrival time and Burst time
+
239 T arrival = get<1>(input[i]);
+
240 E burst = get<2>(input[i]);
+
241
+
242 // If the CPU is idle, move time to the arrival of the next process
+
243 if (arrival > timeElapsed) {
+
244 timeElapsed = arrival;
+
245 }
+
246
+
247 // Update timeElapsed by adding the burst time
+
248 timeElapsed += burst;
+
249
+
250 // Calculate Completion time, Turnaround time, and Waiting time
+
251 double completion = timeElapsed;
+
252 double turnaround = completion - arrival;
+
253 double waiting = turnaround - burst;
+
254
+
255 // Store the results in the result vector
+
256 result[i] = make_tuple(get<0>(input[i]), arrival, burst, completion,
+
257 turnaround, waiting);
+
258 }
+
259
+
260 return result;
+
261}
+
+
262
+
+
267static void test() {
+
268 // A vector to store the results of all processes across all test cases.
+
269 vector<tuple<uint32_t, uint32_t, uint32_t, double, double, double>>
+
270 finalResult;
+
271
+
272 for (int i{}; i < 10; i++) {
+
273 std::random_device rd; // Seeding
+
274 std::mt19937 eng(rd());
+
275 std::uniform_int_distribution<> distr(1, 10);
+
276
+
277 uint32_t n = distr(eng);
+ +
279 vector<tuple<uint32_t, uint32_t, uint32_t, double, double, double>>
+
280 input(n);
+
281
+
282 // Generate random arrival and burst times
+
283 for (uint32_t i{}; i < n; i++) {
+
284 get<0>(input[i]) = i;
+
285 get<1>(input[i]) = distr(eng); // Random arrival time
+
286 get<2>(input[i]) = distr(eng); // Random burst time
+
287 }
+
288
+
289 // Print processes before scheduling
+
290 cout << "Processes before SJF scheduling:" << endl;
+
291 readyQueue.printResult(input);
+
292
+
293 // Add processes to the queue
+
294 for (uint32_t i{}; i < n; i++) {
+
295 readyQueue.addProcess(get<0>(input[i]), get<1>(input[i]),
+
296 get<2>(input[i]));
+
297 }
+
298
+
299 // Perform SJF schedulings
+
300 auto finalResult = readyQueue.scheduleForSJF();
+
301
+
302 // Print processes after scheduling
+
303 cout << "\nProcesses after SJF scheduling:" << endl;
+
304 readyQueue.printResult(finalResult);
+
305 }
+
306 cout << "All the tests have successfully passed!" << endl;
+
307}
+
+
308
+
+
313int main() {
+
314 test();
+
315 return 0;
+
316}
+
+
Comparator class for priority queue.
+
bool operator()(tuple< S, T, E, double, double, double > &t1, tuple< S, T, E, double, double, double > &t2)
A comparator function that checks whether to swap the two tuples or not. detailed description of comp...
+
Class which implements the 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.
+
vector< tuple< S, T, E, double, double, double > > scheduleForSJF()
Algorithm for scheduling CPU processes according to the Shortest Job First (SJF) scheduling algorithm...
+
priority_queue< tuple< S, T, E, double, double, double >, vector< tuple< S, T, E, double, double, double > >, Compare< S, T, E > > schedule
+
void addProcess(S id, T arrival, E burst)
Adds the process to the ready queue if it isn't already there.
+
#define endl
+
bool sortcol(tuple< S, T, E > &t1, tuple< S, T, E > &t2)
Comparator function for sorting a vector.
+
vector< tuple< S, T, E, double, double, double > > get_final_status(vector< tuple< S, T, E > > input)
Computes the final status of processes after applying non-preemptive SJF scheduling.
+
static void test()
Self-test implementations.
+
int main()
Main function.
+
+
+ + + + diff --git a/d7/d9f/class_s_j_f.html b/d7/d9f/class_s_j_f.html new file mode 100644 index 000000000..de4b139ae --- /dev/null +++ b/d7/d9f/class_s_j_f.html @@ -0,0 +1,452 @@ + + + + + + + + +TheAlgorithms/C++: SJF< S, T, E > Class Template Reference + + + + + + + + + + + + + + + + + + + + + +
+
+ + + + + + + +
+
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: +
+
+ + + + diff --git a/d7/d9f/class_s_j_f.js b/d7/d9f/class_s_j_f.js new file mode 100644 index 000000000..cc6ed478e --- /dev/null +++ b/d7/d9f/class_s_j_f.js @@ -0,0 +1,7 @@ +var class_s_j_f = +[ + [ "addProcess", "d7/d9f/class_s_j_f.html#af73fe815835075184724a13c12f613ed", null ], + [ "printResult", "d7/d9f/class_s_j_f.html#a42d7016523e3adfe5b946fab18ce7905", null ], + [ "scheduleForSJF", "d7/d9f/class_s_j_f.html#a61b76afcc94e573d7232ab51c351fd30", null ], + [ "schedule", "d7/d9f/class_s_j_f.html#ac86d31ea081e671adeb82b3f92376fea", null ] +]; \ No newline at end of file diff --git a/d8/dc8/class_compare-members.html b/d8/dc8/class_compare-members.html index 34728ea05..c9d5dd79f 100644 --- a/d8/dc8/class_compare-members.html +++ b/d8/dc8/class_compare-members.html @@ -128,6 +128,7 @@ $(function(){initNavTree('de/d4a/class_compare.html','../../'); initResizable(tr

This is the complete list of members for Compare< S, T, E >, including all inherited members.

+
operator()(tuple< S, T, E, double, double, double > &t1, tuple< S, T, E, double, double, double > &t2)Compare< S, T, E >inline
operator()(tuple< S, T, E, double, double, double > &t1, tuple< S, T, E, double, double, double > &t2)Compare< S, T, E >inline
diff --git a/d9/de5/class_s_j_f-members.html b/d9/de5/class_s_j_f-members.html new file mode 100644 index 000000000..b11a98a86 --- /dev/null +++ b/d9/de5/class_s_j_f-members.html @@ -0,0 +1,145 @@ + + + + + + + + +TheAlgorithms/C++: Member List + + + + + + + + + + + + + + + + + + + + + +
+
+ + + + + + + +
+
TheAlgorithms/C++ 1.0.0 +
+
All the algorithms implemented in C++
+
+
+ + + + + + + + +
+
+ +
+
+
+ +
+ +
+
+ + +
+
+
+
+
+
Loading...
+
Searching...
+
No Matches
+
+
+
+
+ +
+
SJF< S, T, E > Member List
+
+
+ +

This is the complete list of members for SJF< S, T, E >, including all inherited members.

+ + + + + + + +
addProcess(S id, T arrival, E burst)SJF< S, T, E >inline
idList (defined in SJF< S, T, E >)SJF< S, T, E >private
printResult(const vector< tuple< S, T, E, double, double, double > > &processes)SJF< S, T, E >inline
result (defined in SJF< S, T, E >)SJF< S, T, E >private
scheduleSJF< S, T, E >private
scheduleForSJF()SJF< S, T, E >inline
+
+ + + + diff --git a/de/d4a/class_compare.html b/de/d4a/class_compare.html index 3c08d7f10..5b432d3b5 100644 --- a/de/d4a/class_compare.html +++ b/de/d4a/class_compare.html @@ -136,6 +136,9 @@ Public Member Functions bool operator() (tuple< S, T, E, double, double, double > &t1, tuple< S, T, E, double, double, double > &t2)  A comparator function that checks whether to swap the two tuples or not. to https://www.geeksforgeeks.org/comparator-class-in-c-with-examples/ for detailed description of comparator.
  +bool operator() (tuple< S, T, E, double, double, double > &t1, tuple< S, T, E, double, double, double > &t2) + A comparator function that checks whether to swap the two tuples or not. detailed description of comparator

Detailed Description

template<typename S, typename T, typename E>
@@ -152,7 +155,7 @@ class Compare< S, T, E >

Comparator class for priority queue.

Definition at line 63 of file fcfs_scheduling.cpp.

Member Function Documentation

-

◆ operator()()

+

◆ operator()() [1/2]

@@ -207,8 +210,65 @@ false if the tuples SHOULDN'T be swapped
-
The documentation for this class was generated from the following file: