C Program to Simulate FCFS CPU Scheduling Algorithm (With Proper Comments to Understand the Logic)



Given n processes with their burst times, the task is to find the average waiting time and the average turnaround time using the FCFS scheduling algorithm.

FCFS CPU scheduling is the simplest CPU scheduling algorithm. It simply executes the processes in the order that they arrive.
In this, the process that comes first will be executed first and the next process starts only after the previous gets fully executed.
Here we are considering that arrival time for all processes is 0.

Important equations to know before doing this program.

  1. Completion Time: Time at which process completes its execution.
  2. Turn Around Time: Time Difference between completion time and arrival time. Turn Around Time = Completion Time – Arrival Time
  3. Waiting Time(W.T): Time Difference between turn around time and burst time.
    Waiting Time = Turn Around Time – Burst Time


Open in OnlineGDB

#include<stdio.h>
void main()
{
    int n, at[20], bt[20], wt[20], tat[20],i,j;
    float avgwt=0, avgtat=0;
    //Reading number of processes
    printf("Enter the number of processes: ");
    scanf("%d",&n);
    //Reading each process details
    printf("Assuming Arrival time is 0 for all processes.\n");
    for(i=0;i<n;i++)
    {
        printf("Process number %d \n", i);
        at[i]=0;
        printf("Burst Time:");
        scanf("%d",&bt[i]);
        printf("\n");
    }
    wt[0]=0; //first process does not have to wait.
    //Finding Waiting Time of each process
    for(i=1;i<n;i++)
    {
        wt[i]=0;
        for(j=0;j<i;j++)
        {
            wt[i] = wt[i] + bt[j];
        }
    }
    //Finding TurnaroundTime
    for(i=0;i<n;i++)
    {
        tat[i]=wt[i]+bt[i]; //TAT = WT + BT
    }
   
    //Finding Average Waiting Time 
    for(i=0;i<n;i++)
    {
        avgwt = avgwt + wt[i];
    }
    avgwt = avgwt/n;
    
    //Finding Average TurnAround Time
    for(i=0;i<n;i++)
    {
        avgtat = avgtat + tat[i];
    }
    avgtat = avgtat/n;

    //Displaying The Table
    printf("The Table is:\n\n");
    printf("PID\tAT\tBT\tWT\tTAT\n");
    for(i=0;i<n;i++)
    {
        printf("%d\t%d\t%d\t%d\t%d\n", i, at[i], bt[i], wt[i],tat[i]);
    }
    
    //Displaying Average TAT and WT 
    printf("\nAverage Waiting Time = %f \n",avgwt);
    printf("Average TurnAround Time = %f \n", avgtat);
}


Output




No comments:

Post a Comment

Pages

Nanogalaxy is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for website owners to earn advertising fees by advertising and linking to amazon.com and any other website that may be affiliated with Amazon Service LLC Associates Program.