Showing posts with label Round Robin in C. Show all posts
Showing posts with label Round Robin in C. Show all posts

Tuesday, January 27, 2015

Round Robin Algorithm Implementation in C program

/*
 * Round_Robin_Alg.c
 *
 *  Created on: 13.01.2015
 *      Author: karthikeyan dharmalingam */

# include "main_header.h"

//Declaration part
int nRound_Robin_Algorithm_fun(void);

// Definition part
int nRound_Robin_Algorithm_fun()
{

    printf("\nInside Round robin algorithm \n");

    //Variale declaration
    int nQuantumTime;     // To ge the quantum or slice number from user
    int nProcess,nRemainingProcess;     // To ge the number of process
    int nArrivalTime[255], nBurstTime[255], nRemainingTime[255]; //To get the arrival, burst,and remaing time of process.
    int nLoop, nTime, nFlagComplete;

    printf("\nEnter the number of process \n");
    scanf("%d",&nProcess); //To get the user input for number of process
    //To update remaining process count to complete all process
    nRemainingProcess = nProcess;

    printf("Enter the Quantum/Slice size 1 <--> 255\n");
    scanf("%d",&nQuantumTime); // To get the slice value from user

    //To get the individual timing information on each process.
    for (nLoop = 0; nLoop < nProcess; nLoop++ )
    {
        printf("Enter the arrival and Burst timing for process: P%d: ",nLoop+1);
        scanf("%d",&nArrivalTime[nLoop]);
        scanf("%d",&nBurstTime[nLoop]);
        nRemainingTime[nLoop] = nBurstTime[nLoop];
    }
    printf("\nSuccessfully input received from user\n");
    //Round robin Agorithm to schedule all the process.
    for (nLoop = 0, nTime = 0; nRemainingProcess != 0;)
    {
        //Check for remaining time greater than or equals to slice.
        if(nRemainingTime[nLoop] <= nQuantumTime && nRemainingTime[nLoop] > 0 )
        {


            nTime = nTime + nRemainingTime[nLoop];
            nRemainingTime[nLoop] = 0;
            nFlagComplete = 1; //Set the flag to notice the completion
            printf("\nInside  IF \n");

        }
        //Check for remaining time lesser than slice.
        else if(nRemainingTime[nLoop] > 0)
        {
            nTime = nTime + nQuantumTime;
            nRemainingTime[nLoop] = nRemainingTime[nLoop] - nQuantumTime;
            printf("\nInside ELSE IF \n");
        }
        //process compeleted status
        if(nRemainingTime[nLoop] == 0 && nFlagComplete == 1)
        {
            nRemainingProcess--; //Reduce number of process by one.
            printf("P[%d]\t|\t%d\t|\t%d\n",nLoop+1,nTime-nArrivalTime[nLoop],nTime-nArrivalTime[nLoop]-nBurstTime[nLoop]);
            //Remove flag for uncompleted process
            nFlagComplete = 0;
        }

        if(nLoop == nProcess - 1)
        {
            nLoop  = 0;
            printf("\nInside I \n");
        }
        else if(nArrivalTime[nLoop] <= nTime)
        {
            nLoop++;
            printf("\nInside II \n");

        }else
        {
            printf("\nInside III \n");
            nLoop = 0;
        }



    }



    return 0;
}