Sunday, September 29, 2013

Tagged Under: ,

FCFS Algorithm Implementation in C

Share

First Come First Serve (FCFS) Implementation Using C 

First Come First Serve algorithm is used in many areas for solving different kind of problems. According to FCFS the process arriving first will get processed first. i.e the processes get processed/executed according to their arrival time. In a group of processes the process having the lowest arrival time will get processed before any other process having higher processing time, Until the processing of a process is ongoing other processes has to wait in the waiting queue.








Ex: In above diagram P1 has the minimum arrival time, hence got processed first while its execution time is not lowest.Same process continues till all processes in waiting queue get processed.

Implementation of FCFS in C

#include<stdio.h>
#include<conio.h>
main()
{
    int n,i,j,sum=0;
    int arrv[10],executet[10],start[10]; //arrv : arrival time, executet : execution time
    int finish[10],wait[10],turn[10]; //wait: waiting time, turn: turnaround time
    float avgturn=0.0,avgwait=0.0; //avgturn: average turnaround time, avgwait: avg waiting time
    start[0]=0;
    clrscr();
    printf("Enter the number of processes:"); //No of process
    scanf("%d",&n);
    for(i=0;i<n;i++) //Enter Process details
 {
        printf("Enter the arriwal and process time of %d process:",i+1);
        scanf("%d%d",&arrv[i],&executet[i]);
 }
 for(i=0;i<n;i++)
 {
        sum=0;
        for(j=0;j<i;j++)
        sum=sum+executet[j];
        start[i]=sum;
 }
    for(i=0;i<n;i++)
    {
        finish[i]=executet[i]+start[i];
        wait[i]=start[i];
        turn[i]=executet[i]+wait[i];
    }
    for(i=0;i<n;i++)
    {
        avgwait+=wait[i];
        avgturn+=turn[i];
    }
    avgwait/=n;
    avgturn/=n;
    printf("\narraival service Start Finish Wait Turn\n");
    for(i=0;i<n;i++)
    printf("%d\t%d\t%d\t%d\t%d\t%d\n",arrv[i],executet[i],start[i],
    finish[i],wait[i],turn[i]);
    printf("\nAverage waiting time=%f",avgwait);
    printf("\nAverage turn around time=%f",avgturn);
    getch();
}