TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
FCFS< S, T, E > Class Template Reference

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

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

Definition at line 98 of file fcfs_scheduling.cpp.

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

Definition at line 130 of file fcfs_scheduling.cpp.

130 {
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 }
priority_queue< tuple< S, T, E, double, double, double >, vector< tuple< S, T, E, double, double, double > >, Compare< S, T, E > > schedule

◆ 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

Definition at line 192 of file fcfs_scheduling.cpp.

192 {
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 }
#define endl

◆ 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

Definition at line 155 of file fcfs_scheduling.cpp.

155 {
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 }

Member Data Documentation

◆ idList

template<typename S , typename T , typename E >
unordered_set<S> FCFS< S, T, E >::idList
private

Definition at line 119 of file fcfs_scheduling.cpp.

◆ result

template<typename S , typename T , typename E >
vector<tuple<S, T, E, double, double, double> > FCFS< S, T, E >::result
private

Definition at line 115 of file fcfs_scheduling.cpp.

◆ 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

Definition at line 112 of file fcfs_scheduling.cpp.


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