First Come First Serve Scheduling In C Programming

Last updated on Mar 29,2022 112.8K Views


First Come First Serve is a scheduling algorithm used by the CPU to schedule jobs. It is a Non-Preemptive Algorithm. Priority is given according to which they are requested by the processor. Let’s understand the concept in the following order:

 

First Come First Serve SchedulingThe process that requests the services of CPU first, get the CPU first. This is the philosophy used by the first come first serve algorithm.

TERMS In First Come First Serve

  • Completion time: Time when the execution is completed.
  • Turn Around Time: The sum of the burst time and waiting time gives the turn-around time
  • Waiting time: The time the processes need to wait for it to get the CPU and start execution is called waiting time.

Important Points

  • It is non-preemptive
  • Average waiting time is not optimal
  • Cannot utilize resources in parallel

 

Example Code

#include<stdio.h>

 int main()

{
    int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;
    printf("Enter total number of processes(maximum 20):");
    scanf("%d",&n);

    printf("nEnter Process Burst Timen");
    for(i=0;i<n;i++)
    {
        printf("P[%d]:",i+1);
        scanf("%d",&bt[i]);
    }

    wt[0]=0;   

    for(i=1;i<n;i++)
    {
        wt[i]=0;
        for(j=0;j<i;j++)
            wt[i]+=bt[j];
    }

    printf("nProcessttBurst TimetWaiting TimetTurnaround Time");

    for(i=0;i<n;i++)
    {
        tat[i]=bt[i]+wt[i];
        avwt+=wt[i];
        avtat+=tat[i];
        printf("nP[%d]tt%dtt%dtt%d",i+1,bt[i],wt[i],tat[i]);
    }

    avwt/=i;
    avtat/=i;
    printf("nnAverage Waiting Time:%d",avwt);
    printf("nAverage Turnaround Time:%d",avtat);

    return 0;
}
OUTPUT:

 

EXPLANATION

In the above code, the demonstration of the first come first serve scheduling algorithm is shown. The user is asked to enter the number of processes. On entering the number of processes, we have to enter the burst times for each of the processes.

The waiting time is calculated first. First, the waiting time of the first process is zero.

for(i=1;i<n;i++)
  {
      wt[i]=0;
      for(j=0;j<i;j++)
          wt[i]+=bt[j];
  }

Calculation of the waiting time is done by adding the burst time of the previous process. Consider the previous process had a burst time of 10, then the waiting time of second will be 10. Similarly, for the third process, the waiting time will the sum of burst times of first and second processes.

for(i=0;i<n;i++)
    {
        tat[i]=bt[i]+wt[i];
        avwt+=wt[i];
        avtat+=tat[i];
        printf("nP[%d]tt%dtt%dtt%d",i+1,bt[i],wt[i],tat[i]);
    }

The next part we calculate the turn around time. The turn around time for each process is calculated by adding the burst time and the waiting time.

Last, the average turn around time and the average waiting time is calculated.

avwt/=i;
avtat/=i;

i gives the total number of processes. We divide the sum of all the waiting times and turn around times to get the average. This is how the first come first serve algorithm works.

With this, we come to an end of this First Come First Serve Scheduling in C Programming.

I hope you found this informative and helpful, stay tuned for more tutorials on similar topics. You may also check out our training program to get in-depth knowledge on jQuery along with its various applications, you can enroll here for live online training with 24/7 support and lifetime access. Implement the above code with different strings and modifications. Now, we have a good understanding of all key concepts related to the pointer.

Got a question for us? Mention them in the comments section of this blog and we will get back to you.

Comments
0 Comments

Join the discussion

Browse Categories

Subscribe to our Newsletter, and get personalized recommendations.