×

C++ Program For FCFS (First Come First Serve)

The most basic scheduling technique is FCFS, often known as "FIFO (First In, First Out)". In this procedure, the first one is utilized and executed first, while the second one begins only when the prior one has been finished and fully executed.

FCFS (First Come, First Serve) in the C++ programming language is a non-pre-emptive scheduling mechanism. The FIFO method prioritizes processes based on the sequence in which users request the processor. The CPU is allotted to the process that wants it first. This is simple to build, using a FIFO queue to manage the tasks.

As the process arrives, it is placed at the bottom of the queue. When a job is completed, the CPU deletes it from the queue and continues with the next task.

The First Come First Serve (FCFS) algorithm employs the following terms:

1. Completion Time (C.T.): The period of time taken until the process is finished.

2. Waiting Time (W.T.): The time difference between the burst time (B.T.) and the turnaround time (T.A.T.). Waiting Time (W.T.) = T.A.T (Time Around Time) – B.T (Burst Time)

3. Turn Around Time (T.A.T.): The time difference between the arrival time (A.T.) and the completion time (C.T.). Turn Around Time (T.A.T) = C.T (Completion Time) – A.T (Arrival Time)

We assumed arrival time = 0, thus completion time and turn-around time are the same.

Example

/* FCFS implementation code is written in the C++ programming language. */
#include<iostream>
using namespace std;
 /* Function for calculating the total waiting_time for all the processes */
void find_Waiting_Time(int process[], int n, int b_t[], int w_t[])
{
   	 // waiting time for first process is 0
    	w_t[0] = 0;
 	// calculating waiting time
    	for (int  x = 1; x < n ; x++ )
        	w_t[x] =  b_t[x-1] + w_t[x-1] ;
}
 
/* Calculating turn_around time using this function */
void find_TurnAround_Time( int process[], int n, int b_t[], int w_t[], int ta_t[])
{
    	/* adding b_t[x] + w_t[x] to calculate the turn_around time */
    	for (int x = 0; x < n ; x++)
        	ta_t[x] = b_t[x] + w_t[x];
}
 
/* Calculating avg_time time using this function */
void find_avg_Time( int process[], int n, int b_t[])
{
    	int w_t[n], ta_t[n], wt_total = 0, tat_total = 0;
 	/* Function for calculating the waiting_time of all the operations */
    	find_Waiting_Time(process, n, b_t, w_t);
 	/* Function for calculating turn_around time for all the processes available */   	find_TurnAround_Time(process, n, b_t, w_t, ta_t);
 	/* Display all the procedures with all the available details */
    	cout << "Processes  "<< " Burst time  "
         	<< " Waiting time  " << " Turn around time\n";
 	/* The total_turn_around time and the total_wait time should be calculated */
    	for (int  x=0; x<n; x++)
    	{
        		wt_total = wt_total + w_t[x];
        		tat_total = tat_total + ta_t[x];
        		cout << "   " << x+1 << "\t\t" << b_t[x] <<"\t    "
            	<< w_t[x] <<"\t\t  " << ta_t[x] <<endl;
    	}
 
    	cout << "Average waiting time = "
         	<< (float)wt_total / (float)n;
    	cout << "\nAverage turn around time = "
         	<< (float)tat_total / (float)n;
}
 
int main()
{
    	/*  identifiers for all the processes */
    	int process[] = { 1, 2, 3};
    	int n = sizeof process / sizeof process[0];
 	/* burst_time of all the available processes */
    	int  burst_time[] = {10, 5, 8};
 	find_avg_Time(process, n,  burst_time);
    	return 0;
}

Output:

C++ Program For (FCFS) FIRST COME FIRST SERVE

How is it Implemented?

  1. Enter the processes and their b_t (burst time).
  2. Determine the w_t (waiting time) for all the processes.
  3. Because the first process that arrives does not need to wait, w_t[0] = 0, i.e., the waiting time for process 1 is zero.
  4. Determine the waiting time for all the other processes, i.e., for process i:
w_t[i] = b_t[i-1] + w_t[i-1].
  1. Calculate turnaround time for all processes as (waiting_time) + (burst_time). 
  2. Find the average waiting time by (total_waiting_time) / (no_of_processes).
  3. Similarly, the average turn-around time is calculated as (total_turn_around_time) / (no_of_processes). 

Some Important points on First Come First Serve (FCFS):

  1. FCFS (First Come, First Serve) in the C++ language is a non-preemptive system.
  2. The average wait time for FCFS (First Come, First Serve) is insufficient.
  3. Resources cannot be used concurrently:

The results that in the Convoy effect (Imagine a situation with multiple I/O (input/output) bound processes and only one CPU bound process. The I/O (input/output) bound processes must wait until the CPU bound process receives the CPU. The I/O (input/output) bound process should have used the CPU for a while before using I/O (input/output) devices).


Related Topics

Passing a Vector to a function in C++

A pointer is sent to the function when we feed it an array. However, there are two ways to pass a vector: Pass by Value Pass by Reference A copy of a vector...

3 minutes read.

C++ Virtual Destructor

In C++, a destructor is a class member function that is used to free up space or remove an object of the class that has gone out of scope. The...

4 minutes read.

How the value is passed in C++

Introduction: The call-by-value method of giving arguments to a function duplicates the real value of an argument into the formal parameter of the function. In this instance, modifications to the parameter...

5 minutes read.

Scope Resolution Operator in C++

The scope resolution operator and its different usage in the C++ programming language will be discussed in this section. The scope resolution operator is used to refer to an out-of-scope...

6 minutes read.

Maps in C++

Maps: Maps in C++ are the containers associated with key and mapped values. By keys and mapped values, we mean that the maps are used to store elements formed by the...

4 minutes read.

C++ STL Components

C++ STL Components In today’s article, we are going to learn about all the points things that is related to STL in C++ so stay connected because you are going to...

6 minutes read.

How to find the length of the vector in C++

Like dynamic arrays, vectors can automatically adjust their size when an element is added or removed, and the container manages its storage. Because vector items are stored in contiguous storage, iterators...

3 minutes read.

C++ if-else

C++ if else Control Statement if else control statement in C++ is used to control the program flow in a two-way direction. When condition returns a true value, then the program executes if condition block otherwise...

1 minute read.

Decimal to Binary in C++

What is the meaning of Decimal Numbers? Decimal numbers range from 0 to 9, there are a total of ten digits between 0 and 9. Any number with more than two...

3 minutes read.

C++ User Defined Exceptions

Overriding and inheriting exception class capabilities may be used to define the new exception. Exception handling can also be used with classes. We may also make an exception for user-defined...

4 minutes read.

swap() function in C++

Swap() function: swap() function in C++: swap() function is a pre-define function in c++ present in STL( Standard template library ). It is used to swap two numbers. It takes two mandatory...

6 minutes read.

Virtual base class in C++

Consider in a C++ program, there are 4 classes named class A, class B, class C, and class D. If class B and class c inherit properties from class A....

3 minutes read.

C++ program to read string using cin.getline()

C++ program to read string using cin.getline() C++ getline() is a standard library feature for reading a string or a line from an input source. A getline() function gets characters from...

3 minutes read.

Do-While Loop Examples in C++

Before we move on the examples of Do-While loop, let’s learn little bit about the Do-While loop in C++ language. Do While loop An iterative loop that checks the condition at the...

5 minutes read.

C++ Goto

In this article, we will discuss the C++ goto statement with its syntax, use, key features, key points, pseudo code, and examples. What is the goto statement in C++? In C++, the...

4 minutes read.

List back () function in C++ STL

The list::back () function of the C++ STL returns a direct reference to the last element in the list container. This function varies from list::end (), which just returns an...

2 minutes read.

Difference between OOP and POP in C++

Object-Oriented Programming (OOP) Prioritizes data over methods (functions) and treats data as an essential component of program development.  OOP prohibits the free flow of data throughout the system. Tighter ties to data manipulation...

4 minutes read.

Diamond Pattern Using Do-While loop in C++

What is Do-While Loop? An iterative loop that checks the condition at the end.The Do-While loop can be used whenever a test condition is specific, as the control enters the loop...

5 minutes read.

C++ Try-Catch

Every useful program will eventually encounter unexpected outcomes. By entering data that are incorrect, users might create mistakes. Sometimes the program's creator didn't consider all of the options or was...

7 minutes read.

C++ Queue

C++ queue: Queue in C++ is also a container adapter with the functionality of a queue. Queue is just the opposite of the stack in C++ because stack works on...

4 minutes read.