Lab Assignment 01 .
Title: FCFS CPU scheduling algorithm using Python
Course code: CSE-325
Course title: Operating System
Submission deadline: 18/2/2024
Submission date: 18/2/2024
Submitted to–
Md. Mafiul Hasan Matin
Lecturer
Department of Computer Science and Engineering
East West University
Submitted by–
Mohammad Akibul Hasan Arman.
Id – 2020-2-60-079.
Sec-05
Session- Spring 2024
Learning Objectives:
Understand the concept of First-Come, First-Served (FCFS) scheduling
algorithm.
Implement FCFS scheduling algorithm using Python programming
language.
Analyze the performance of FCFS scheduling algorithm through
theoretical analysis and practical experimentation.
Theoretical Analysis:
FCFS scheduling primarily relies on the arrival time of processes to
determine their order of execution. Processes are served based solely
on their arrival order, without considering their burst time or any
priority associated with them.
At the core of FCFS scheduling is the principle of fairness. It ensures
fairness by treating all processes equally, giving each process an
opportunity to execute in the order it arrived.
FCFS scheduling can have varying impacts on system performance
metrics such as average waiting time, average turnaround time, and
CPU utilization.
It performs well in scenarios where processes have similar burst
times or when there is no significant variation in arrival times.
Based on the provided arrival times (AT) and burst times (BT) for each
process, let's create the Gantt chart:
P1 P2 P3
0 18 21 24
Process 1 (P1) arrives at time 0 and has a burst time of 18
units. It executes from time 0 to 18.
Process 2 (P2) arrives at time 1 and has a burst time of 3
units. It executes from time 18 to 21.
Process 3 (P3) arrives at time 2 and has a burst time of 3
units. It executes from time 21 to 24.
Process Arrival Burst Waiting Turn Around Completion
Time Time Time Time Time
P1 0 18 0 18 18
P2 1 3 17 20 21
P3 2 3 19 22 24
FCFS Algorithm Implementation
Implementation: The Python code implements the First-Come,
First-Served (FCFS) scheduling algorithm. It sorts processes based on
arrival time and calculates exit time, turnaround time, and waiting time
for each process.
Input and Output: The code requires user input for the number
of processes, arrival times, and burst times. It generates a Gantt chart
and computes performance metrics such as average waiting time,
average turnaround time, and average completion time as output.
The provided Python code encapsulates the First-Come, First-Served (FCFS)
scheduling algorithm. Upon execution, the user is prompted to input the number
of processes, along with their arrival times and burst times. These details are
stored in a list called "processes" as tuples containing process ID, arrival time,
and burst time. The processes are then sorted based on arrival time.
Subsequently, the exit time, turnaround time, and waiting time for each process
are calculated. The Gantt chart is generated to visualize the process timeline,
and the results, including arrival time, burst time, exit time, turnaround time,
and waiting time for each process, are displayed in tabular format. Additionally,
the average waiting time, average turnaround time, and average completion
time are computed and printed.
The code demonstrates the functionality of the FCFS scheduling algorithm,
providing insights into process execution order and associated performance
metrics. It serves as a practical implementation of FCFS, enabling analysis of
scheduling efficiency and resource utilization.
Code explanation: def fcfs_scheduling():
print("FIRST COME FIRST SERVE SCHEDULING")
# The fcfs_scheduling function is a Python function designed to implement the First-
Come, First-Served (FCFS) scheduling algorithm.
n = int(input("Enter number of processes: "))
# It asks the user to input the number of processes n that need to be scheduled
processes = []
# This line creates an empty list named processes which will hold information about
each process.
for i in range(1, n + 1):
arrival_time = int(input(f"Enter arrival time of process P{i}: "))
burst_time = int(input(f"Enter burst time of process P{i}: "))
[Link]((f"P{i}", arrival_time, burst_time))
# Here, it iterates through each process from 1 to ‘ n’. For each process, it prompts
the user to input its arrival time and burst time. Then, it creates a tuple
(process_id, arrival_time, burst_time) representing the process and adds it to the
processes list.
[Link](key=lambda x: x[1])
# It sorts the list of processes based on their arrival time, so that they are in
the order they arrived.
exit_time = [0] * n
turnaround_time = [0] * n
waiting_time = [0] * n
# These lines initialize three lists: exit_time, turnaround_time, and waiting_time,
each filled with zeros, to store corresponding values for each process.
gantt_chart = []
# This creates an empty list named gantt_chart, which will store information for
generating the Gantt chart.
for i in range(n):
if i == 0:
exit_time[i] = processes[i][2]
else:
exit_time[i] = exit_time[i - 1] + processes[i][2]
# This loop calculates the exit time for each process. For the first process ( i ==
0), its exit time is equal to its burst time. For subsequent processes, the exit
time is the sum of the exit time of the previous process and their own burst time.
turnaround_time[i] = exit_time[i] - processes[i][1]
waiting_time[i] = turnaround_time[i] - processes[i][2]
# It calculates the turnaround time and waiting time for each process based on the
exit time, arrival time, and burst time.
#Here, processes[i] refers to the information about the i-th process
stored in the processes list. This information is a tuple containing:
#processes[i][0]: Process ID
#processes[i][1]: Arrival time
#processes[i][2]: Burst time
gantt_chart.append((processes[i][0], exit_time[i]))
# This adds entries to the Gantt chart list, consisting of tuples (process_id,
exit_time).
avg_waiting_time = sum(waiting_time) / n
avg_turnaround_time = sum(turnaround_time) / n
avg_completion_time = sum(exit_time) / n
# It calculates the average waiting time, average turnaround time, and average
completion time.
print("\nGantt Chart:")
print("-" * 40)
for entry in gantt_chart:
print(f"| {entry[0]} ", end="")
print("|")
print("-" * 40)
for entry in gantt_chart:
print(f"| {entry[1]:<2} ", end="")
print(f"|\n{'-' * 40}")
# This part prints the Gantt chart. It iterates through the entries in the gantt_chart
list and prints them in a formatted way to display the process timeline.
print("\nProcess | Arrival | Burst | Exit | Turn Around | Wait |")
for i in range(n):
print(f" {processes[i][0]} | {processes[i][1]} | {processes
[i][2]} | {exit_time[i]} | {turnaround_time[i]} | {waitin
g_time[i]} | ")
#The results, including arrival time, burst time, exit time, turnaround time, and
waiting time for each process, are printed in tabular format.
print("\nAverage Waiting Time: ", avg_waiting_time)
print("Average Turnaround Time: ", avg_turnaround_time)
print("Average Completion Time: ", avg_completion_time)
#The average waiting time, average turnaround time, and average
completion time are printed.
fcfs_scheduling()
# This line calls the fcfs_scheduling function to execute the FCFS scheduling
algorithm.
Result: The values obtained from both the experimental setup and the
Python code were found to be in close agreement, demonstrating consistency
between the empirical measurements and the computational model. The
comparison between the experimental results and the Python code outputs
demonstrates the reliability and accuracy of both the empirical measurements and
the computational model.
Conclusion: The Gantt chart shows that the First-Come, First-Served
(FCFS) scheduling algorithm is simple and fair in process execution, but it has
limitations in efficiency, particularly in the convoy effect. This results in longer wait
times and turnaround times. While batch processing systems may find FCFS
suitable, real-time systems and environments with diverse process
characteristics may find it less ideal due to its lack of prioritization and potential
inefficiencies. Understanding the trade-offs between efficiency and fairness is
crucial when selecting a scheduling algorithm.