# Write a Program Using C to Implement the First Come First Serve Scheduling

#### Aim:

To Write a Program Using C to Implement the First Come First Serve Scheduling.

#### Algorithm:

• STEP 1- START
• STEP 2- To declare the n, bt, wt, tat, avwt, i, and j.
• STEP 3- Print the total number of process in n. Enter process Burst Time
• STEP 4- The bt and wt in for loop ‘i ‘value is 0, i<n and i increment.
• STEP 5- Print the Process , Burst time, Waiting time and Turnaround Time
• STEP 6- wt in for loop ‘j ‘value is 0, j<i and j increment.
• STEP 7- tat equal to bt add wt, avwt+=wt[i], avtat+=tat[i], Print the tat, bt and wt.
• STEP 8- avwt/=i, avtat/=I . And print the average waiting time and average turnaround time.
• STEP 9- STOP

#### Description:

Suppose there are 3 processes in the ready list. Further, assume that they had entered into the ready list in the order P0, P1, P2.

#### Gantt (Gantl Chart):

0                    24                    27                   30

#### Turn Around Time:

TRND(P0) =T(P0) =24

TRND(P1) = T(P1)+ TRND(P0) = 3+24 = 27

TRND(P2) =T(P2)+ TRND(P1) = 3+27 = 30

#### Average Turn Around Time:

TRND =(24+27+30)/3=27

#### Waiting Time:

W(P0)=0

W(P1)= TRND(P0)=24 or W(P1)= W(P0)+burst time of P0 = 0+24=24

W(P2)= TRND(P1)=27

#### Average Waiting Time:-

(0+24+27)/3=17

• Consider the following processes arrive for execution at the times indicated.
• Each process will run for the amount of time listed.
• In answering the questions, use nonpreemptive scheduling, and base all decisions on the information you have at the time the decision must be made.

Process         Arrival Time        Burst Time

P1                       0.0                          8

P2                       0.4                          4

P3                       1.0                          1

#### Question:

1. What is the average turnaround time for these processes with the FCFS scheduling algorithm?
2. What is the average turnaround time for these processes with the SJF scheduling algorithm?
3. What is the average turn around time for these processes with the round robin scheduling algorithm?
4. What is the average turn around time for these processes with the priority scheduling algorithm?
5. The SJF algorithm is supposed to improve performance, but notice that we chose to run process P1 at time 0 because we did not know that two shorter processes would arrive soon. Compute what the average turnaround time will be if the CPU is left idle for the first 1 unit and then SJF scheduling is used. Remember that processes P1 and P2 are waiting during this idle time, so their waiting time may increase. This algorithm could be called future-knowledge scheduling.

#### Program:

```#include<stdio.h>
int main()
{
int n,bt,wt,tat,avwt=0,avtat=0,i,j;
printf("Enter total number of processes");
scanf("%d",&n);
printf("\nEnter Process Burst Time\n");
for(i=0;i<n;i++)
{
printf("%d",i+1);
scanf("%d",&bt[i]);
}
wt=0;
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
}
printf("\nProcess\t\tBurst Time\tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
avwt+=wt[i];
avtat+=tat[i];
printf("\n%d\t\t%d\t\t%d\t\t%d",i+1,bt[i],wt[i],tat[i]);
}
avwt/=i;
avtat/=i;
printf("\n\nAverage Waiting Time:%d",avwt);
printf("\nAverage Turnaround Time:%d",avtat);
return 0;
}
```

#### Execution:

```Input
3 4 3 5

Output
Enter Total Number of processes:
Enter Process Burst Time:
Enter P1:
Enter P2:
Enter P3:

Process	   Burst Time     Waiting Time     Turnaround Time
P1		4		0		4
P2		3		4		7
P3		5		7		12

Avgerage Waiting Time:3.66
Avg Turn around Time :7.66```

#### Result:

Thus first come first serve scheduling program was executed Successfully.