Tuesday, December 4, 2018

C code review implement Round Robin Scehdular program

This C code shows how to implement Round Robin Scehdular program . And code is helpful while user is in between any algorithm manipulation task.

code:

//Tested OK in Code::Blocks IDE 17.12 & Cygwin+GCC tools.
//Round Robin Scehdular program in c

#include<stdio.h>

struct rr
{
    int pid;
    int sertime,cputime,waittime,rcputime,tat;
} a[10];

int n,q,k,prev,next,j,i,l,wtime;
static float avgtat,avgwaittime;

int main(void)
{
    printf("\n===Round Robin SCHEDULING MECHANISM===");
    printf("\nENTER THE NUMBER OF JOBS IN THE READY QUEUE: \n\t");
    scanf("%d",&n);
    for(i=0; i<n; i++)
    {
        printf("\nENTER THE PROCESS ID AND SERVICE TIME OF THE JOB: \n\t");
        scanf("%d %d",&a[i].pid,&a[i].sertime);
        a[i].rcputime=a[i].sertime;
    }
    printf("\nENTER THE TIME QUANTUM: \n\t");
    scanf("%d",&q);
    a[0].cputime=0;
    if(a[0].sertime<q)
    {
        a[0].tat=a[0].sertime;
        a[0].rcputime=a[0].sertime-q;
    }
    else
    {
        a[0].tat=q;
        a[0].rcputime=a[0].sertime-q;
    }
    a[0].waittime=0;
    prev=0;
    next=1;
    while(1)
    {
        k=0;
        for(j=next; j<n; j++)
        {
            if(a[j].rcputime>0)
            {
                next=j;
                a[next].cputime=a[prev].tat;
                if(a[next].rcputime<q)
                    a[next].tat=a[next].rcputime+a[next].cputime;
                else
                    a[next].tat=q+a[next].cputime;
                wtime=a[next].sertime-a[next].rcputime;
                a[next].waittime=a[next].cputime-wtime;
                a[next].rcputime=a[next].rcputime-q;
                prev=next;
            }
            else
                k++;
        }
        if(k==n)
            break;
        next=0;
    }
    for(i=0; i<n; i++)
    {
        avgtat+=(float)a[i].tat/n;
        avgwaittime+=(float)a[i].waittime/n;
    }
    printf("\nPROCESSID \tSERVICETIME \tCPUACCESSTIME \tTURNAROUNDTIME \tWAITINGTIME\n");
    for(i=0; i<n; i++)
    {

        printf(" %d \t\t%d \t\t%d \t\t%d \t\t%d \n",a[i].pid,a[i].sertime,a[i].cputime,a[i].tat,a[i].waittime);
    }
    printf("\nTHE AVERAGE TURN AROUND TIME IS :   %f",avgtat);
    printf("\nTHE AVERAGE WAITING TIME IS     :   %f \n",avgwaittime);
}
//end of program

//end of program

To see such C code reviews refer blog archieve at right side or click CCode lable for till posted posts list.

...till next post, bye-bye and take care.

No comments:

Post a Comment