×

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

C++ Installation

Let's install C++ setup to start programming in C++. C++ setup contains C++ compiler which is required in your system. There are lots of C++ compilers available, you must choose...

1 minute read.

Data Hiding in C++

C++ : High-performance apps can be made using the cross-platform language C++. Bjarne Stroustrup created C++ as an addition to the C language. Programmers have extensive control over memory and system...

3 minutes read.

sort() function in C++

This tutorial covers the various built-in sort functions found in the C++ algorithm’s library.  What Does C++ Sort Mean? The concept of sorting in C++ entails rearranging an array's elements in a...

3 minutes read.

fscanf() Function in the C++

In the C++ programming language, the fscanf() method can be used to read data from a file stream. Syntax: The syntax for the fscanf() function in the C++ programming language is as...

3 minutes read.

Web Development in C++

Before learning above C++ web development, we need to learn about CGI What is CGI? CGI stands for common gateway interface. CGI is a standard that tells us how the exchange of...

4 minutes read.

How to create a library in C++

Before going on to the creation of a library, let’s understand its meaning. What is a library? In simple words, a library is a collection of numerous functions, methods, classes, header files...

7 minutes read.

Factory Method for Designing Pattern in C++

In C++, the factory method is a type of conditional design pattern. The factory method is related to creating a new object in C++. With the help of a factory...

3 minutes read.

C++ References

C++ References An alias is a reference variable that is another name for a variable already in existence. If a relation is initialized with a variable, it is possible to use...

3 minutes read.

Top best IDEs for C/C++ Developers in 2024

Nothing in the current digital world is conceivable without programming. Everything needs programming, from the cell phones in our pockets to self-driving cars. Programming is also necessary for the mouse...

9 minutes read.

Returning a Function Pointer from a Function in C/C++

Pointers to functions can be used in the C programming language just like standard data pointers such as "int *," "char *," etc. The following is a basic example of a...

3 minutes read.

Scope Resolution Operator vs this Pointer in C++

In this tutorial, we will compare the Scope Resolution operation to this Pointer in C++ language. Scope Resolution Operator The Scope Resolution Operator in C++ programming language is usually denoted by (::)....

3 minutes read.

Dynamic _Cast in C++

C++ is one of the most powerful programming languages. We can write object-oriented and structured programming with the help of C++. In this article, we will learn about the dynamic...

3 minutes read.

Type difference of Character literals in C VS C++

Character literals in C: In C, a character literal is represented by a single character enclosed in single quotes, such as 'a' or 'b'. It is of type int. This means...

5 minutes read.

Queue in C++

What is Queue? As the name suggests, the queue is the type of data structure that follows the FIFO (First In - First Out) mechanism. In simple words, it is...

4 minutes read.

C++ Continue in for loop

Continue statement: Inside the loop, the continue statement is used to control the loop. C++ utilizes the continue keyword to implement the continue statement, which transfers the program's flow at the start...

4 minutes 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++ Inheritance

What do you mean by Inheritance ? The ability to define new classes based on existing classes in order to reuse and organise code is referred to as inheritance. Single inheritance...

7 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.

How to Sort an Array in C++

What is Sorting? Sorting is a process of arranging elements in sequential order, either numerically or alphabetically. The sorting of a numerical array can be accomplished using a variety of algorithms,...

4 minutes read.

Hybrid Inheritance

C++ Hybrid Inheritance When more than one type of inheritance is combined in single inheritance is called as hybrid inheritance. C++ Hybrid Inheritance Example #include<iostream>   using namespace std;   class Student{       protected:           int rollno;       public:           void getRoll(int a){               rollno=a;           }           void putRoll(void){               cout <<"Roll No: "<< rollno<<endl;           }   };   class Test : public Student{       protected:           float subject1, subject2;       public:           void getMarks(float x, float y){               subject1=x;               subject2=y;           }           void putMarks(void){               cout<< "Marks gain: "<<endl <<"Subject1 =  "<<subject1<<endl<<"Subject2 = "<<subject2 <<endl;           }   };   class Sport{       protected:           float score;       public:           void getScore(float s){               score=s;           }           void putScore(void){               cout<<"Sports score: "<<score<<endl;           }   };   class Result : public Test, public Sport{       float total;       public:           void display(void);   };   void Result:: display(void){       total=subject1+subject2+score;       putRoll();       putMarks();       putScore();       cout<<"Total Score: "<<total<<endl;   }   int main(){       Result stu;       stu.getRoll(10);       stu.getMarks(40,50);       stu.getScore(60);       stu.display();       return 0;   } Output: Roll No: 10 Marks gain: Subject1 = 40 Subject2 = 50 Sports score: 60 Total...

1 minute read.