From 29308eaffa05ddabe304e17b17dc4a89ccf0a648 Mon Sep 17 00:00:00 2001 From: Pratyush219 Date: Tue, 12 Oct 2021 11:42:01 +0530 Subject: [PATCH] Updated the documentation and used unsigned int for non-negative integers --- cpu_scheduling_algorithms/fcfs_scheduling.cpp | 95 ++++++++++++------- 1 file changed, 60 insertions(+), 35 deletions(-) diff --git a/cpu_scheduling_algorithms/fcfs_scheduling.cpp b/cpu_scheduling_algorithms/fcfs_scheduling.cpp index bba34c0dd..a1ea52635 100644 --- a/cpu_scheduling_algorithms/fcfs_scheduling.cpp +++ b/cpu_scheduling_algorithms/fcfs_scheduling.cpp @@ -1,10 +1,18 @@ -#include -#include -#include -#include -#include -#include -#include +/** + * @file fcfs_scheduling.cpp + * @brief Implementation of FCFS CPU scheduling algorithm + * @details + * FCFS is a non-preemptive CPU scheduling algorithm in which whichever process arrives first, gets executed first. + * If two or more processes arrive simultaneously, the process with smaller process ID gets executed first. + * @link https://bit.ly/3ABNXOC + * @author Pratyush Vatsa(https://github.com/Pratyush219) +*/ + +#include // for IO operations +#include // for using vector +#include // for using unordered_set +#include // for priority_queue +#include // for formatting the output using std::cin; using std::cout; @@ -18,23 +26,28 @@ using std::endl; using std::left; /** - * @brief Comparator class for Priority queue - * S: Data type of Process id - * T: Data type of Arrival time - * E: Data type of Burst time + * @class Compare + * @brief Comparator class for priority queue + * @tparam S: Data type of Process ID + * @tparam T: Data type of Arrival time + * @tparam E: Data type of Burst time */ template class Compare{ public: /** - * @param t1 = first tuple - * @param t2 = second tuple - * @brief checks whether to swap two tuples or not + * @param t1: first tuple + * @param t2: second tuple + * @brief A comparator function that checks whether to swap the two tuples or not. + * @link Refer https://www.geeksforgeeks.org/comparator-class-in-c-with-examples/ for detailed description of comparator + * @returns true if the tuples should be swapped, false othewise */ bool operator () (tuple& t1, tuple& t2){ + // Compare arrival times if(get<1>(t2) < get<1>(t1)){ return true; } + // If arrival times are same, then compare Process IDs else if(get<1>(t2) == get<1>(t1)){ return get<0>(t2) < get<0>(t1); } @@ -43,17 +56,18 @@ class Compare{ }; /** + * @class FCFS * @brief Class which implements the FCFS scheduling algorithm - * S: Data type of Process id - * T: Data type of Arrival time - * E: Data type of Burst time + * @tparam S: Data type of Process ID + * @tparam T: Data type of Arrival time + * @tparam E: Data type of Burst time */ template class FCFS{ /** * Priority queue of schedules(stored as tuples) of processes. * In each tuple - * 1st element: Process id + * 1st element: Process ID * 2nd element: Arrival Time * 3rd element: Burst time * 4th element: Completion time @@ -62,21 +76,22 @@ class FCFS{ */ priority_queue, vector>, Compare> schedule; - // Stores final status of all the processes after completing execution. + // Stores final status of all the processes after completing the execution. vector> result; - // Stores process ids. Used for confirming absence of a process while adding it. + // Stores process IDs. Used for confirming absence of a process while adding it. unordered_set idList; public: /** - * @brief add the process to the ready queue if it isn't already there - * @param id: Process id + * @brief adds the process to the ready queue if it isn't already there + * @param ID: Process ID * @param arrival: Arrival time of the process * @param burst: Burst time of the process + * @returns void * */ void addProcess(S id, T arrival, E burst){ - // Add if process with process id not found in idList. + // Add if a process with process ID as id is not found in idList. if(idList.find(id) == idList.end()) { tuple t = make_tuple(id, arrival, burst, 0, 0, 0); schedule.push(t); @@ -88,20 +103,22 @@ class FCFS{ /** * @brief Algorithm for scheduling CPU processes according to the First Come First Serve(FCFS) scheduling algorithm. * - * @description FCFS is a non -preemptive algorithm in which the process which arrives first gets executed first. If two or - * more processes arrive together then the process with smaller process id runs first (each process has a unique proces id). + * @description FCFS is a non-preemptive algorithm in which the process which arrives first gets executed first. If two or + * more processes arrive together then the process with smaller process ID runs first (each process has a unique proces ID). * * I used a min priority queue of tuples to accomplish this task. The processes are ordered by their arrival times. If arrival - * times of some processes are equal, then they are ordered by their process id. + * times of some processes are equal, then they are ordered by their process ID. + * + * @returns void */ void scheduleForFcfs(){ - // Variable to keep track of time elepsed so far + // Variable to keep track of time elapsed so far double timeElapsed = 0; while(!schedule.empty()){ tuple cur = schedule.top(); - // If the next process arrived at time t2, the last process completed its execution at time t1, and t2 > t1. + // If the current process arrived at time t2, the last process completed its execution at time t1, and t2 > t1. if(get<1>(cur) > timeElapsed){ timeElapsed += get<1>(cur) - timeElapsed; } @@ -109,12 +126,13 @@ class FCFS{ // Add Burst time to time elapsed timeElapsed += get<2>(cur); + // Completion time of the current process will be same as time elapsed so far get<3>(cur) = timeElapsed; // Turnaround time = Completion time - Arrival time get<4>(cur) = get<3>(cur) - get<1>(cur); - //Waiting time = Turnaround time - Burst time + // Waiting time = Turnaround time - Burst time get<5>(cur) = get<4>(cur) - get<2>(cur); result.push_back(cur); @@ -123,11 +141,14 @@ class FCFS{ printResult(); } - // Utility function for printing the status of each process after execution + /** + * @brief Utility function for printing the status of each process after execution + * @returns void + */ void printResult(){ cout << "Status of all the proceses post completion is as follows:" << endl; - cout << std::setw(17) << left << "Process Id" + cout << std::setw(17) << left << "Process ID" << std::setw(17) << left << "Arrival Time" << std::setw(17) << left << "Burst Time" << std::setw(17) << left << "Completion Time" @@ -148,21 +169,25 @@ class FCFS{ }; +/** + * @brief Entry point of the program + * @returns 0 on exit +*/ int main(){ - FCFS readyQueue; + FCFS readyQueue; //Sample test case int n = 3; - vector> input = { + vector> input = { make_tuple(1, 0, 30), make_tuple(2, 0, 5), make_tuple(3, 0, 5) }; - for(int i{}; i < n; i++){ + for(unsigned int i{}; i < n; i++){ readyQueue.addProcess(get<0>(input[i]), get<1>(input[i]), get<2>(input[i])); } readyQueue.scheduleForFcfs(); return 0; -} \ No newline at end of file +}