C Program for FCFS Scheduling

We are given with the n number of processes i.e. P1, P2, P3, ..., Pn and their corresponding burst times. The task is to find the average waiting time and average turnaround time using FCFS CPU Scheduling algorithm.

What is Waiting Time and Turnaround Time?

  • Turnaround Time is the time interval between the submission of a process and its completion.

    Turnaround Time = completion of a process − submission of a process

  • Waiting Time is the difference between turnaround time and burst time

    Waiting Time = turnaround time − burst time

What is FCFS Scheduling?

First Come, First Served (FCFS) also known as First In, First Out (FIFO) is the CPU scheduling algorithm in which the CPU is allocated to the processes in the order they are queued in the ready queue.

FCFS follows non-preemptive scheduling which means once the CPU is allocated to a process it does not leave the CPU until the process will not get terminated or may get halted due to some I/O interrupt.

Syntax

/* Function prototypes for FCFS scheduling */
int waitingtime(int proc[], int n, int burst_time[], int wait_time[]);
int turnaroundtime(int proc[], int n, int burst_time[], int wait_time[], int tat[]);
int avgtime(int proc[], int n, int burst_time[]);

Example

Let's say, there are four processes arriving in the sequence as P2, P3, P1 with their corresponding execution time as shown in the table below. Also, taking their arrival time to be 0 −

Process Order of arrival Execution time in msec
P1 3 15
P2 1 3
P3 2 3

Gantt chart showing the waiting time of processes P1, P2 and P3 in the system −

Gantt Chart: P2 P3 P1 0 3 6 21 Waiting Time: P2 = 0, P3 = 3, P1 = 6 Average = (0 + 3 + 6) / 3 = 3 msec

As shown above, the waiting time of process P2 is 0, the waiting time of process P3 is 3, and the waiting time of process P1 is 6. Average waiting time = (0 + 3 + 6) / 3 = 3 msec.

Complete Implementation

#include <stdio.h>

/* Function to find the waiting time for all processes */
int waitingtime(int proc[], int n, int burst_time[], int wait_time[]) {
    /* waiting time for first process is 0 */
    wait_time[0] = 0;
    
    /* calculating waiting time */
    for (int i = 1; i < n; i++)
        wait_time[i] = burst_time[i-1] + wait_time[i-1];
    
    return 0;
}

/* Function to calculate turn around time */
int turnaroundtime(int proc[], int n, int burst_time[], int wait_time[], int tat[]) {
    /* calculating turnaround time by adding burst_time[i] + wait_time[i] */
    for (int i = 0; i < n; i++)
        tat[i] = burst_time[i] + wait_time[i];
    
    return 0;
}

/* Function to calculate average time */
int avgtime(int proc[], int n, int burst_time[]) {
    int wait_time[n], tat[n], total_wt = 0, total_tat = 0;
    
    /* Function to find waiting time of all processes */
    waitingtime(proc, n, burst_time, wait_time);
    
    /* Function to find turn around time for all processes */
    turnaroundtime(proc, n, burst_time, wait_time, tat);
    
    /* Display processes along with all details */
    printf("Processes  Burst   Waiting  Turn around <br>");
    
    /* Calculate total waiting time and total turn around time */
    for (int i = 0; i < n; i++) {
        total_wt = total_wt + wait_time[i];
        total_tat = total_tat + tat[i];
        printf(" %d\t   %d\t\t %d \t%d<br>", i+1, burst_time[i], wait_time[i], tat[i]);
    }
    
    printf("Average waiting time = %.6f<br>", (float)total_wt / (float)n);
    printf("Average turn around time = %.6f<br>", (float)total_tat / (float)n);
    
    return 0;
}

/* main function */
int main() {
    /* process id's */
    int proc[] = {1, 2, 3};
    int n = sizeof proc / sizeof proc[0];
    
    /* Burst time of all processes */
    int burst_time[] = {5, 8, 12};
    
    avgtime(proc, n, burst_time);
    
    return 0;
}

Output

Processes  Burst   Waiting  Turn around 
 1	   5		 0 	5
 2	   8		 5 	13
 3	   12		 13 	25
Average waiting time = 6.000000
Average turn around time = 14.333333

How FCFS Works

  • First process in the queue gets CPU first (P1 with burst time 5)
  • Second process waits for first to complete (P2 waits 5 units)
  • Third process waits for both previous processes to complete (P3 waits 13 units)
  • Total waiting time and turnaround time are calculated for performance analysis

Conclusion

FCFS is the simplest CPU scheduling algorithm that processes tasks in arrival order. While easy to implement, it may suffer from convoy effect when short processes wait behind long ones, leading to higher average waiting times.

Updated on: 2026-03-15T12:29:34+05:30

56K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements