Sunday, November 25, 2012

CPU scheduling algorithms.--- Priority.

/* Simulate the following cpu scheduling algorithms.
    d. Priority.   */

#include<stdio.h>
#include<conio.h>
void main()
{
    int i,sum=0,bt[20],wt[20],p[20];
    int twt=0,p1[20],n,j,temp,sum1=0,z[20];
    float avgt=0.00;
    clrscr();
    printf("\n Enter no. of Processes ::");
    scanf("%d",&n);
    printf("\n Enter the %d burst times::",n);
    for(i=0;i<n;i++)
    {
        printf("\n\n Enter burst time and priority for process%d::",i+1);
        scanf("%d%d",&bt[i],&p[i]);
        p1[i]=i+1;
    }
    for(i=0;i<n-1;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(p[i]>p[j])
            {
                temp=bt[i];
                bt[i]=bt[j];
                bt[j]=temp;
                temp=p1[i];
                p1[i]=p1[j];
                p1[j]=temp;
            }
        }
    }
    for(i=0;i<n;i++)
    {
        if(i==0)
        {
            wt[i]=0;
            sum=sum+bt[i];
        }
        else
        {
            wt[i]=sum;
            sum=sum+bt[i];
        }
        sum1=sum1+bt[i];
        z[i]=sum1;
    }
    printf("\n\n----------------------------------------------\n");
    printf("\n\tPNo\tbt\tprt\twt\n");
    printf("\n----------------------------------------------\n");
    for(i=0;i<n;i++)
    {
        twt+=wt[i];
        printf("\n\n\tP%d\t%d\t%d\t%d\n",p1[i],bt[i],i+1,wt[i]);
    }
    printf("\n----------------------------------------------\n");
    avgt=(float)twt/(float)n;
    printf("\n\nGannt Chart ::\n\n");
    printf("\t0");
    for(i=0;i<j-1;i++)
        printf("%4d",z[i]);
    printf("\n\n Total waiting time is ::%d",twt);
    printf("\n\n Average waiting time is ::%f",avgt);
    getch();
}

/* Input and Output :-
 Enter no. of Processes ::3
 Enter the 3 burst times::
 Enter burst time and priority for process1::10 2
 Enter burst time and priority for process2::12 3
 Enter burst time and priority for process3::9 1
----------------------------------------------
    PNo     bt      prt     wt
----------------------------------------------
    P3      9       1       0
    P1      10      2       9
    P2      12      3       19
----------------------------------------------
Gannt Chart ::

    0   9  19

 Total waiting time is ::28
 Average waiting time is ::9.333333        */



                                    

No comments:

Post a Comment