20#include <unordered_set>
29using std::priority_queue;
33using std::unordered_set;
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)) {
49 }
else if (get<1>(t1) == get<1>(t2) && get<0>(t1) < get<0>(t2)) {
62template <
typename S,
typename T,
typename E>
76 bool operator()(tuple<S, T, E, double, double, double>& t1,
77 tuple<S, T, E, double, double, double>& t2) {
79 if (get<1>(t2) < get<1>(t1)) {
83 else if (get<1>(t2) == get<1>(t1)) {
84 return get<0>(t2) < get<0>(t1);
97template <
typename S,
typename T,
typename E>
109 priority_queue<tuple<S, T, E, double, double, double>,
110 vector<tuple<S, T, E, double, double, double>>,
115 vector<tuple<S, T, E, double, double, double>> result;
119 unordered_set<S> 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);
157 double timeElapsed = 0;
160 tuple<S, T, E, double, double, double> cur =
schedule.top();
164 if (get<1>(cur) > timeElapsed) {
165 timeElapsed += get<1>(cur) - timeElapsed;
169 timeElapsed += get<2>(cur);
173 get<3>(cur) = timeElapsed;
176 get<4>(cur) = get<3>(cur) - get<1>(cur);
179 get<5>(cur) = get<4>(cur) - get<2>(cur);
181 result.push_back(cur);
193 cout <<
"Status of all the proceses post completion is as follows:"
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;
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;
225template <
typename S,
typename T,
typename E>
227 vector<tuple<uint32_t, uint32_t, uint32_t>> input) {
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]);
235 if (arrival > timeElapsed) {
236 timeElapsed += arrival - timeElapsed;
238 timeElapsed += burst;
239 double completion = timeElapsed;
240 double turnaround = completion - arrival;
241 double waiting = turnaround - burst;
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;
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);
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;
272 for (uint32_t i{}; i < n; i++) {
273 readyQueue.
addProcess(get<0>(input[i]), get<1>(input[i]),
276 vector<tuple<uint32_t, uint32_t, uint32_t, double, double, double>>
281 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. 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.