Sunday, November 25, 2012

CPU scheduling algorithms. --- Round Robin

/* Simulate the following cpu scheduling algorithms.
    A. Round Robin    */

#include<stdio.h>
#include<conio.h>
void main()
{
    int bt[30],wt[30],temp[10],p[20],i,t;
    int n,x=0,sum=0,twt=0,z[30],j=0;
    float avgt=0.00;
    clrscr();
    printf("\n Enter no. of Processes ::");
    scanf("%d",&n);
    printf("\n Enter time slice ::");
    scanf("%d",&t);
    printf("\n Enter the %d burst times::",n);
    for(i=0;i<n;i++)
    {
        printf("\n\n Enter time for process%d::",i+1);
        scanf("%d",&bt[i]);
        p[i]=0;
        temp[i]=bt[i];
        sum=sum+bt[i];
    }
    while(sum!=x)
    {
        for(i=0;i<n;i++)
        {
            if(bt[i]!=0)
            {
                if(bt[i]>t)
                {
                    bt[i]=bt[i]-t;
                    x=x+t;
                    z[j]=x;
                    j++;
                    p[i]++;
                }
                else
                {
                    wt[i]=x-(p[i]*t);
                    x=x+bt[i];
                    z[j]=x;
                    j++;
                    bt[i]=0;
                }
            }
        }
    }
    printf("\n\n----------------------------------------------\n");
    printf("\n\tPNo\tbt\twt\n");
    printf("\n----------------------------------------------\n");
    for(i=0;i<n;i++)
    {
        twt+=wt[i];
        printf("\n\n\tP%d\t%d\t%d\n",i+1,temp[i],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 time slice ::2
 Enter the 3 burst times::
 Enter time for process1::24
 Enter time for process2::3
 Enter time for process3::3
----------------------------------------------
    PNo     bt      wt
----------------------------------------------
    P1      24      6
    P2      3       6
    P3      3       7
----------------------------------------------
Gannt Chart ::

    0   2   4   6   8   9  10  12  14  16  18  20  22  24  26  28

 Total waiting time is   ::19
 Average waiting time is ::6.333333        */


                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               

No comments:

Post a Comment