20#include <unordered_set>
29using std::priority_queue;
31using std::unordered_set;
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))) {
60template <
typename S,
typename T,
typename E>
74 bool operator()(tuple<S, T, E, double, double, double>& t1,
75 tuple<S, T, E, double, double, double>& t2) {
77 if (get<2>(t2) < get<2>(t1)) {
81 else if (get<2>(t2) == get<2>(t1)) {
82 return get<1>(t2) < get<1>(t1);
95template <
typename S,
typename T,
typename E>
107 priority_queue<tuple<S, T, E, double, double, double>,
108 vector<tuple<S, T, E, double, double, double>>,
113 vector<tuple<S, T, E, double, double, double>> result;
116 unordered_set<S> 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);
156 double timeElapsed = 0;
159 tuple<S, T, E, double, double, double> cur =
schedule.top();
163 if (get<1>(cur) > timeElapsed) {
164 timeElapsed += get<1>(cur) - timeElapsed;
168 timeElapsed += get<2>(cur);
172 get<3>(cur) = timeElapsed;
175 get<4>(cur) = get<3>(cur) - get<1>(cur);
178 get<5>(cur) = get<4>(cur) - get<2>(cur);
181 assert(get<4>(cur) >= get<2>(cur));
184 assert(get<5>(cur) >= 0);
186 result.push_back(cur);
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;
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)
227template <
typename S,
typename T,
typename E>
229 vector<tuple<S, T, E>> input) {
234 vector<tuple<S, T, E, double, double, double>> result(input.size());
235 double timeElapsed = 0;
237 for (
size_t i = 0; i < input.size(); i++) {
239 T arrival = get<1>(input[i]);
240 E burst = get<2>(input[i]);
243 if (arrival > timeElapsed) {
244 timeElapsed = arrival;
248 timeElapsed += burst;
251 double completion = timeElapsed;
252 double turnaround = completion - arrival;
253 double waiting = turnaround - burst;
256 result[i] = make_tuple(get<0>(input[i]), arrival, burst, completion,
257 turnaround, waiting);
269 vector<tuple<uint32_t, uint32_t, uint32_t, double, double, double>>
272 for (
int i{}; i < 10; i++) {
273 std::random_device rd;
274 std::mt19937 eng(rd());
275 std::uniform_int_distribution<> distr(1, 10);
277 uint32_t n = distr(eng);
279 vector<tuple<uint32_t, uint32_t, uint32_t, double, double, double>>
283 for (uint32_t i{}; i < n; i++) {
284 get<0>(input[i]) = i;
285 get<1>(input[i]) = distr(eng);
286 get<2>(input[i]) = distr(eng);
290 cout <<
"Processes before SJF scheduling:" <<
endl;
294 for (uint32_t i{}; i < n; i++) {
295 readyQueue.
addProcess(get<0>(input[i]), get<1>(input[i]),
303 cout <<
"\nProcesses after SJF scheduling:" <<
endl;
306 cout <<
"All the tests have successfully passed!" <<
endl;
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.
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.