Write a Program Using C to Implement the Shortest Job First Scheduling Algorithm

Aim:

To Write a Program Using C to Implement the Shortest Job First Scheduling Algorithm.

Algorithm:

  • STEP 1- START
  • STEP 2- Declare the integer bt, p, wt, ta, i, j, n, tot=0, pos and temp
  • STEP 3- Declare to float average waiting time and average turnaround time.
  • STEP 4-Print the number of process in n and Burst time.
  • STEP 5- The bt in for loop ‘i ‘value is 0, i<n and i increment in p[i]+i.
  • STEP 6- pos=i, The bt in for loop ‘j ‘value is i+1, j<n and j increment.
  • STEP 7- if bt[j] less than bt[pos], pos=j and create the temporary file and equal to the bt[i], bt[i] equal to bt[pos], bt[pos]equal to temporary, temporary equal to p[i], so p[i] equal to p[pos] and p[pos] equal their temporary file
  • STEP 8- wt[0] ,the wt in for loop ‘i ‘value is 0, i<n and i increment, becomes wt[i], In for loop ‘j ‘value is i+1, j<n and j increment wt[i] add or equal to bt[j], tot add or equal to wt[i].
  • STEP 9- In float average waiting time equal to total divided be n value, And the value of total is 0.
  • STEP 10- Print the value process burst time waiting time and turnaround time.
  • STEP 11- tat[i] equal to bt[i] add the wt[i], tot add or equal to tat[i],and print the value p[i],bt[i],wt[i] andtat[i].
  • STEP 12- In float average turnaround time equal to total divided be n value, and print the average waiting time and average turn around time.
  • STEP 13- STOP.

Description:

In the following example assume no other jobs arrive during the servicing of all jobs in the ready list. Assume there are 4 jobs.

i             T(Pi)

0              5

1             10

2              8

3             3

Since the service time of P3 is 3, which is the shortest, P3 is scheduled first and then P0 is scheduled next and so on.

Gantt Chart:

P3P0P2P1

0                              3                                8                              16                     26

Turn Around Time:

TRND(P0)=T(P0)+T(P3)=5+3=8

TRND(P1)=T(P1)+ T(P0)+T(P3)+T(P2)=10+5+3+5=26

TRND(P2)=T(P2)+ T(P0)+T(P3)=8+5+3=16

TRND(P3)= T(P3)= 3

Average Turn Around Time:

TRND =(8+26+16+3)/4=13

Waiting Time:

W(P0)=3

W(P1)=16

W(P2)= 8

W(P3)= 0

Average Waiting Time:

W=(3+16+8+0)/4=6.75

Program:

#include<stdio.h>
void main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,tot=0,pos,temp;
float avgwt,avgtat;
printf("Enter number of process:");
scanf("%d",&n);
printf("\nEnter Burst Time:\n");
for(i=0;i<n;i++)
{
printf("p%d:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1;          
}
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(bt[j]<bt[pos])
pos=j;
}
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0;          
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
tot+=wt[i];
}
avgwt=(float)tot/n;      
tot=0;
printf("\nProcess\t    Burst Time \tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];    
tot+=tat[i];
printf("\np%d\t\t  %d\t\t    %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}
avgtat=(float)tot/n;  
printf("\n\nAverage Waiting Time=%f",avgwt);
printf("\nAverage Turnaround Time=%f\n",avgtat);
}

Execution:

Input
4 5 3 9 2

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

Process	   Burst Time     Waiting Time     Turnaround Time
 P4		2		0		2
 P2		3		2		5
 P1		5		5		10
 P3		9		10		19

Average Waiting Time= 4.25
Avg Turn Around Time= 9.00

Result:

 Thus Shortest job first scheduling program was executed Successfully.

Leave a Comment