FCFS - First Come First Serve Code in C, Algorithm, Advantage, Disadvantage

No comments

Tutorial on:

FCFS - First Come First Serve Code and Algorithm in C.



FCFS - First Come First Serve

First Come First Serve or FCFS is based on First Come First Serve Basis.

Algorithm of FCFS:

  • Which process comes first to CPU, the process will serve then.
  • It is mainly a non preemptive scheduling algorithm. That means, no stop of a process when that's running.
  • FCFS implements the First In First Out (FIFO) algorithm. That means which come first to the queue,  that will out first from the queue.

C Code of FCFS / First Come First Serve C implementation:


#include<stdio.h>

int main()
{
    int n, burst_time[100], waiting_time[100], turn_around_time[100];
    int average_wating_time=0,average_turn_around_time=0;

    printf("Total Processes : ");
    scanf("%d",&n);

    int i, j;
    printf("\nEnter Burst Time : \n");
    for(i=0; i<n; i++)
    {
        printf("P%d = ",i+1);
        scanf("%d",&burst_time[i]);
    }

    waiting_time[0]=0;
    for(i=1; i<n; i++)
    {
        waiting_time[i]=0;
        for(j=0; j<i; j++){
            waiting_time[i] = waiting_time[i] + burst_time[j];
        }

    }


    printf("\n--------------------------------------------------------------");
    printf("\nProcess\t\tBurst Time\tWaiting Time\tTurn around Time");
    printf("\n--------------------------------------------------------------\n");

    for(i=0; i<n; i++)
    {
        turn_around_time[i] = burst_time[i] + waiting_time[i];
        average_wating_time += waiting_time[i];
        average_turn_around_time += turn_around_time[i];
        printf("\nProcess = %d\t\t%d\t\t%d\t\t%d", i+1, burst_time[i], waiting_time[i], turn_around_time[i]);
    }

    average_wating_time = average_wating_time/n;
    average_turn_around_time = average_turn_around_time / n;


    printf("\n\nAverage Waiting Time:%d", average_wating_time);
    printf("\nAverage Turnaround Time:%d\n\n", average_turn_around_time);

    return 0;
}


Output of FCFS:

FCFS - First Come First Serve Code in C, Algorithm, Advantage, Disadvantage

Explanation of First Come First Serve Algorithm (FCFS)

1) First take the burst time for processes in an array called burst_time[], in these lines,
    for(i=0; i<n; i++)
    {
        printf("P%d = ",i+1);
        scanf("%d",&burst_time[i]);
    }

2) Then check waiting time. For our first process which waiting time = 0 and for what.
    waiting_time[0]=0;

3) Then in the waiting_time[] put the waiting times using waiting_time = waiting_time + burst_time for that process. In these lines,
    for(i=1; i<n; i++)
    {
        waiting_time[i]=0;
        for(j=0; j<i; j++){
            waiting_time[i] = waiting_time[i] + burst_time[j];
        }

    }

4) Then Find the turn around time, average waiting time in these lines,
    for(i=0; i<n; i++)
    {
        turn_around_time[i] = burst_time[i] + waiting_time[i];
        average_wating_time += waiting_time[i];
        average_turn_around_time += turn_around_time[i];
        printf("\nProcess = %d\t\t%d\t\t%d\t\t%d", i+1, burst_time[i], waiting_time[i], turn_around_time[i]);
    }


5) Find Average waiting time and average turn around time by dividing the n, in these lines-
    average_wating_time = average_wating_time/n;
    average_turn_around_time = average_turn_around_time / n;

Advantages of FCFS or First Come First Serve Algorithm:


  • FCFS is suitable for batch system, basically.
  • FCFS is easier to implement.

Disadvantages of FCFS or First Come First Serve Algorithm:


  • Waiting time can be large if short request wait for the long process in execution.
  • FCFS is not suitable for time sharing system where it is important that each user should get the CPU for equal amount of time interval.


Note:Having any problems in this First Come First Serve algorithm or code, please comment here and don't hesitate.

Tags:
FCFS - First Come First Serve Code in C, Algorithm, Advantage, Disadvantage, FCFS Algorithm, FCFS code in C, FCFS code, FCFS algorithm.

No comments :

Post a Comment