C++ Program for FCFS Scheduling Algorithm

C++C++Beginner
Practice Now

Introduction

In this lab, we will learn how to implement the First Come First Serve (FCFS) Scheduling Algorithm using C++ programming language. FCFS is the simplest scheduling algorithm which schedules the jobs according to their arrival time. In this algorithm, the job which arrives first in the ready queue will get the CPU first. This lab is suitable for beginners who want to learn about the FCFS scheduling algorithm.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL cpp(("`C++`")) -.-> cpp/SyntaxandStyleGroup(["`Syntax and Style`"]) cpp(("`C++`")) -.-> cpp/BasicsGroup(["`Basics`"]) cpp(("`C++`")) -.-> cpp/ControlFlowGroup(["`Control Flow`"]) cpp(("`C++`")) -.-> cpp/FunctionsGroup(["`Functions`"]) cpp/SyntaxandStyleGroup -.-> cpp/comments("`Comments`") cpp/BasicsGroup -.-> cpp/variables("`Variables`") cpp/BasicsGroup -.-> cpp/data_types("`Data Types`") cpp/BasicsGroup -.-> cpp/operators("`Operators`") cpp/ControlFlowGroup -.-> cpp/for_loop("`For Loop`") cpp/BasicsGroup -.-> cpp/arrays("`Arrays`") cpp/FunctionsGroup -.-> cpp/function_parameters("`Function Parameters`") subgraph Lab Skills cpp/comments -.-> lab-96161{{"`C++ Program for FCFS Scheduling Algorithm`"}} cpp/variables -.-> lab-96161{{"`C++ Program for FCFS Scheduling Algorithm`"}} cpp/data_types -.-> lab-96161{{"`C++ Program for FCFS Scheduling Algorithm`"}} cpp/operators -.-> lab-96161{{"`C++ Program for FCFS Scheduling Algorithm`"}} cpp/for_loop -.-> lab-96161{{"`C++ Program for FCFS Scheduling Algorithm`"}} cpp/arrays -.-> lab-96161{{"`C++ Program for FCFS Scheduling Algorithm`"}} cpp/function_parameters -.-> lab-96161{{"`C++ Program for FCFS Scheduling Algorithm`"}} end

Include required header files and namespace

We will create a new file named main.cpp in the ~/project directory using the following command:

touch ~/project/main.cpp

Include the necessary header files for input/output processing and declare the namespace used in the program.

#include<iostream>

using namespace std;

Define main function and variables

Define the main() function and declare the variables needed for the FCFS algorithm.

int main() {
    int n, burst_time[20], waiting_time[20], turnaround_time[20], total_waiting_time = 0, total_turnaround_time = 0;
    cout << "Enter total number of processes (maximum 20): ";
    cin >> n;

Get the Burst Time for each process

Get the burst time for each process using a loop. The burst time for each process represents the duration required to complete the process.

cout << "\nEnter the Burst Time for each process\n";
    for(int i = 0; i < n; i++) {
        cout << "Process " << i + 1 << ": ";
        cin >> burst_time[i];
    }

Calculate Waiting Time for each process

Calculate the waiting time for each process using a loop. The waiting time represents the time spent in the ready queue by each process before being executed.

// Calculate the waiting time for each process
    waiting_time[0] = 0;  // Waiting time for the first process is 0
    for(int i = 1; i < n; i++) {
        waiting_time[i] = 0;
        for(int j = 0; j < i; j++) {
            waiting_time[i] += burst_time[j];
        }
    }

Calculate Turnaround Time for each process

Calculate the turnaround time for each process using a loop. The turnaround time represents the time between the process arrival and its completion.

// Calculate the turnaround time for each process and add up to the total waiting time and turnaround time
    cout << "\nProcess\t\tBurst Time\tWaiting Time\tTurnaround Time";
    for(int i = 0; i < n; i++) {
        turnaround_time[i] = burst_time[i] + waiting_time[i];
        total_waiting_time += waiting_time[i];
        total_turnaround_time += turnaround_time[i];
        cout << "\nP[" << i + 1 << "]" << "\t\t" << burst_time[i] << "\t\t" << waiting_time[i] << "\t\t" << turnaround_time[i];
    }

Calculate and Print Average Waiting Time and Turnaround Time

Calculate the average waiting time and average turnaround time using the formula and print them.

// Calculate the average waiting time and average turnaround time
    float average_waiting_time = (float)total_waiting_time / (float)n;
    float average_turnaround_time = (float)total_turnaround_time / (float)n;
    cout << "\n\nAverage Waiting Time: " << average_waiting_time;
    cout << "\nAverage Turnaround Time: " << average_turnaround_time;

    return 0;
}

To run the code in the terminal:

g++ main.cpp -o main
./main

Summary

In this lab, we have learned how to implement the First Come First Serve (FCFS) Scheduling Algorithm using C++ programming language. FCFS is the simplest scheduling algorithm which schedules the jobs according to their arrival time. We have covered the following concepts:

  • FCFS algorithm
  • Calculation of waiting time and turnaround time
  • Calculation of average waiting time and average turnaround time

With this lab, beginners can now implement the FCFS scheduling algorithm with ease using C++ programming language.

Other C++ Tutorials you may like