FCFS Program in python
import operator
# Process class to store process details
class Process:
def __init__(self, process_id, arrival_time, burst_time):
self.process_id = process_id
self.arrival_time = arrival_time
self.burst_time = burst_time
def __str__(self):
return f"Process ID: {self.process_id}, Arrival Time: {self.arrival_time}, Burst Time: {self.burst_time}"
# Function to calculate the waiting time of each process
def find_waiting_time(processes, n):
# Sort processes based on their arrival time
processes.sort(key=operator.attrgetter('arrival_time'))
# Create a list to store the waiting time of each process
waiting_time = [0] * n
# Initialize variables for current time, completed processes and remaining processes
current_time = processes[0].arrival_time
completed = 0
remaining_processes = processes[:]
# Loop until all processes are completed
while completed < n:
# Sort the remaining processes based on their burst time
remaining_processes.sort(key=operator.attrgetter('burst_time'))
# Get the process with shortest burst time
shortest_process = remaining_processes.pop(0)
# Calculate waiting time of the shortest process
waiting_time[shortest_process.process_id - 1] = current_time - shortest_process.arrival_time
# Update current time and completed processes
current_time += shortest_process.burst_time
completed += 1
return waiting_time
# Function to calculate the average waiting time of all processes
def find_average_waiting_time(processes, n):
waiting_time = find_waiting_time(processes, n)
# Calculate the total waiting time of all processes
total_waiting_time = sum(waiting_time)
# Calculate and return the average waiting time
return total_waiting_time / n
# Sample input data
processes = [Process(1, 0, 6), Process(2, 1, 8), Process(3, 2, 7), Process(4, 3, 3)]
# Calculate and print the average waiting time
avg_waiting_time = find_average_waiting_time(processes, len(processes))
print(f"Average Waiting Time: {avg_waiting_time}")