Skip to content

Instantly share code, notes, and snippets.

@raghavmri
Created August 28, 2025 03:10
Show Gist options
  • Select an option

  • Save raghavmri/dde82764ea3421dd7e1dae97193647ad to your computer and use it in GitHub Desktop.

Select an option

Save raghavmri/dde82764ea3421dd7e1dae97193647ad to your computer and use it in GitHub Desktop.
# FCFS Scheduling Algorithm Implementation in Python
def find_waiting_time(processes, n, burst_time, waiting_time):
waiting_time[0] = 0
for i in range(1, n):
waiting_time[i] = burst_time[i-1] + waiting_time[i-1]
def find_turn_around_time(processes, n, burst_time, waiting_time, turnaround_time):
for i in range(n):
turnaround_time[i] = burst_time[i] + waiting_time[i]
def find_average_time(processes, n, burst_time):
waiting_time = [0] * n
turnaround_time = [0] * n
find_waiting_time(processes, n, burst_time, waiting_time)
find_turn_around_time(processes, n, burst_time, waiting_time, turnaround_time)
print("Processes\tBurst Time\tWaiting Time\tTurnaround Time")
total_waiting_time = 0
total_turnaround_time = 0
for i in range(n):
total_waiting_time += waiting_time[i]
total_turnaround_time += turnaround_time[i]
print(f"P{i+1}\t\t{burst_time[i]}\t\t{waiting_time[i]}\t\t{turnaround_time[i]}")
avg_waiting_time = total_waiting_time / n
avg_turnaround_time = total_turnaround_time / n
print(f"\nAverage Waiting Time: {avg_waiting_time:.2f}")
print(f"Average Turnaround Time: {avg_turnaround_time:.2f}")
if __name__ == "__main__":
processes = [1, 2, 3]
n = len(processes)
burst_time = [10, 5, 8]
print("First Come First Serve (FCFS) Scheduling Simulation\n")
find_average_time(processes, n, burst_time)
# Shortest Job First (SJF) Scheduling Algorithm in Python
def find_waiting_time(n, burst_time, waiting_time):
"""
Calculates the waiting time for each process in the sorted order.
The waiting time of the first process is 0. For subsequent processes,
it's the sum of the burst times of all previously executed processes.
Args:
n (int): The number of processes.
burst_time (list): A list of burst times, already sorted.
waiting_time (list): A list to store the calculated waiting times.
"""
# The waiting time for the first process in the sorted list is 0.
waiting_time[0] = 0
# Calculate waiting time for the rest of the processes.
for i in range(1, n):
waiting_time[i] = burst_time[i-1] + waiting_time[i-1]
def find_turn_around_time(n, burst_time, waiting_time, turnaround_time):
"""
Calculates the turnaround time for each process.
Turnaround Time (TAT) = Burst Time + Waiting Time.
Args:
n (int): The number of processes.
burst_time (list): A list of burst times.
waiting_time (list): A list of waiting times.
turnaround_time (list): A list to store the calculated turnaround times.
"""
# Calculate turnaround time for all processes.
for i in range(n):
turnaround_time[i] = burst_time[i] + waiting_time[i]
def find_average_time(processes, n, burst_time):
"""
Sorts processes and then calculates and displays the average
waiting and turnaround times.
Args:
processes (list): The list of process IDs.
n (int): The total number of processes.
burst_time (list): A list of burst times for each process.
"""
# Create a list of tuples to sort processes by burst time.
# Each tuple contains (burst_time, process_id).
sorted_processes = sorted(zip(burst_time, processes))
# Extract the sorted burst times and process IDs.
sorted_burst_time = [item[0] for item in sorted_processes]
sorted_p_ids = [item[1] for item in sorted_processes]
waiting_time = [0] * n
turnaround_time = [0] * n
# Calculate waiting time of all processes.
find_waiting_time(n, sorted_burst_time, waiting_time)
# Calculate turnaround time for all processes.
find_turn_around_time(n, sorted_burst_time, waiting_time, turnaround_time)
print("Processes\tBurst Time\tWaiting Time\tTurnaround Time")
total_waiting_time = 0
total_turnaround_time = 0
# Display the sorted process details and sum up total times.
for i in range(n):
total_waiting_time += waiting_time[i]
total_turnaround_time += turnaround_time[i]
print(f"{sorted_p_ids[i]}\t\t{sorted_burst_time[i]}\t\t{waiting_time[i]}\t\t{turnaround_time[i]}")
# Calculate and display the average times.
avg_waiting_time = total_waiting_time / n
avg_turnaround_time = total_turnaround_time / n
print(f"\nAverage Waiting Time: {avg_waiting_time:.2f}")
print(f"Average Turnaround Time: {avg_turnaround_time:.2f}")
# Main execution block
if __name__ == "__main__":
# Define the example processes and their burst times
processes = ["P1", "P2", "P3"]
n = len(processes)
burst_time = [10, 5, 8]
print("Shortest Job First (SJF) Scheduling Simulation\n")
find_average_time(processes, n, burst_time)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment