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

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: ");
    //Reading each process details
    printf("Assuming Arrival time is 0 for all processes.\n");
        printf("Process number %d \n", i);
        printf("Burst Time:");
    wt[0]=0; //first process does not have to wait.
    //Finding Waiting Time of each process
            wt[i] = wt[i] + bt[j];
    //Finding TurnaroundTime
        tat[i]=wt[i]+bt[i]; //TAT = WT + BT
    //Finding Average Waiting Time 
        avgwt = avgwt + wt[i];
    avgwt = avgwt/n;
    //Finding Average TurnAround Time
        avgtat = avgtat + tat[i];
    avgtat = avgtat/n;

    //Displaying The Table
    printf("The Table is:\n\n");
        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);


No comments:

Post a Comment


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.