First Come First Serve Scheduling in Python [FCFS]

Feature Img FCFS

What is First Come First Serve Scheduling? Hey learners! Today we are going to understand the theoretical concepts and code implementation of a very important topic that comes under the operating system known as the first come first serve CPU Scheduling.

Before jumping to the code implementation, let us first understand what first come first serve means.


Introduction to First Come First Serve

First Come First Serve (FCFS) is the easiest and simplest CPU scheduling algorithm in the operating system that automatically executes processes in order of their arrival.

In this type of algorithm, processes which request the CPU first get the CPU for their complete execution first. This method is poor in performance, and the general wait time is quite high.

Let’s look at some real-life examples:

  1. People waiting in a queue to buy amusement part tickets
  2. People waiting for the bus at the bus stop

Now in CPU Scheduling, we are required to compute some time values which are listed below:

  1. Exit Time: When did the process leave the CPU after getting completly executed.
  2. Turn Around Time: The difference between the Arrival and Exit time of the processes.
  3. Waiting Time: The difference between the burst/execution time and the turn around time of the processes.

In addition to all this, we can also compute the average waiting time of the processes.


An Illustration of First Come First Serve

Let us consider a case where we have 4 processes with different arrival and execution times. The data is displayed in the table below:

Process IDArrival TimeBurst/Execution Time
P104
P215
P325
P433
Arrival Time and Burst time of 4 different processes

Now we need to calculate different time values such as exit time, turn around time, and waiting time. You can have a look at the time chart mentioned below and analyze and compute various time values.

Featured Img FCFS Timechart
FCFS Timechart

Here the exit times for the process are 4,9,14 and 17 respectively. The turnaround times for the processes are 4,8,12 and 14 respectively.

And similarly, the waiting time of the processes are 0,3,7,11 respectively. We finally have to compute the average waiting time which comes out to be 5.25.

Now let’s move to the code implementation of the FCFS process.


Implementing FCFS in Python

print("FIRST COME FIRST SERVE SCHEDULLING")
n= int(input("Enter number of processes : "))
d = dict()

for i in range(n):
    key = "P"+str(i+1)
    a = int(input("Enter arrival time of process"+str(i+1)+": "))
    b = int(input("Enter burst time of process"+str(i+1)+": "))
    l = []
    l.append(a)
    l.append(b)
    d[key] = l

d = sorted(d.items(), key=lambda item: item[1][0])

ET = []
for i in range(len(d)):
    # first process
    if(i==0):
        ET.append(d[i][1][1])

    # get prevET + newBT
    else:
        ET.append(ET[i-1] + d[i][1][1])

TAT = []
for i in range(len(d)):
    TAT.append(ET[i] - d[i][1][0])

WT = []
for i in range(len(d)):
    WT.append(TAT[i] - d[i][1][1])

avg_WT = 0
for i in WT:
    avg_WT +=i
avg_WT = (avg_WT/n)

print("Process | Arrival | Burst | Exit | Turn Around | Wait |")
for i in range(n):
      print("   ",d[i][0],"   |   ",d[i][1][0]," |    ",d[i][1][1]," |    ",ET[i],"  |    ",TAT[i],"  |   ",WT[i],"   |  ")
print("Average Waiting Time: ",avg_WT)

Sample Output

FIRST COME FIRST SERVE SCHEDULLING

Enter number of processes : 4
Enter arrival time of process1: 1
Enter burst time of process1: 5
Enter arrival time of process2: 0
Enter burst time of process2: 4
Enter arrival time of process3: 3
Enter burst time of process3: 3
Enter arrival time of process4: 2
Enter burst time of process4: 5

Process | Arrival | Burst | Exit | Turn Around | Wait |
    P2    |    0  |     4  |     4   |     4   |    0    |  
    P1    |    1  |     5  |     9   |     8   |    3    |  
    P4    |    2  |     5  |     14   |     12   |    7    |  
    P3    |    3  |     3  |     17   |     14   |    11    |  
Average Waiting Time:  5.25

Advantages and Disadvantages of FCFS

Let’s look at some of the advantages

Advantages of First Come First Serve

  1. Easy to program
  2. The simplest form of a CPU scheduling algorithm

Disdvantages of First Come First serve

  1. Average Waiting Time is high
  2. Not an ideal technique for time-sharing systems
  3. FCFS is not very efficient

Conclusion

I hope you are now clear with what FCFS CPU Scheduling is and how one can implement the same with the help of the python programming language.

Hope you liked the tutorial! Thank you for reading! Happy Learning! 😇