C Program for FCFS CPU Scheduling

Subject: OS (Operating Systems)

Contributed By: Nunugoppula Ajay

Created At: March 6, 2025

Question:


Write a C Program for FCFS (First come First serve ) CPU Scheduling algorithm

Explanation Video:

Custom Image

Explanation:

The operating systems' FCFS scheduling C program

The FCFS scheduling program operates on the assumption that all processes arrive at time ‘0’ and are carried out in the order that we defined.

(NOTE:  We can calculate the FCFS based on arrival time at ‘0’ for all processes, and for different arrival times for each process. Here in this article, we are going to consider all process arrivals at time ‘0’ .)

🔹 Turnaround Time (TAT) → Total time from beginning to end (WT + Burst Time); 

🔹 Waiting Time (WT) → Time a process waits before execution.

How does it operate?

1️⃣ The initial procedure (WT = 0) begins instantly.

2️⃣ Every step after that awaits the completion of the one before it.

3️⃣ The waiting time is determined by adding together the burst times of previous operations.

4️⃣ WT + Burst Time is the turnaround time.

5️⃣ Lastly, we calculate averages and present the findings.

🔥 Very easy! Only the orderly execution of the process is required; there are no arrival times.

Source Code:
#include <stdio.h>

// Function to implement First Come First Serve (FCFS) scheduling
void FCFS(int bt[], int n) {
    int wt[n], tat[n], total_wt = 0, total_tat = 0;

    // First process has zero waiting time
    wt[0] = 0;

    // Calculate waiting time for each process
    for (int i = 1; i < n; i++) {
        wt[i] = wt[i - 1] + bt[i - 1];
        total_wt += wt[i];
    }

    // Calculate turnaround time for each process
    for (int i = 0; i < n; i++) {
        tat[i] = wt[i] + bt[i];
        total_tat += tat[i];
    }

    // Calculate average waiting and turnaround times
    float avg_wt = (float)total_wt / n;
    float avg_tat = (float)total_tat / n;

    // Display process details
    printf("\nProcess\tBurst Time\tWaiting Time\tTurnaround Time\n");
    printf("-------------------------------------------------\n");
    for (int i = 0; i < n; i++) {
        printf("%d\t%d\t\t%d\t\t%d\n", i + 1, bt[i], wt[i], tat[i]);
    }

    printf("\nAverage Waiting Time: %.2f\n", avg_wt);
    printf("Average Turnaround Time: %.2f\n", avg_tat);
}

int main() {
    int n;

    // Input number of processes
    printf("Enter the number of processes: ");
    scanf("%d", &n);

    int bt[n];

    // Input burst time for each process
    printf("Enter Burst Time for each process:\n");
    for (int i = 0; i < n; i++) {
        printf("Process %d Burst Time: ", i + 1);
        scanf("%d", &bt[i]);
    }

    // Call FCFS scheduling function
    FCFS(bt, n);

    return 0;
}
Input:
Enter the number of processes: 3
Enter Burst Time for each process:
Process 1 Burst Time: 5
Process 2 Burst Time: 3
Process 3 Burst Time: 2
Output:
Process   Burst Time   Waiting Time   Turnaround Time
1         5           0              5
2         3           5              8
3         2           8              10

Average Waiting Time: 4.33
Average Turnaround Time: 7.67
Share this Article & Support Us:
Status
printf('Loading...');