TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
fcfs_scheduling.cpp
Go to the documentation of this file.
1
12#include <algorithm>
13#include <cassert>
14#include <cstdint>
15#include <cstdlib>
16#include <ctime>
17#include <iomanip>
18#include <iostream>
19#include <queue>
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::rand;
31using std::srand;
32using std::tuple;
33using std::unordered_set;
34using std::vector;
45template <typename S, typename T, typename E>
46bool sortcol(tuple<S, T, E>& t1, tuple<S, T, E>& t2) {
47 if (get<1>(t1) < get<1>(t2)) {
48 return true;
49 } else if (get<1>(t1) == get<1>(t2) && get<0>(t1) < get<0>(t2)) {
50 return true;
51 }
52 return false;
53}
54
62template <typename S, typename T, typename E>
63class Compare {
64 public:
76 bool operator()(tuple<S, T, E, double, double, double>& t1,
77 tuple<S, T, E, double, double, double>& t2) {
78 // Compare arrival times
79 if (get<1>(t2) < get<1>(t1)) {
80 return true;
81 }
82 // If arrival times are same, then compare Process IDs
83 else if (get<1>(t2) == get<1>(t1)) {
84 return get<0>(t2) < get<0>(t1);
85 }
86 return false;
87 }
88};
89
97template <typename S, typename T, typename E>
98class FCFS {
109 priority_queue<tuple<S, T, E, double, double, double>,
110 vector<tuple<S, T, E, double, double, double>>,
113
114 // Stores final status of all the processes after completing the execution.
115 vector<tuple<S, T, E, double, double, double>> result;
116
117 // Stores process IDs. Used for confirming absence of a process while adding
118 // it.
119 unordered_set<S> idList;
120
121 public:
130 void addProcess(S id, T arrival, E burst) {
131 // Add if a process with process ID as id is not found in idList.
132 if (idList.find(id) == idList.end()) {
133 tuple<S, T, E, double, double, double> t =
134 make_tuple(id, arrival, burst, 0, 0, 0);
135 schedule.push(t);
136 idList.insert(id);
137 }
138 }
139
155 vector<tuple<S, T, E, double, double, double>> scheduleForFcfs() {
156 // Variable to keep track of time elapsed so far
157 double timeElapsed = 0;
158
159 while (!schedule.empty()) {
160 tuple<S, T, E, double, double, double> cur = schedule.top();
161
162 // If the current process arrived at time t2, the last process
163 // completed its execution at time t1, and t2 > t1.
164 if (get<1>(cur) > timeElapsed) {
165 timeElapsed += get<1>(cur) - timeElapsed;
166 }
167
168 // Add Burst time to time elapsed
169 timeElapsed += get<2>(cur);
170
171 // Completion time of the current process will be same as time
172 // elapsed so far
173 get<3>(cur) = timeElapsed;
174
175 // Turnaround time = Completion time - Arrival time
176 get<4>(cur) = get<3>(cur) - get<1>(cur);
177
178 // Waiting time = Turnaround time - Burst time
179 get<5>(cur) = get<4>(cur) - get<2>(cur);
180
181 result.push_back(cur);
182 schedule.pop();
183 }
184 return result;
185 }
186
192 void printResult() {
193 cout << "Status of all the proceses post completion is as follows:"
194 << endl;
195
196 cout << std::setw(17) << left << "Process ID" << std::setw(17) << left
197 << "Arrival Time" << std::setw(17) << left << "Burst Time"
198 << std::setw(17) << left << "Completion Time" << std::setw(17)
199 << left << "Turnaround Time" << std::setw(17) << left
200 << "Waiting Time" << endl;
201
202 for (size_t i{}; i < result.size(); i++) {
203 cout << std::setprecision(2) << std::fixed << std::setw(17) << left
204 << get<0>(result[i]) << std::setw(17) << left
205 << get<1>(result[i]) << std::setw(17) << left
206 << get<2>(result[i]) << std::setw(17) << left
207 << get<3>(result[i]) << std::setw(17) << left
208 << get<4>(result[i]) << std::setw(17) << left
209 << get<5>(result[i]) << endl;
210 }
211 }
212};
213
225template <typename S, typename T, typename E>
226vector<tuple<S, T, E, double, double, double>> get_final_status(
227 vector<tuple<uint32_t, uint32_t, uint32_t>> input) {
228 sort(input.begin(), input.end(), sortcol<S, T, E>);
229 vector<tuple<S, T, E, double, double, double>> result(input.size());
230 double timeElapsed = 0;
231 for (size_t i{}; i < input.size(); i++) {
232 T arrival = get<1>(input[i]);
233 E burst = get<2>(input[i]);
234
235 if (arrival > timeElapsed) {
236 timeElapsed += arrival - timeElapsed;
237 }
238 timeElapsed += burst;
239 double completion = timeElapsed;
240 double turnaround = completion - arrival;
241 double waiting = turnaround - burst;
242
243 get<0>(result[i]) = get<0>(input[i]);
244 get<1>(result[i]) = arrival;
245 get<2>(result[i]) = burst;
246 get<3>(result[i]) = completion;
247 get<4>(result[i]) = turnaround;
248 get<5>(result[i]) = waiting;
249 }
250 return result;
251}
252
257static void test() {
258 for (int i{}; i < 1000; i++) {
259 srand(time(nullptr));
260 uint32_t n = 1 + rand() % 1000;
262 vector<tuple<uint32_t, uint32_t, uint32_t>> input(n);
263
264 for (uint32_t i{}; i < n; i++) {
265 get<0>(input[i]) = i;
266 srand(time(nullptr));
267 get<1>(input[i]) = 1 + rand() % 10000;
268 srand(time(nullptr));
269 get<2>(input[i]) = 1 + rand() % 10000;
270 }
271
272 for (uint32_t i{}; i < n; i++) {
273 readyQueue.addProcess(get<0>(input[i]), get<1>(input[i]),
274 get<2>(input[i]));
275 }
276 vector<tuple<uint32_t, uint32_t, uint32_t, double, double, double>>
278 assert(res == readyQueue.scheduleForFcfs());
279 // readyQueue.printResult();
280 }
281 cout << "All the tests have successfully passed!" << endl;
282}
283
288int main() {
289 test(); // run self-test implementations
290 return 0;
291}
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. to https://www....
Class which implements the FCFS scheduling algorithm.
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 > > scheduleForFcfs()
Algorithm for scheduling CPU processes according to the First Come First Serve(FCFS) scheduling algor...
void printResult()
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
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< uint32_t, uint32_t, uint32_t > > input)
Function to be used for testing purposes. This function guarantees the correct solution for FCFS sche...
static void test()
Self-test implementations.
int main()
Entry point of the program.
#define endl