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;
}

Tuesday, January 13, 2015

Reading system value through system function

# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <time.h>

int main()
{
        //Variable declaration
        FILE *fp;
        char hc1,hc2,mc1,mc2;
        int hi1,hi2,mi1,mi2,hour,minute;
        //Reading system value through system function 
        system("echo %time% >time.txt");
        fp=fopen("time.txt","r");
        // Sanity Check
        if(fp==NULL)
           exit(1) ;
        //Assigning individual character information into char varialbels
        hc1=fgetc(fp);
        hc2=fgetc(fp);
        fgetc(fp); // to avoid : symbol from integer calculation
        mc1=fgetc(fp);
        mc2=fgetc(fp);
        fclose(fp);
        remove("time.txt");
       
        hi1=hc1;
        hi2=hc2;
        mi1=mc1;
        mi2=mc2;
        // Char to Integer conversion
        hi1-=48;
        hi2-=48;
        mi1-=48;
        mi2-=48;
        // adding characters for double digit
        hour=hi1*10+hi2;
        minute=mi1*10+mi2;
        //Final output
        printf("Current time is %d:%d\n",hour,minute);


        return 0;
}

Output:
Current time is 11:40

Friday, January 9, 2015

Macros in conditonal operation and data mismatch issue

/*
 * Debugger.c
 *
 *  Created on: 09.01.2015
 *      Author: karthik
 */


# include <stdio.h>
# include <stdlib.h>
# define CEILby2(X) ((X+1)/2)

int main()
{
    int nCycle,nCount=0;
    printf("Understanding of Macros and TypeCasting\n");
    printf("Enter the Cycle in even number for a scenario\n");
    scanf("%d",&nCycle);

    while(nCount <CEILby2(nCycle))
    {
        nCount++;
    }
    printf("Number of cycle executed: %d", nCount);

    return 0;
}

**************************************
Output:
Understanding of Macros and TypeCasting
Enter the Cycle in even number for a scenario
5
 Number of cycle executed:3 ------ expected only 2.


Note: Integer is compared with floating(Real numbers)