Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
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.
 
vector< tuple< S, T, E, double, double, double > > scheduleForFcfs ()
 Algorithm for scheduling CPU processes according to the First Come First Serve(FCFS) scheduling algorithm.
 
void printResult ()
 Utility function for printing the status of each process after execution.
 

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
SData type of Process ID
TData type of Arrival time
EData 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
idProcess ID
arrivalArrival time of the process
burstBurst time of the process
Returns
void
129 {
130 // Add if a process with process ID as id is not found in idList.
131 if (idList.find(id) == idList.end()) {
133 make_tuple(id, arrival, burst, 0, 0, 0);
134 schedule.push(t);
135 idList.insert(id);
136 }
137 }
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
T end(T... args)
T find(T... args)
T insert(T... args)
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
191 {
192 cout << "Status of all the proceses post completion is as follows:"
193 << endl;
194
195 cout << std::setw(17) << left << "Process ID" << std::setw(17) << left
196 << "Arrival Time" << std::setw(17) << left << "Burst Time"
197 << std::setw(17) << left << "Completion Time" << std::setw(17)
198 << left << "Turnaround Time" << std::setw(17) << left
199 << "Waiting Time" << endl;
200
201 for (size_t i{}; i < result.size(); i++) {
202 cout << std::setprecision(2) << std::fixed << std::setw(17) << left
203 << get<0>(result[i]) << std::setw(17) << left
204 << get<1>(result[i]) << std::setw(17) << left
205 << get<2>(result[i]) << std::setw(17) << left
206 << get<3>(result[i]) << std::setw(17) << left
207 << get<4>(result[i]) << std::setw(17) << left
208 << get<5>(result[i]) << endl;
209 }
210 }
T fixed(T... args)
#define endl
Definition matrix_exponentiation.cpp:36
T setprecision(T... args)
T setw(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
154 {
155 // Variable to keep track of time elapsed so far
156 double timeElapsed = 0;
157
158 while (!schedule.empty()) {
160
161 // If the current process arrived at time t2, the last process
162 // completed its execution at time t1, and t2 > t1.
163 if (get<1>(cur) > timeElapsed) {
164 timeElapsed += get<1>(cur) - timeElapsed;
165 }
166
167 // Add Burst time to time elapsed
168 timeElapsed += get<2>(cur);
169
170 // Completion time of the current process will be same as time
171 // elapsed so far
172 get<3>(cur) = timeElapsed;
173
174 // Turnaround time = Completion time - Arrival time
175 get<4>(cur) = get<3>(cur) - get<1>(cur);
176
177 // Waiting time = Turnaround time - Burst time
178 get<5>(cur) = get<4>(cur) - get<2>(cur);
179
180 result.push_back(cur);
181 schedule.pop();
182 }
183 return result;
184 }

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: