Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Aptitude
  • Engineering Mathematics
  • Discrete Mathematics
  • Operating System
  • DBMS
  • Computer Networks
  • Digital Logic and Design
  • C Programming
  • Data Structures
  • Algorithms
  • Theory of Computation
  • Compiler Design
  • Computer Org and Architecture
Open In App
Next Article:
Program for SSTF Disk Scheduling Algorithm
Next article icon

Program for FCFS CPU Scheduling | Set 1

Last Updated : 14 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

First come – First served (FCFS), is the simplest scheduling algorithm. FIFO simply queues processes according to the order they arrive in the ready queue. In this algorithm, the process that comes first will be executed first and next process starts only after the previous gets fully executed. 

Terminologies Used in CPU Scheduling 

  • Arrival Time: The time at which the process arrives in the ready queue.
  • Completion Time: The time at which the process completes its execution.
  • Turn Around Time: Time Difference between completion time and arrival time. Turn Around Time = (Completion Time – Arrival Time)
  • Waiting Time (W. T): Time Difference between turnaround time and burst time. CPU Burst time is the overall CPU time a process needs.
  • Waiting Time = (Turn Around Time – Burst Time).

In this example, we have assumed the arrival time of all processes is 0, so turnaround and completion times are the same. 

Implementation

Input : Processes along with their burst time (bt).
Output : Waiting time, turnaround time for all processes.

  1. As first process that comes need not to wait so waiting time for process 1 will be 0 i.e. wt[0] = 0.
  2. Find waiting time for all other processes i.e. for process i:
    wt[i] = bt[i-1] + wt[i-1] .
  3. Find turnaround time = waiting_time + burst_time for all processes.
  4. Find average waiting time = total_waiting_time / no_of_processes.
  5. Similarly, find average turnaround time =
    total_turn_around_time / no_of_processes.

Code to Implement the Algorithm

C++
// C++ program for implementation of FCFS  // scheduling #include<iostream> using namespace std;  // Function to find the waiting time for all  // processes void findWaitingTime(int processes[], int n,                            int bt[], int wt[]) {     // waiting time for first process is 0     wt[0] = 0;      // calculating waiting time     for (int  i = 1; i < n ; i++ )         wt[i] =  bt[i-1] + wt[i-1] ; }  // Function to calculate turn around time void findTurnAroundTime( int processes[], int n,                    int bt[], int wt[], int tat[]) {     // calculating turnaround time by adding     // bt[i] + wt[i]     for (int  i = 0; i < n ; i++)         tat[i] = bt[i] + wt[i]; }  //Function to calculate average time void findavgTime( int processes[], int n, int bt[]) {     int wt[n], tat[n], total_wt = 0, total_tat = 0;      //Function to find waiting time of all processes     findWaitingTime(processes, n, bt, wt);      //Function to find turn around time for all processes     findTurnAroundTime(processes, n, bt, wt, tat);      //Display processes along with all details     cout << "Processes  "<< " Burst time  "          << " Waiting time  " << " Turn around time\n";      // Calculate total waiting time and total turn      // around time     for (int  i=0; i<n; i++)     {         total_wt = total_wt + wt[i];         total_tat = total_tat + tat[i];         cout << "   " << i+1 << "\t\t" << bt[i] <<"\t    "             << wt[i] <<"\t\t  " << tat[i] <<endl;     }      cout << "Average waiting time = "           << (float)total_wt / (float)n;     cout << "\nAverage turn around time = "           << (float)total_tat / (float)n; }  // Driver code int main() {     //process id's     int processes[] = { 1, 2, 3};     int n = sizeof processes / sizeof processes[0];      //Burst time of all processes     int  burst_time[] = {10, 5, 8};      findavgTime(processes, n,  burst_time);     return 0; } 
C
// C program for implementation of FCFS   // scheduling  #include<stdio.h>  // Function to find the waiting time for all   // processes  void findWaitingTime(int processes[], int n,                             int bt[], int wt[])  {      // waiting time for first process is 0      wt[0] = 0;         // calculating waiting time      for (int  i = 1; i < n ; i++ )          wt[i] =  bt[i-1] + wt[i-1] ;  }     // Function to calculate turn around time  void findTurnAroundTime( int processes[], int n,                     int bt[], int wt[], int tat[])  {      // calculating turnaround time by adding      // bt[i] + wt[i]      for (int  i = 0; i < n ; i++)          tat[i] = bt[i] + wt[i];  }     //Function to calculate average time  void findavgTime( int processes[], int n, int bt[])  {      int wt[n], tat[n], total_wt = 0, total_tat = 0;         //Function to find waiting time of all processes      findWaitingTime(processes, n, bt, wt);         //Function to find turn around time for all processes      findTurnAroundTime(processes, n, bt, wt, tat);         //Display processes along with all details      printf("Processes   Burst time   Waiting time   Turn around time\n");         // Calculate total waiting time and total turn       // around time      for (int  i=0; i<n; i++)      {          total_wt = total_wt + wt[i];          total_tat = total_tat + tat[i];          printf("   %d ",(i+1));         printf("       %d ", bt[i] );         printf("       %d",wt[i] );         printf("       %d\n",tat[i] );      }      float s=(float)total_wt / (float)n;     float t=(float)total_tat / (float)n;     printf("Average waiting time = %f",s);     printf("\n");     printf("Average turn around time = %f ",t);  }     // Driver code  int main()  {      //process id's      int processes[] = { 1, 2, 3};      int n = sizeof processes / sizeof processes[0];         //Burst time of all processes      int  burst_time[] = {10, 5, 8};         findavgTime(processes, n,  burst_time);      return 0;  }  // This code is contributed by Shivi_Aggarwal 
Java
// Java program for implementation of FCFS  // scheduling   import java.text.ParseException;  class GFG {      // Function to find the waiting time for all      // processes      static void findWaitingTime(int processes[], int n,             int bt[], int wt[]) {         // waiting time for first process is 0          wt[0] = 0;          // calculating waiting time          for (int i = 1; i < n; i++) {             wt[i] = bt[i - 1] + wt[i - 1];         }     }      // Function to calculate turn around time      static void findTurnAroundTime(int processes[], int n,             int bt[], int wt[], int tat[]) {         // calculating turnaround time by adding          // bt[i] + wt[i]          for (int i = 0; i < n; i++) {             tat[i] = bt[i] + wt[i];         }     }      //Function to calculate average time      static void findavgTime(int processes[], int n, int bt[]) {         int wt[] = new int[n], tat[] = new int[n];         int total_wt = 0, total_tat = 0;          //Function to find waiting time of all processes          findWaitingTime(processes, n, bt, wt);          //Function to find turn around time for all processes          findTurnAroundTime(processes, n, bt, wt, tat);          //Display processes along with all details          System.out.printf("Processes Burst time Waiting"                        +" time Turn around time\n");          // Calculate total waiting time and total turn          // around time          for (int i = 0; i < n; i++) {             total_wt = total_wt + wt[i];             total_tat = total_tat + tat[i];             System.out.printf(" %d ", (i + 1));             System.out.printf("     %d ", bt[i]);             System.out.printf("     %d", wt[i]);             System.out.printf("     %d\n", tat[i]);         }         float s = (float)total_wt /(float) n;         int t = total_tat / n;         System.out.printf("Average waiting time = %f", s);         System.out.printf("\n");         System.out.printf("Average turn around time = %d ", t);     }      // Driver code      public static void main(String[] args) throws ParseException {         //process id's          int processes[] = {1, 2, 3};         int n = processes.length;          //Burst time of all processes          int burst_time[] = {10, 5, 8};          findavgTime(processes, n, burst_time);      } } // This code is contributed by 29ajaykumar  
Python
# Python3 program for implementation  # of FCFS scheduling  # Function to find the waiting  # time for all processes def findWaitingTime(processes, n,                     bt, wt):      # waiting time for      # first process is 0     wt[0] = 0      # calculating waiting time     for i in range(1, n ):         wt[i] = bt[i - 1] + wt[i - 1]   # Function to calculate turn # around time def findTurnAroundTime(processes, n,                         bt, wt, tat):      # calculating turnaround      # time by adding bt[i] + wt[i]     for i in range(n):         tat[i] = bt[i] + wt[i]  # Function to calculate # average time def findavgTime( processes, n, bt):      wt = [0] * n     tat = [0] * n      total_wt = 0     total_tat = 0      # Function to find waiting      # time of all processes     findWaitingTime(processes, n, bt, wt)      # Function to find turn around      # time for all processes     findTurnAroundTime(processes, n,                         bt, wt, tat)      # Display processes along     # with all details     print( "Processes Burst time " +                    " Waiting time " +                  " Turn around time")      # Calculate total waiting time      # and total turn around time     for i in range(n):              total_wt = total_wt + wt[i]         total_tat = total_tat + tat[i]         print(" " + str(i + 1) + "\t\t" +                      str(bt[i]) + "\t " +                      str(wt[i]) + "\t\t " +                      str(tat[i]))       print( "Average waiting time = "+                    str(total_wt / n))     print("Average turn around time = "+                      str(total_tat / n))  # Driver code if __name__ =="__main__":          # process id's     processes = [ 1, 2, 3]     n = len(processes)      # Burst time of all processes     burst_time = [10, 5, 8]      findavgTime(processes, n, burst_time)  # This code is contributed  # by ChitraNayal 
C#
// C# program for implementation of FCFS  // scheduling  using System;      class GFG  {      // Function to find the waiting time for all      // processes      static void findWaitingTime(int []processes, int n,                                 int []bt, int[] wt)      {         // waiting time for first process is 0          wt[0] = 0;          // calculating waiting time          for (int i = 1; i < n; i++)          {             wt[i] = bt[i - 1] + wt[i - 1];         }     }      // Function to calculate turn around time      static void findTurnAroundTime(int []processes, int n,             int []bt, int []wt, int []tat) {         // calculating turnaround time by adding          // bt[i] + wt[i]          for (int i = 0; i < n; i++)          {             tat[i] = bt[i] + wt[i];         }     }      // Function to calculate average time      static void findavgTime(int []processes, int n, int []bt)     {         int []wt = new int[n];         int []tat = new int[n];         int total_wt = 0, total_tat = 0;          //Function to find waiting time of all processes          findWaitingTime(processes, n, bt, wt);          //Function to find turn around time for all processes          findTurnAroundTime(processes, n, bt, wt, tat);          //Display processes along with all details          Console.Write("Processes Burst time Waiting"                     +" time Turn around time\n");          // Calculate total waiting time and total turn          // around time          for (int i = 0; i < n; i++)         {             total_wt = total_wt + wt[i];             total_tat = total_tat + tat[i];             Console.Write(" {0} ", (i + 1));             Console.Write("     {0} ", bt[i]);             Console.Write("     {0}", wt[i]);             Console.Write("     {0}\n", tat[i]);         }         float s = (float)total_wt /(float) n;         int t = total_tat / n;         Console.Write("Average waiting time = {0}", s);         Console.Write("\n");         Console.Write("Average turn around time = {0} ", t);     }      // Driver code      public static void Main(String[] args)     {         // process id's          int []processes = {1, 2, 3};         int n = processes.Length;          // Burst time of all processes          int []burst_time = {10, 5, 8};          findavgTime(processes, n, burst_time);     } }  // This code contributed by Rajput-Ji 
JavaScript
<script>      // JavaScript program for implementation of FCFS     // scheduling            // Function to find the waiting time for all    // processes     function findWaitingTime(processes,n,bt,wt)     {         // waiting time for first process is 0          wt[0] = 0;            // calculating waiting time          for (let i = 1; i < n; i++) {             wt[i] = bt[i - 1] + wt[i - 1];         }     }          function findTurnAroundTime(processes,n,bt,wt,tat)     {         // calculating turnaround time by adding          // bt[i] + wt[i]          for (let i = 0; i < n; i++) {             tat[i] = bt[i] + wt[i];         }     }          function findavgTime(processes,n,bt)     {         let wt = new Array(n), tat = new Array(n);         for(let i=0;i<n;i++)         {             wt[i]=0;             tat[i]=0;         }         let total_wt = 0, total_tat = 0;            //Function to find waiting time of all processes          findWaitingTime(processes, n, bt, wt);            //Function to find turn around time for all processes          findTurnAroundTime(processes, n, bt, wt, tat);            //Display processes along with all details          document.write("Processes Burst time Waiting"                        +" time Turn around time<br>");            // Calculate total waiting time and total turn          // around time          for (let i = 0; i < n; i++) {             total_wt = total_wt + wt[i];             total_tat = total_tat + tat[i];             document.write("    ", (i + 1)+" ");             document.write("     "+  bt[i]+" ");             document.write("     "+ wt[i]);             document.write("     "+ tat[i]+"<br>");         }         let s = total_wt / n;         let t = Math.floor(total_tat / n);         document.write("Average waiting time = "+ s);         document.write("<br>");         document.write("Average turn around time = ", t+" ");     }          let processes=[1,2,3];     let  n = processes.length;          let burst_time=[10,5,8];     findavgTime(processes, n, burst_time);          // This code is contributed by rag2127      </script>  

Output: 

Processes

Burst Time

Waiting Time

Turn-around Time

1

10

0

10

2

5

10

15

3

8

15

23

Average waiting time = 8.33333
Average turn around time = 16

Implementation Using OOPS

C++
#include <bits/stdc++.h> using namespace std;  class Process { private:     int at;     int bt;     int ct;     int tat;     int wt;     int pid;  public:     int& operator[](string var)     {         if (var == "at")             return at;         if (var == "bt")             return bt;         if (var == "ct")             return ct;         if (var == "tat")             return tat;         if (var == "wt")             return wt;         return pid;     }      void update_after_ct()     {         tat = ct - at;         wt = tat - bt;     }      void display()     {         printf("%d\t%d\t%d\t%d\t%d\t%d\n", pid, at, bt, ct,                tat, wt);     } };  float average(vector<Process> P, string var) {     int total = 0;     for (auto temp : P) {         total += temp[var];     }     return (float)total / P.size(); }  int main() {     /*     Input description.     First line contains an integer n     the next n lines contains 2 space separated integers     containing values for arrival time and burst time for     example:     2     0 3     1 2     */     int n;     cin >> n;     int counter = 0;     vector<Process> P(n);     for (Process& temp : P) {         temp["id"] = counter++;         cin >> temp["at"] >> temp["bt"];     }     sort(P.begin(), P.end(),          [](Process first, Process second) {              return first["at"] < second["at"];          });     printf("pid\tat\tbt\tct\ttat\twt\n");     P[0]["ct"] = P[0]["at"] + P[0]["bt"];     P[0].update_after_ct();     P[0].display();     for (int i = 1; i < P.size(); i++) {         if (P[i]["at"] < P[i - 1]["ct"]) {             P[i]["ct"] = P[i - 1]["ct"] + P[i]["bt"];         }         else {             printf("curr['at'] : %d, prev['ct'] : %d\n",                    P[i]["at"], P[i - 1]["ct"]);             P[i]["ct"] = P[i]["at"] + P[i]["bt"];         }         P[i].update_after_ct();         P[i].display();     }      printf("Average waiting time : %f\n", average(P, "wt"));     return 0; } 
Java
import java.util.*;  public class Main {          // Process class to represent a process     static class Process {         private int at; // arrival time         private int bt; // burst time         private int ct; // completion time         private int tat; // turn around time         private int wt; // waiting time         private int pid; // process ID          // Getter method to get a variable value of the process         public int getVar(String var) {             if (var.equals("at"))                 return at;             if (var.equals("bt"))                 return bt;             if (var.equals("ct"))                 return ct;             if (var.equals("tat"))                 return tat;             if (var.equals("wt"))                 return wt;             return pid;         }          // Setter method to set a variable value of the process         public void setVar(String var, int value) {             if (var.equals("at"))                 at = value;             else if (var.equals("bt"))                 bt = value;             else if (var.equals("ct"))                 ct = value;             else if (var.equals("tat"))                 tat = value;             else if (var.equals("wt"))                 wt = value;             else                 pid = value;         }          // Update the turn around time and waiting time after completion         public void updateAfterCt() {             tat = ct - at;             wt = tat - bt;         }          // Display the process details         public void display() {             System.out.printf("%d\t%d\t%d\t%d\t%d\t%d\n", pid, at, bt, ct, tat, wt);         }     }          // Calculate the average of a variable value for all processes     public static float average(ArrayList<Process> P, String var) {         int total = 0;         for (Process temp : P) {             total += temp.getVar(var);         }         return (float) total / P.size();     }          public static void main(String[] args) {         /*         Input description.         First line contains an integer n         the next n lines contains 2 space separated integers         containing values for arrival time and burst time for         example:         2         0 3         1 2         */         Scanner sc = new Scanner(System.in);         int n = sc.nextInt();         int counter = 0;         ArrayList<Process> P = new ArrayList<Process>(n);         // Create a process object for each input and add to the process list         for (int i = 0; i < n; i++) {             Process temp = new Process();             temp.setVar("pid", counter++);             temp.setVar("at", sc.nextInt());             temp.setVar("bt", sc.nextInt());             P.add(temp);         }         // Sort the process list by arrival time         Collections.sort(P, new Comparator<Process>() {             public int compare(Process first, Process second) {                 return first.getVar("at") - second.getVar("at");             }         });         System.out.println("pid\tat\tbt\tct\ttat\twt");         // Calculate completion time and display the details of the first process         P.get(0).setVar("ct", P.get(0).getVar("at") + P.get(0).getVar("bt"));         P.get(0).updateAfterCt();         P.get(0).display();         // Calculate completion time and display the details of the remaining processes        for (int i = 1; i < P.size(); i++) {            // Loop through the remaining processes         if (P.get(i).getVar("at") < P.get(i - 1).getVar("ct")) {             // If the process arrives before the previous process completes             P.get(i).setVar("ct", P.get(i - 1).getVar("ct") + P.get(i).getVar("bt"));             // Calculate completion time as the completion time of previous process plus its burst time         } else {             // If the process arrives after the previous process completes             System.out.printf("curr['at'] : %d, prev['ct'] : %d\n\n", P.get(i).getVar("at"),                     P.get(i - 1).getVar("ct"));             P.get(i).setVar("ct", P.get(i).getVar("at") + P.get(i).getVar("bt"));             // Calculate completion time as the arrival time plus its burst time         }         P.get(i).updateAfterCt(); // Update the turnaround time and waiting time for the current process         P.get(i).display(); // Display the details of the current process     }      System.out.printf("Average waiting time : %f\n", average(P, "wt"));     sc.close(); // Close the scanner } } 
Python
class processes:     def __init__(self, id, at, bt, ct):         self.id = id         self.at = at         self.bt = bt         self.ct = ct         self.tat = self.ct-self.at         self.wt = self.tat-self.bt      def get(self):         print(f"{self.id}\t{self.at}\t{self.bt}\t{self.ct}\t{self.tat}\t{self.wt}")      def turnaround(self):         return self.tat      def waiting(self):         return self.wt   num = int(input("Enter the Number of Processes:")) l = [] ct = 0  for i in range(num):      print(f'Process {i+1}')     at = int(input("Enter the Arrival Time:-"))     bt = int(input("Enter the Burst Time:-"))     if (len(l) == 0):         ct = bt         l.append(processes(i, at, bt, ct))     else:         ct += bt         l.append(processes(i, at, bt, ct))      print("\n") avg_tat = 0 avg_wat = 0 print("PID\tAT\tBT\tCT\tTAT\tWT") for process in l:     process.get()  for process in l:     avg_tat += process.turnaround()     avg_wat += process.waiting() print(f"Avg_turnaround:{avg_tat/num}\nAvg_Waitingtime:{avg_wat/num}")  # pid at bt # 0 0 5 # 1 2 3 # 2 6 2 # 3 7 3 
C#
using System; using System.Collections.Generic;  class Process {     private int at;     private int bt;     private int ct;     private int tat;     private int wt;     private int pid;      public int this[string var]     {         get         {             if (var == "at")                 return at;             if (var == "bt")                 return bt;             if (var == "ct")                 return ct;             if (var == "tat")                 return tat;             if (var == "wt")                 return wt;             return pid;         }         set         {             if (var == "at")                 at = value;             else if (var == "bt")                 bt = value;             else if (var == "ct")                 ct = value;             else if (var == "tat")                 tat = value;             else if (var == "wt")                 wt = value;             else                 pid = value;         }     }      public void UpdateAfterCt()     {         tat = ct - at;         wt = tat - bt;     }      public void Display()     {         Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}",             pid, at, bt, ct, tat, wt);     } }  class Program {     static float Average(List<Process> P, string var)     {         int total = 0;         foreach (var temp in P)         {             total += temp[var];         }         return (float)total / P.Count;     }      static void Main()     {         /*         Input description.         First line contains an integer n         the next n lines contains 2 space separated integers         containing values for arrival time and burst time for         example:         2         0 3         1 2         */         int n = int.Parse(Console.ReadLine());         int counter = 0;         var P = new List<Process>(n);         for (int i = 0; i < n; i++)         {             var temp = new Process();             temp["id"] = counter++;             var line = Console.ReadLine().Split();             temp["at"] = int.Parse(line[0]);             temp["bt"] = int.Parse(line[1]);             P.Add(temp);         }         P.Sort((first, second) => first["at"].CompareTo(second["at"]));         Console.WriteLine("pid\tat\tbt\tct\ttat\twt");         P[0]["ct"] = P[0]["at"] + P[0]["bt"];         P[0].UpdateAfterCt();         P[0].Display();         for (int i = 1; i < P.Count; i++)         {             if (P[i]["at"] < P[i - 1]["ct"])             {                 P[i]["ct"] = P[i - 1]["ct"] + P[i]["bt"];             }             else             {                 Console.WriteLine("curr['at'] : {0}, prev['ct'] : {1}",                     P[i]["at"], P[i - 1]["ct"]);                 P[i]["ct"] = P[i]["at"] + P[i]["bt"];             }             P[i].UpdateAfterCt();             P[i].Display();         }         Console.WriteLine("Average waiting time : {0}", Average(P, "wt"));     } } 
JavaScript
class Process {   constructor() {     this.at = 0;     this.bt = 0;     this.ct = 0;     this.tat = 0;     this.wt = 0;     this.pid = 0;   }    update_after_ct() {     this.tat = this.ct - this.at;     this.wt = this.tat - this.bt;   }    display() {     console.log(`${this.pid}\t${this.at}\t${this.bt}\t${this.ct}\t${this.tat}\t${this.wt}`);   } }  function average(P, varName) {   let total = 0;   for (let i = 0; i < P.length; i++) {     total += P[i][varName];   }   return total / P.length; }  function main() {   /*   Input description.   First line contains an integer n   the next n lines contains 2 space separated integers   containing values for arrival time and burst time for   example:   2   0 3   1 2   */   const readline = require("readline");   const rl = readline.createInterface({     input: process.stdin,     output: process.stdout,   });    rl.question("", (n) => {     const P = new Array(parseInt(n)).fill(null).map(() => new Process());     let counter = 0;     rl.on("line", (line) => {       const [at, bt] = line.trim().split(" ");       P[counter]["pid"] = counter++;       P[counter - 1]["at"] = parseInt(at);       P[counter - 1]["bt"] = parseInt(bt);        if (counter == P.length) {         rl.close();       }     });      rl.on("close", () => {       P.sort((first, second) => first["at"] - second["at"]);        console.log("pid\tat\tbt\tct\ttat\twt");       P[0]["ct"] = P[0]["at"] + P[0]["bt"];       P[0].update_after_ct();       P[0].display();        for (let i = 1; i < P.length; i++) {         if (P[i]["at"] < P[i - 1]["ct"]) {           P[i]["ct"] = P[i - 1]["ct"] + P[i]["bt"];         } else {           console.log(             `curr['at'] : ${P[i]["at"]}, prev['ct'] : ${P[i - 1]["ct"]}`           );           P[i]["ct"] = P[i]["at"] + P[i]["bt"];         }         P[i].update_after_ct();         P[i].display();       }        console.log(`Average waiting time : ${average(P, "wt")}`);     });   }); }  main(); 

PID

AT

BT

CT

TAT

WT

0

0

10

10

10

0

1

10

5

15

5

0

2

15

8

23

8

0

Avg_turnaround:7.666666666666667
Avg_Waitingtime:0.0


Next Article
Program for SSTF Disk Scheduling Algorithm
author
kartik
Improve
Article Tags :
  • Operating Systems
  • cpu-scheduling
  • Process Management

Similar Reads

  • Program for SSTF Disk Scheduling Algorithm
    Given an array of disk track numbers and initial head position, our task is to find the total number of seek operations done to access all the requested tracks if Shortest Seek Time First (SSTF) is a disk scheduling algorithm is used. The basic idea is the tracks that are closer to the current disk
    10 min read
  • Program for HRRN CPU Scheduling Algorithm
    The Highest Response Ratio Next (HRRN) scheduling algorithm is a non-preemptive scheduling technique used in operating systems to manage the execution of processes. It is designed to improve upon simpler algorithms like First-Come-First-Serve (FCFS) and Shortest Job Next (SJN) by balancing both the
    15 min read
  • Shortest Job First or SJF CPU Scheduling
    Shortest Job First (SJF) or Shortest Job Next (SJN) is a scheduling process that selects the waiting process with the smallest execution time to execute next. This scheduling method may or may not be preemptive. Significantly reduces the average waiting time for other processes waiting to be execute
    4 min read
  • FCFS Scheduling Full Form - First Come First Serve
    FCFS stands for First Come First Serve. In the FCFS scheduling algorithm, the job that arrived first in the ready queue is allocated to the CPU and then the job that came second, and so on. We can say that the ready queue acts as a FIFO (First In First Out) queue thus the arriving jobs/processes are
    3 min read
  • Fair-share CPU scheduling
    Fair-share scheduling is a scheduling algorithm that was first designed by Judy Kay and Piers Lauder at Sydney University in the 1980s. It is a scheduling algorithm for computer operating systems that dynamically distributes the time quanta "equally" to its users. Time quantum is the processor time
    2 min read
  • Longest Remaining Time First (LRTF) CPU Scheduling Program
    We have given some processes with arrival time and Burst Time and we have to find the completion time (CT), Turn Around Time(TAT), Average Turn Around Time (Avg TAT), Waiting Time(WT), Average Waiting Time (AWT) for the given processes. Prerequisite: CPU Scheduling | Longest Remaining Time First (LR
    15+ min read
  • Program for Preemptive Priority CPU Scheduling
    Implementing priority CPU scheduling. In this problem, we are using Min Heap as the data structure for implementing priority scheduling. In this problem smaller numbers denote higher priority. The following functions are used in the given code below: struct process { processID, burst time, response
    15+ min read
  • Program for FCFS CPU Scheduling | Set 2 (Processes with different arrival times)
    We have already discussed FCFS Scheduling of processes with same arrival time. In this post, scenarios, when processes have different arrival times, are discussed. Given n processes with their burst times and arrival times, the task is to find the average waiting time and an average turn around time
    15+ min read
  • CPU Scheduling in Operating Systems
    CPU scheduling is a process used by the operating system to decide which task or process gets to use the CPU at a particular time. This is important because a CPU can only handle one task at a time, but there are usually many tasks that need to be processed. The following are different purposes of a
    8 min read
  • Program for Shortest Job First (or SJF) CPU Scheduling | Set 1 (Non- preemptive)
    The shortest job first (SJF) or shortest job next, is a scheduling policy that selects the waiting process with the smallest execution time to execute next. SJN, also known as Shortest Job Next (SJN), can be preemptive or non-preemptive. Characteristics of SJF Scheduling: Shortest Job first has the
    13 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences