Thursday, 10 January 2019

Program on CPU SCHEDULING - PRIORITY ( NON PREEMPTIVE)

/* Program to implement PRIORITY CPU SCHEDULING */
#include<stdio.h>

void main()
{
    int bt[10],p[10],priority[10],st[10],ct[10],tat[10],wt[10],n,i,j,temp;
    float sumtat=0,sumwt=0,avwt,avtat;
    printf("\nPriority Scheduling");
    printf("\nEnter Number of Processes:");
    scanf("%d",&n);
    printf("\nEnter %d Processes Details\n\n",n);
    for(i=0;i<n;i++)
    {
        printf("\nEnter Process %d ID:",i+1);
        scanf("%d",&p[i]);
        printf("\nEnter Process %d Burst Time:",i+1);
        scanf("%d",&bt[i]);
        printf("\nEnter Process %d Priority:",i+1);
        scanf("%d",&priority[i]);
    }

    for(i=0;i<n;i++)
    {
        for(j=0;j<n-1;j++)
        {
            if(priority[j]>priority[j+1])
            {
                //Swapping Processes Based on Priority
                temp=priority[j];
                priority[j]=priority[j + 1];
                priority[j + 1]=temp;
                //Swapping Process ID Accordingly
                temp=p[j];
                p[j]=p[j + 1];
                p[j +1]=temp;
                //Swapping Burst Time
                temp=bt[j];
                bt[j]=bt[j + 1];
                bt[j + 1]=temp;
            }

        }
    }

st[0]=0;
wt[0]=0;
ct[0]=st[0]+bt[0];
for(i=0;i<n;i++)
    {
        st[i+1]=st[i]+bt[i];
        ct[i+1]=st[i+1]+bt[i+1];
    }
   

for(i=0;i<n;i++)
    {
        tat[i]=ct[i] - 0;
        sumtat=sumtat+tat[i];
    }



for(i=0;i<n;i++)
    {
        wt[i]=tat[i]-bt[i];
        sumwt=sumwt+wt[i];
    }


printf("\n\tPNO\tPr\tbt\tst\tct\ttat\twt\n");
    for(i=0;i<n;i++)
    {
        printf("\n\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",p[i],priority[i],bt[i],st[i],ct[i],tat[i],wt[i]);
    }

avtat=sumtat/n;
avwt=sumwt/n;
printf("\n average waiting time is %f\n",avwt);
printf("\n average tutn around time is %f\n",avtat);
}


output:



No comments:

Post a Comment