13 Program to demonstrate First Come First Serve(FCFS) CPU Scheduling Algorithm

Program:

    
#include<stdio.h>

void main()
{
  
    int n;
    printf("\n Enter Number of Process:");
    scanf("%d",&n);

    // Array to Store 
    // 0 - Arrival Time
    // 1 - Burst Time
    // 2 - TAT (Turn Around Time)
    // 3 - WT  (Waiting Time)
    // 4 - Proc essNo
    //  arr = [[A,B,TAT,WT]]
    
    float arr[n][5];
    
    for(int i=0;i<n;i++)
    {
        printf("\n Enter Arrival Time and Burst Time for Process P%d:",i+1);
        scanf("%f%f",&arr[i][0],&arr[i][1]);
        arr[i][4] = i+1;
    }       
    
    printf("\n\n Displaying the Values:");
    printf("\n ProcessNo \t ArrivalTime \t BurstTime");
    for(int i=0;i<n;i++)
    {
        printf("\n P%0.0f \t\t %0.2f \t\t %0.2f",arr[i][4],arr[i][0],arr[i][1]);
    }   

    //Sort According to Arrival Time
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n-1;j++)
        {
            if(arr[j][0] > arr[j+1][0])
            {
                int at, bt,pn;// pn = process number
                at = arr[j][0];
                bt = arr[j][1];
                pn = arr[j][4];

                arr[j][0] = arr[j+1][0];
                arr[j][1] = arr[j+1][1];
                arr[j][4] = arr[j+1][4];

                arr[j+1][0] = at;
                arr[j+1][1] = bt;
                arr[j+1][4] = pn;
            }           
        }
    }

    printf("\n\n After Scheduling Process According to FCFS:");
    printf("\n P \t AT \t BT \t TAT \t WT");
    //TAT = Exit Time - Arrival Time
    //WT = TAT - BT
    
    float total_tat = 0,total_tat_cal = 0,total_wt = 0,cpu_idle=0; 
    
    // Assuming Process Starts from 0
    if(arr[0][0] != 0)
    {
        total_tat = arr[0][0];
    }

    for(int i=0;i<n;i++)
    {    
        total_tat += arr[i][1];
       
        arr[i][2] = total_tat - arr[i][0]; // TAT
        total_tat_cal += arr[i][2];
        arr[i][3] = arr[i][2] - arr[i][1]; // WT
        
        total_wt +=arr[i][3]; 

        // If next Process has not arrived
        if(arr[i+1][0] > total_tat && i<n-1)
        {
            cpu_idle += arr[i+1][0] - total_tat;  
            total_tat =  arr[i+1][0];
        }

        printf("\n P%0.0f \t %0.2f \t %0.2f \t %0.2f \t %0.2f \n",arr[i][4],arr[i][0],arr[i][1],arr[i][2],arr[i][3]);
    }

    printf("\n\n Average TurnAround Time: %0.2f",total_tat_cal/n);
    printf("\n Average Waiting Time: %0.2f",total_wt/n);
    printf("\n CPU IDLE Time: %0.2f",cpu_idle);

    printf("\n\n");
 
    return;
}
    

Output:

FCFS


Previous
Next Post »