Algorithm to compute waiting time for fcfs scheduling in python

When it comes to computing the waiting time for FCFS (First-Come-First-Serve) scheduling in Python, there are several approaches you can take. In this article, we will explore three different solutions to this problem.

Solution 1: Using a List

One way to compute the waiting time for FCFS scheduling is by using a list to store the arrival times of the processes. Here’s a sample code that demonstrates this approach:


# Sample input
arrival_times = [0, 2, 4, 6, 8]

# Initialize waiting time
waiting_time = 0

# Compute waiting time for each process
for i in range(1, len(arrival_times)):
    waiting_time += arrival_times[i-1]

# Print the waiting time
print("Waiting time:", waiting_time)

This solution works by iterating over the list of arrival times and adding the previous arrival time to the waiting time. The final waiting time is then printed.

Solution 2: Using a Dictionary

Another approach is to use a dictionary to store the arrival times of the processes. Here’s a sample code that demonstrates this approach:


# Sample input
arrival_times = {'P1': 0, 'P2': 2, 'P3': 4, 'P4': 6, 'P5': 8}

# Initialize waiting time
waiting_time = 0

# Compute waiting time for each process
for process in arrival_times:
    waiting_time += arrival_times[process]

# Print the waiting time
print("Waiting time:", waiting_time)

In this solution, the arrival times are stored as key-value pairs in a dictionary. The waiting time is computed by iterating over the dictionary and adding the arrival time of each process to the waiting time.

Solution 3: Using a Class

A more object-oriented approach is to define a class that represents a process and its arrival time. Here’s a sample code that demonstrates this approach:


class Process:
    def __init__(self, name, arrival_time):
        self.name = name
        self.arrival_time = arrival_time

# Sample input
processes = [Process('P1', 0), Process('P2', 2), Process('P3', 4), Process('P4', 6), Process('P5', 8)]

# Initialize waiting time
waiting_time = 0

# Compute waiting time for each process
for i in range(1, len(processes)):
    waiting_time += processes[i-1].arrival_time

# Print the waiting time
print("Waiting time:", waiting_time)

In this solution, a Process class is defined with attributes for the process name and arrival time. The arrival times are stored as instances of the Process class in a list. The waiting time is computed by iterating over the list and adding the arrival time of each process to the waiting time.

After exploring these three solutions, it is clear that Solution 3, using a class, is the most flexible and scalable option. It allows for better organization and encapsulation of the data, making the code more maintainable in the long run. Therefore, Solution 3 is the recommended approach for computing the waiting time for FCFS scheduling in Python.

Rate this post

4 Responses

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents