Tuesday, June 28, 2011

IMPLEMENT First Come First Come scheduling algoritm.

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

#define MAX 5

void get_data(int *a, int *b);
void fcfs(int *a,int *b,int *t,int *w);
void calc(int *tt, int *tw,int *a,int *b,int *t,int *w);
void show(int *tt, int *tw,int *a,int *b,int *t,int *w);

int main()
{
    int arr[MAX];
    int brs[MAX];
    int trn[MAX];
    int wat[MAX];
    int total_turn=0;
    int total_wait=0;

    get_data(arr,brs);
    fcfs(arr,brs,trn,wat);
    calc(&total_turn,&total_wait,arr,brs,trn,wat);
    show(&total_turn,&total_wait,arr,brs,trn,wat);

    printf("\n\n");
    system("pause");
    return 0;
}

void get_data(int *a, int *b)
{
    int i;

    for(i=0;i<MAX;i++)
    {
        printf("\nENTER ARRIVAL TIME OF P[%d] : ",i);
        scanf("%d",a+i);
        printf("ENTER BURST TIME OF P[%d] : ",i);
        scanf("%d",b+i);
    }
}

void fcfs(int *a,int *b,int *t,int *w)
{
    int response=0;
    int i;
    int execution_time=0;

    for(i=0;i<MAX;i++)
    {
        w[i] = response - a[i];        //wait is response - arrival
        execution_time += b[i];        //add current burst to total execution, this gives finish time for this process
        response = execution_time;    //this is response time for next process
        t[i] = execution_time - a[i];    //turnaround is finish - arrival;
    }
}

void calc(int *tt, int *tw,int *a,int *b,int *t,int *w)
{
    int i;
   
    for(i=0;i<MAX;i++)
    {
        *tt += t[i];
        *tw += w[i];
    }
}

void show(int *tt, int *tw,int *a,int *b,int *t,int *w)
{
    int i;

    printf("\nPRC ARRV BRST WAIT TURN");

    for(i=0;i<MAX;i++)
    {
        printf("\n%3d %4d %4d %4d %4d",i,a[i],b[i],w[i],t[i]);
    }

    printf("\nTOTAL TURNAROUND TIME : %4d",*tt);
    printf("\nTOTAL WAITING    TIME : %4d",*tw);
}

No comments:

Post a Comment