0% found this document useful (0 votes)
68 views2 pages

Fcfs

The document contains a Python implementation of the First-Come, First-Served (FCFS) scheduling algorithm for process management. It includes functions to calculate waiting time, turnaround time, and average times based on user input for process IDs, arrival times, and burst times. The main function prompts the user for input and displays the scheduling results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views2 pages

Fcfs

The document contains a Python implementation of the First-Come, First-Served (FCFS) scheduling algorithm for process management. It includes functions to calculate waiting time, turnaround time, and average times based on user input for process IDs, arrival times, and burst times. The main function prompts the user for input and displays the scheduling results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

def FCFS(processes, arrivalTime, burstTime):

n = len(processes)
waitingTime = [0] * n
startTime = [0] * n # Time at which process starts execution

# First process starts at its arrival time


startTime[0] = arrivalTime[0]
waitingTime[0] = 0 # First process has no waiting time

for i in range(1, n):


# Calculate the service time of the current process
startTime[i] = max(startTime[i - 1] + burstTime[i - 1], arrivalTime[i])
# Calculate waiting time
waitingTime[i] = startTime[i] - arrivalTime[i]

# If waiting time is negative, set it to 0


waitingTime[i] = max(waitingTime[i], 0)

return waitingTime

# Function to calculate turnaround time


def calculateTurnaroundTime(burstTime, waitingTime):
n = len(burstTime)
turnaroundTime = [0] * n
for i in range(n):
turnaroundTime[i] = burstTime[i] + waitingTime[i]
return turnaroundTime

# Function to calculate average times


def calculateAverageTimes(processes, arrivalTime, burstTime):

processes=sorted(zip(processes,arrivalTime,burstTime),key=lambda x:x[1])
process_ids=[p[0]for p in processes]
arrivalTime=[p[1] for p in processes]
burstTime=[p[2] for p in processes]
waitingTime = FCFS(process_ids, arrivalTime, burstTime)
turnaroundTime = calculateTurnaroundTime(burstTime, waitingTime)

avgWaitingTime = sum(waitingTime) / len(process_ids)


avgTurnaroundTime = sum(turnaroundTime) / len(process_ids)

print("\nProcess\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time")


for i in range(len(processes)):
print(f"{process_ids[i]}\t\t{arrivalTime[i]}\t\t{burstTime[i]}\t\
t{waitingTime[i]}\t\t{turnaroundTime[i]}")

print(f"\nAverage Waiting Time: {avgWaitingTime:.2f}")


print(f"Average Turnaround Time: {avgTurnaroundTime:.2f}")

def acceptDisplay():
# Accept number of processes
n = int(input("Enter the number of processes: "))

# Accept process IDs


processes = []
for i in range(n):
processes.append(input(f"Enter Process ID for process {i+1}: "))

# Accept arrival times


arrivalTime = []
for i in range(n):
arrivalTime.append(int(input(f"Enter Arrival Time for process
{processes[i]}: ")))

# Accept burst times


burstTime = []
for i in range(n):
burstTime.append(int(input(f"Enter Burst Time for process {processes[i]}:
")))

print("\nFCFS Scheduling Algorithm with User Input:")


calculateAverageTimes(processes, arrivalTime, burstTime)

# Main function
if __name__ == "__main__":
acceptDisplay()

You might also like