Write a c program to implement shortest job first scheduling algorithms non-preemptive



Write a c program to implement shortest job first scheduling algorithms non-preemptive

Algorithm:
1.       Start
2.      Declare the array size p[20], bt[20], wt[20], tat[20], i, k, n, temp; float wtavg, tatavg;
3.      Read the number of processes to be inserted
4.      Read the Burst times of processes
5.      Sort the Burst times in ascending order and process with shortest burst time is first executed.
for(i=0;i<n;i++)
for(k=i+1;k<n;k++)
if(bt[i]>bt[k])
{   
temp=bt[i];
bt[i]=bt[k];
bt[k]=temp;
temp=p[i];
p[i]=p[k];
p[k]=temp;
}
6.      waiting time for first process will be zero
wt[0] = wtavg = 0;
tat[0] = tatavg = bt[0];
7.      calculate waiting time and turnaround time
for(i=1;i<n;i++)
{
wt[i] = wt[i-1] +bt[i-1];
tat[i] = tat[i-1] +bt[i];
wtavg = wtavg + wt[i];
tatavg = tatavg + tat[i];
}
8.      Calculate the average waiting time and average turnaround time.
awt=wtavg/n;
atat=tatavg/n;
9.      Display the values

10.  Stop
source code:

#include<stdio.h>
 void main()
{
    int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
    float avg_wt,avg_tat;
    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;           //contains process number
    }

    //sorting burst time in ascending order using selection sort
    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;            //waiting time for first process will be zero

    //calculate waiting time
    for(i=1;i<n;i++)
    {
        wt[i]=0;
        for(j=0;j<i;j++)
            wt[i]+=bt[j];

        total+=wt[i];
    }

    avg_wt=(float)total/n;      //average waiting time
    total=0;

    printf("\nProcess\t    Burst Time    \tWaiting Time\tTurnaround Time");
    for(i=0;i<n;i++)
    {
        tat[i]=bt[i]+wt[i];     //calculate turnaround time
        total+=tat[i];
        printf("\np%d\t\t  %d\t\t    %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
    }

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

output:

Comments

Popular posts from this blog

Write C programs to simulate the Paging techniques of memory management

Write C programs to simulate the Two level directory File organization technique

Write C programs to simulate the Hierarchical directory File organization technique