Apr 18, 2012

C program to implement Round Robin CPU scheduling algorithm.

Write a C program to implement Round Robin CPU scheduling algorithm.
In Round Robin scheduling algorithm, a small time slice or quantum is defined, all the tasks are kept in queue. The CPU scheduler picks the first task from the queue ,sets a timer to interrupt after one quantum, and dispatches the process. If the process is still running at the end of the quantum, the CPU is preempted and the process is added to the tail of the queue. If the process finishes before the end of the quantum, the process itself releases the CPU voluntarily. In either case, the CPU scheduler assigns the CPU to the next process in the ready queue. Every time a process is granted the CPU, a context switch occurs, which adds overhead to the process execution time. Round Robin scheduler is mainly used for the time sharing systems. Read more about C Programming Language . and read the C Programming Language (2nd Edition) by K and R.

/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/


#include<stdio.h>
#include<conio.h>
struct process
{
    char na[20];
    int at,bt,ft,tat,rem;
    float ntat;
}Q[5],temp;
int rr[20],q,x,k;
main()
{
    int f,r,n,i,j,tt=0,qt,t,flag,wt=0;
    float awt=0,antat=0,atat=0;
    clrscr();
    printf("Enter the no. of jobs:");
    scanf("%d",&n);
    for(r=0;r<n;r++)
    {
        printf("Enter process name,arrival time and burst time:\n");
        scanf("%s%d%d",Q[r].na,&Q[r].at,&Q[r].bt);
    }
    printf("Enter quantum:\n");
    scanf("%d",&qt);
    for(i=0;i<n;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(Q[i].at>Q[j].at)
            {
                temp=Q[i];
                Q[i]=Q[j];
                Q[j]=temp;
            }
        }
    }
    for(i=0;i<n;i++)
    {
        Q[i].rem=Q[i].bt;
        Q[i].ft=0;
    }
    tt=0;
    q=0;
    rr[q]=0;
    do
    {
        for(j=0;j<n;j++)
        if(tt>=Q[j].at)
        {
            x=0;
            for(k=0;k<=q;k++)
            if(rr[k]==j)
            x++;
            if(x==0)
            {
                q++;
                rr[q]=j;
            }
        }
        if(q==0)
        i=0;
        if(Q[i].rem==0)
        i++;
        if(i>q)
        i=(i-1)%q;
        if(i<=q)
        {
            if(Q[i].rem>0)
            {
                if(Q[i].rem<qt)
                {
                    tt+=Q[i].rem;
                    Q[i].rem=0;
                }
                else
                {
                    tt+=qt;
                    Q[i].rem-=qt;
                }
                Q[i].ft=tt;
            }
            i++;
        }
        flag=0;
        for(j=0;j<n;j++)
        if(Q[j].rem>0)
        flag++;
    }while(flag!=0);
    clrscr();
    printf("\n\n\t\tROUND ROBIN ALGORITHM");
    printf("\n***************************");
    printf("\nprocesses Arrival time burst time finish time tat wt ntat");
    for(f=0;f<n;f++)
    {
        wt=Q[f].ft-Q[f].bt-Q[f].at;
        Q[f].tat=Q[f].ft-Q[f].at;
        Q[f].ntat=(float)Q[f].tat/Q[f].bt;
        antat+=Q[f].ntat;
        atat+=Q[f].tat;
        awt+=wt;
        printf("\n\t%s\t%d\t\t%d\t%d\t%d\t%d %f",
        Q[f].na,Q[f].at,Q[f].bt,Q[f].ft,Q[f].tat,wt,Q[f].ntat);
    }
    antat/=n;
    atat/=n;
    awt/=n;
    printf("\nAverage tat is %f",atat);
    printf("\nAverage normalised tat is %f",antat);
    printf("\n average waiting time is %f",awt);
getch();    }
 
  
Read more Similar C Programs
C Basic

Search Algorithms.
You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!


To browse more C Programs visit this link