FCFS Scheduling of processes with different arrival time


Also Read

  • FCFS Scheduling of processes with same arrival time
  • Preemptive and Non Preemptive Scheduling
  • CPU Scheduling in Operating Systems

  • Given n processes with their burst times and arrival times, the task is to find average waiting time and average turn around time using FCFS scheduling algorithm.

    As the name suggests First Come and First Server is based on queue. The process that comes first will be executed first and then it will move to the next process. It is non-preemptive and average waiting time is optional.It also cannot utilize resources in parallel.

    Service Time: The amount of time after which the process start execution. It is summation of burst time of previous processes (Processes that came before)

    Algorithm :

    1. Input the processes along with their burst time(bt) and arrival time(at)
    2. Find waiting time for all other processes i.e. for a given process i: wt[i] = (bt[0] + bt[1] +...... bt[i-1]) - at[i]
    3. Now find turn around time = waiting_time + burst_time for all processes
    4. Average waiting time = total_waiting_time / no_of_processes
    5. Average turn around time = total_turn_around_time / no_of_processes

    Code - Python:
        # Function to find the waiting time
        # for all processes
        def findWaitingTime(processes, n, bt, wt, at):
            service_time = [0] * n
            service_time[0] = 0
            wt[0] = 0
        
            # calculating waiting time
            for i in range(1, n):
        
                # Add burst time of previous processes
                service_time[i] = (service_time[i - 1] +
                                   bt[i - 1])
        
                # Find waiting time for current
                # process = sum - at[i]
                wt[i] = service_time[i] - at[i]
        
                # If waiting time for a process is in
                # negative that means it is already
                # in the ready queue before CPU becomes
                # idle so its waiting time is 0
                if (wt[i] < 0):
                    wt[i] = 0
        
        
        # Function to calculate turn around time
        def findTurnAroundTime(processes, n, bt, wt, tat):
            # Calculating turnaround time by
            # adding bt[i] + wt[i]
            for i in range(n):
                tat[i] = bt[i] + wt[i]
        
        
        # Function to calculate average waiting
        # and turn-around times.
        def findavgTime(processes, n, bt, at):
            wt = [0] * n
            tat = [0] * n
        
            # Function to find waiting time
            # of all processes
            findWaitingTime(processes, n, bt, wt, at)
        
            # Function to find turn around time for
            # all processes
            findTurnAroundTime(processes, n, bt, wt, tat)
        
            # Display processes along with all details
            print("Processes Burst Time Arrival Time	 Waiting", "Time Turn-Around Time Completion Time \n")
            total_wt = 0
            total_tat = 0
            for i in range(n):
                total_wt = total_wt + wt[i]
                total_tat = total_tat + tat[i]
                compl_time = tat[i] + at[i]
                print(" ", i + 1, "\t\t", bt[i], "\t\t", at[i], "\t\t", wt[i], "\t\t ", tat[i], "\t\t ", compl_time)
        
            print("Average waiting time = %.5f " % (total_wt / n))
            print("\nAverage turn around time = ", total_tat / n)
        
        
        if __name__ == "__main__":
            # Process id's
            processes = [1, 2, 3]
            n = 3
        
            # Burst time of all processes
            burst_time = [5, 9, 6]
        
            # Arrival time of all processes
            arrival_time = [0, 3, 6]
        
            findavgTime(processes, n, burst_time,
                        arrival_time)