C Program to show an example of Shortest Job First Scheduling

 


C Program to show an example of Shortest Job First Scheduling
#include<stdio.h>
void main()
{
        int n,i,j,k,pos[30],temp1,temp2,a[30],e[30],t[30],w[30];
        float m,p=0;
        printf("Enter the number of process:");
        scanf("%d",&n);
        printf("Enter the execution time:");
        for(i=0;i<n;i++)
        {
                scanf("%d",&a[i]);
                pos[i]=i;
        }
        for(i=0;i<n;i++)
        for(j=0;j<n;j++)
        if(a[i]<a[j])
        {
                temp2=a[i];
                a[i]=a[j];
                a[j]=temp2;
                temp1=pos[i];
                pos[i]=pos[j];
                pos[j]=temp1;
        }
        for(i=0;i<n;i++)
        {
                e[i]=a[i];
        }
        printf("After shortest job first scheduling:");
        w[0]=0;
        t[0]=a[0];
        for(i=1;i<n;i++)
        {
                w[i]=t[i-1];
                t[i]=t[i-1]+a[i];
        }
        printf("Process\tBurst Time\tWait Time\tTurn Around\n");
        int tt=0,wt=0;
    for(i=0;i<n;i++)
        {
                printf("\t\t\t\t\t%d\t\t%d\t\t%d\t\t%d\t\t\n",pos[i]+1,e[i],w[i],t[i]);
                tt=tt+t[i];
                wt=wt+w[i];
        }
        printf("Average waiting time=%d\n",wt/n);
        printf("Average turn around time=%d\n",tt/n);
}


Post a Comment

Previous Post Next Post