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

FCFS

The document outlines the implementation of the First-Come, First-Served (FCFS) CPU scheduling algorithm in Python, detailing the steps to accept process information and calculate scheduling metrics. It includes a structured approach for user input, sorting processes, and displaying results such as Gantt charts and average waiting times. A sample input and output are provided to illustrate the functionality of the program.

Uploaded by

mdsalmanddn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views2 pages

FCFS

The document outlines the implementation of the First-Come, First-Served (FCFS) CPU scheduling algorithm in Python, detailing the steps to accept process information and calculate scheduling metrics. It includes a structured approach for user input, sorting processes, and displaying results such as Gantt charts and average waiting times. A sample input and output are provided to illustrate the functionality of the program.

Uploaded by

mdsalmanddn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Lab Title: FCFS Scheduling Algorithm in Python

Objective: Implement the First-Come, First-Served (FCFS) CPU scheduling algorithm in Python.
The program should prompt the user for arrival time and burst time only. Process IDs are
automatically assigned.

Problem Statement:

1. Accepts the number of processes.


2. For each process, takes:
o Arrival Time
o Burst Time
o (Automatically assigns process IDs as P1, P2, etc.)
3. Sorts the processes by arrival time.
4. Calculates:
o Start and end time for each process (Gantt Chart)
o Waiting time for each process
o Average waiting time
o Average turnaround time
5. Displays results in a structured format.

Python Code Structure:

# Get number of processes


n = int(input("Enter the number of processes: "))
processes = []

# Input arrival and burst times


for i in range(n):
arrival = int(input(f"Enter arrival time for process P{i+1}: "))
burst = int(input(f"Enter burst time for process P{i+1}: "))
processes.append({"pid": f"P{i+1}", "arrival": arrival, "burst": burst})

# Sort by arrival time


processes.sort(key=lambda x: x["arrival"])

current_time = 0
waiting_times = []
turnaround_times = []
gantt_chart = []

# FCFS Scheduling logic


for process in processes:
start_time = max(current_time, process["arrival"])
waiting_time = start_time - process["arrival"]
completion_time = start_time + process["burst"]
turnaround_time = completion_time - process["arrival"]

waiting_times.append(waiting_time)
turnaround_times.append(turnaround_time)
gantt_chart.append((process["pid"], start_time, completion_time))
current_time = completion_time
# Gantt Chart
print("\nGantt Chart:")
for entry in gantt_chart:
print(f"| {entry[0]} ", end="")
print("|")
for entry in gantt_chart:
print(f"{entry[1]:<4}", end=" ")
print(f"{gantt_chart[-1][2]}")

# Process Table
print("\nProcess\tArrival\tBurst\tTAT\tWaiting")
for i, p in enumerate(processes):

print(f"{p['pid']}\t{p['arrival']}\t{p['burst']}\t{turnaround_times[i]}\t{waiting
_times[i]}")

# Average Waiting
avg_waiting = sum(waiting_times) / n

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

Sample Input:

• Total number of processes (integer)


• For each process:
o Arrival Time (integer)
o Burst Time (integer)

Sample Output:

Enter the number of processes: 3


Enter arrival time for process P1: 0
Enter burst time for process P1: 5
Enter arrival time for process P2: 2
Enter burst time for process P2: 3
Enter arrival time for process P3: 4
Enter burst time for process P3: 1
Gantt Chart:
| P1 | P2 | P3 |
0 5 8 9

Process Arrival Burst TAT Waiting


P1 0 4 4 0
P2 1 3 6 3
P3 2 2 7 5

Average Waiting Time: 2.33

You might also like