Aug 22, 2024

CPU Scheduling Algorithms

A Python program to simulate and display results for various CPU scheduling algorithms.

Source Code

class Process:
    def __init__(self, pid, burst_time, arrival_time, priority):
        self.pid = pid
        self.burst_time = burst_time
        self.arrival_time = arrival_time
        self.priority = priority
        self.remaining_time = burst_time
        self.completion_time = 0
        self.waiting_time = 0
        self.turnaround_time = 0

def first_come_first_serve(processes):
    processes.sort(key=lambda x: x.arrival_time)
    current_time = 0
    for process in processes:
        current_time = max(current_time, process.arrival_time)
        process.completion_time = current_time + process.burst_time
        process.turnaround_time = process.completion_time - process.arrival_time
        process.waiting_time = process.turnaround_time - process.burst_time
        current_time += process.burst_time

def shortest_job_first(processes):
    current_time = 0
    completed = 0
    n = len(processes)
    while completed < n:
        ready_queue = [p for p in processes if p.arrival_time <= current_time and p.completion_time == 0]
        if ready_queue:
            ready_queue.sort(key=lambda x: x.burst_time)
            process = ready_queue[0]
            current_time = max(current_time, process.arrival_time)
            process.completion_time = current_time + process.burst_time
            process.turnaround_time = process.completion_time - process.arrival_time
            process.waiting_time = process.turnaround_time - process.burst_time
            current_time += process.burst_time
            completed += 1
        else:
            current_time += 1

def shortest_remaining_time_first(processes):
    current_time = 0
    completed = 0
    n = len(processes)
    while completed < n:
        ready_queue = [p for p in processes if p.arrival_time <= current_time and p.remaining_time > 0]
        if ready_queue:
            ready_queue.sort(key=lambda x: x.remaining_time)
            process = ready_queue[0]
            process.remaining_time -= 1
            current_time += 1
            if process.remaining_time == 0:
                process.completion_time = current_time
                process.turnaround_time = process.completion_time - process.arrival_time
                process.waiting_time = process.turnaround_time - process.burst_time
                completed += 1
        else:
            current_time += 1

def round_robin(processes, quantum):
    current_time = 0
    completed = 0
    n = len(processes)
    ready_queue = []
    while completed < n:
        for p in processes:
            if p.arrival_time <= current_time and p.completion_time == 0 and p not in ready_queue:
                ready_queue.append(p)
        if ready_queue:
            process = ready_queue.pop(0)
            time_slice = min(quantum, process.remaining_time)
            process.remaining_time -= time_slice
            current_time += time_slice
            if process.remaining_time == 0:
                process.completion_time = current_time
                process.turnaround_time = process.completion_time - process.arrival_time
                process.waiting_time = process.turnaround_time - process.burst_time
                completed += 1
            else:
                ready_queue.append(process)
        else:
            current_time += 1

def priority_scheduling(processes):
    current_time = 0
    completed = 0
    n = len(processes)
    while completed < n:
        ready_queue = [p for p in processes if p.arrival_time <= current_time and p.completion_time == 0]
        if ready_queue:
            ready_queue.sort(key=lambda x: x.priority)
            process = ready_queue[0]
            current_time = max(current_time, process.arrival_time)
            process.completion_time = current_time + process.burst_time
            process.turnaround_time = process.completion_time - process.arrival_time
            process.waiting_time = process.turnaround_time - process.burst_time
            current_time += process.burst_time
            completed += 1
        else:
            current_time += 1

def print_results(processes, algorithm):
    print(f"\n{algorithm} Scheduling Results:")
    print("PID\tArrival\tBurst\tCompletion\tWaiting\tTurnaround")
    for p in processes:
        print(f"{p.pid}\t{p.arrival_time}\t{p.burst_time}\t{p.completion_time}\t\t{p.waiting_time}\t{p.turnaround_time}")

def get_processes():
    n = int(input("Enter the number of processes: "))
    processes = []
    for i in range(n):
        arrival_time = int(input(f"Process {i+1} Arrival Time: "))
        burst_time = int(input(f"Process {i+1} Burst Time: "))
        priority = int(input(f"Process {i+1} Priority: "))
        processes.append(Process(i + 1, burst_time, arrival_time, priority))
    return processes

def menu():
    while True:
        print("\nCPU Scheduling Algorithms:")
        print("1. First Come First Serve")
        print("2. Shortest Job First")
        print("3. Shortest Remaining Time First")
        print("4. Round Robin")
        print("5. Priority Scheduling")
        print("6. Exit")
        choice = int(input("Enter your choice: "))

        if choice == 6:
            print("Exiting...")
            break

        processes = get_processes()

        if choice == 1:
            first_come_first_serve(processes)
            print_results(processes, "First Come First Serve")
        elif choice == 2:
            shortest_job_first(processes)
            print_results(processes, "Shortest Job First")
        elif choice == 3:
            shortest_remaining_time_first(processes)
            print_results(processes, "Shortest Remaining Time First")
        elif choice == 4:
            quantum = int(input("Enter the time quantum: "))
            round_robin(processes, quantum)
            print_results(processes, "Round Robin")
        elif choice == 5:
            priority_scheduling(processes)
            print_results(processes, "Priority Scheduling")
        else:
            print("Invalid choice. Please try again.")

if __name__ == "__main__":
    menu()

Output

CPU Scheduling Algorithms:
1. First Come First Serve
2. Shortest Job First
3. Shortest Remaining Time First
4. Round Robin
5. Priority Scheduling
6. Exit
Enter your choice: 1

Enter the number of processes: 3
Process 1 Arrival Time: 0
Process 1 Burst Time: 5
Process 1 Priority: 2
Process 2 Arrival Time: 3
Process 2 Burst Time: 8
Process 2 Priority: 1
Process 3 Arrival Time: 2
Process 3 Burst Time: 4
Process 3 Priority: 5

First Come First Serve Scheduling Results:
PID     Arrival Burst   Completion      Waiting Turnaround
1       0       5       5               0       5
3       2       4       9               3       7
2       3       8       17              6       14