# Algorithms_in_C++: FCFS< S, T, E > Class Template Reference

Loading...
Searching...
No Matches
FCFS< S, T, E > Class Template Reference

Class which implements the FCFS scheduling algorithm. More...

Collaboration diagram for FCFS< S, T, E >:
[legend]

## Public Member Functions

void | addProcess (S id, T arrival, E burst)
| Adds the process to the ready queue if it isn't already there. More...
vector< tuple< S, T, E, double, double, double > > | scheduleForFcfs ()
| Algorithm for scheduling CPU processes according to the First Come First Serve(FCFS) scheduling algorithm. More...
void | printResult ()
| Utility function for printing the status of each process after execution. More...

## 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 FCFS< S, T, E >

Class which implements the FCFS scheduling algorithm.

Template Parameters

  S | Data type of Process ID
  T | Data type of Arrival time
  E | Data type of Burst time

## Member Function Documentation

## addProcess()

template<typename S , typename T , typename E >

void FCFS< 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

  id | Process ID
  arrival | Arrival time of the process
  burst | Burst time of the process

Returns
  void

{
// Add if a process with process ID as id is not found in idList.
if (idList.find(id) == idList.end()) {
tuple<S, T, E, double, double, double> t =
make_tuple(id, arrival, burst, 0, 0, 0);
schedule.push(t);
idList.insert(id);
}
}
FCFS::schedule
priority_queue< tuple< S, T, E, double, double, double >, vector< tuple< S, T, E, double, double, double > >, Compare< S, T, E > > schedule
Definition: fcfs_scheduling.cpp:111
std::unordered_set::end
T end(T... args)
std::unordered_set::find
T find(T... args)
std::unordered_set::insert
T insert(T... args)
std::make_tuple
T make_tuple(T... args)
std::priority_queue::push
T push(T... args)
std::tuple
Here is the call graph for this function:

## printResult()

template<typename S , typename T , typename E >

void FCFS< S, T, E >::printResult | ( |  | ) |

inline

Utility function for printing the status of each process after execution.

Returns
  void

{
cout << "Status of all the proceses post completion is as follows:"
<< endl;
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" << std::setw(17)
<< left << "Turnaround Time" << std::setw(17) << left
<< "Waiting Time" << endl;
for (size_t i{}; i < result.size(); i++) {
cout << std::setprecision(2) << std::fixed << std::setw(17) << left
<< get<0>(result[i]) << std::setw(17) << left
<< get<1>(result[i]) << std::setw(17) << left
<< get<2>(result[i]) << std::setw(17) << left
<< get<3>(result[i]) << std::setw(17) << left
<< get<4>(result[i]) << std::setw(17) << left
<< get<5>(result[i]) << endl;
}
}
std::cout
std::fixed
T fixed(T... args)
std::left
T left(T... args)
endl
#define endl
Definition: matrix_exponentiation.cpp:36
std::setprecision
T setprecision(T... args)
std::setw
T setw(T... args)
std::vector::size
T size(T... args)
Here is the call graph for this function:

## scheduleForFcfs()

template<typename S , typename T , typename E >

vector< tuple< S, T, E, double, double, double > > FCFS< S, T, E >::scheduleForFcfs | ( |  | ) |

inline

Algorithm for scheduling CPU processes according to the First Come First Serve(FCFS) scheduling algorithm.

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.

Returns
  void

{
// Variable to keep track of time elapsed so far
double timeElapsed = 0;
while (!schedule.empty()) {
tuple<S, T, E, double, double, double> cur = schedule.top();
// 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;
}
// 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
get<5>(cur) = get<4>(cur) - get<2>(cur);
result.push_back(cur);
schedule.pop();
}
return result;
}
std::priority_queue::empty
T empty(T... args)
std::priority_queue::pop
T pop(T... args)
std::vector::push_back
T push_back(T... args)
std::priority_queue::top
T top(T... args)
Here is the call graph for this function:

## Member Data Documentation

## 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> > FCFS< S, T, E >::schedule

private

Priority queue of schedules(stored as tuples) of processes. In each tuple 1st element: Process ID 2nd element: Arrival Time 3rd element: Burst time 4th element: Completion time 5th element: Turnaround time 6th element: Waiting time

The documentation for this class was generated from the following file:

- cpu_scheduling_algorithms/fcfs_scheduling.cpp