Program to implement SJF (Shortest Job first) algorithm in C | Wave the world

Program to implement SJF (Shortest Job first) algorithm in C


/*
    SJF (Shortest Job First) Scheduling Algorithm
    Created By: Pirate
*/

#include<stdio.h>
#include<conio.h>
void main(){
    int input[500],process[500];
    int wt[500],tat[500],total=0;
    int i,j,n,pos,temp;
    float avg_wt,avg_tat;
    printf("*** SJF Process Scheduling Algorithm ***\n");
    printf("Enter the number of Process: ");
    scanf("%d",&n);

    printf("\nEnter Burst Time:\n\n");
    for(i=0;i<n;i++){
        printf("P%d: ",i+1);
        scanf("%d",&input[i]);
        process[i]=i+1;
    }

    for(i=0;i<n;i++){
        pos=i;
        for(j=i+1;j<n;j++){
            if(input[j]<input[pos])
                pos=j;
        }
        temp=input[i];
        input[i]=input[pos];
        input[pos]=temp;

        temp=process[i];
        process[i]=process[pos];
        process[pos]=temp;
    }

    wt[0]=0;

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

    avg_wt=(float)total/n;
    total=0;

    printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");
    for(i=0;i<n;i++){
        tat[i]=input[i]+wt[i];
        total+=tat[i];
        printf("\nP%d\t\t%d\t\t%d\t\t%d",process[i],input[i],wt[i],tat[i]);
    }

    avg_tat=(float)total/n;
    printf("\n\nAverage Waiting Time    =   %f",avg_wt);
    printf("\nAverage Turnaround Time =   %f\n",avg_tat);

}

Output




0 comments:

Post a Comment

 

Pro

About