# FCFS(WithAT)
'''
def fcfs(arrival, burst):
n = len(arrival)
completion = [0] * n
waiting = [0] * n
turnaround = [0] * n
# Pair process IDs with arrival and burst times, then sort by arrival time
process = list(zip(range(1, n + 1), arrival, burst))
process.sort(key=lambda x: x[1])
process_id, arrival, burst = zip(*process)
# First process
completion[0] = arrival[0] + burst[0]
# Completion time for the rest
for i in range(1, n):
if arrival[i] > completion[i - 1]:
completion[i] = arrival[i] + burst[i]
else:
completion[i] = completion[i - 1] + burst[i]
# Turnaround time and waiting time
for i in range(n):
turnaround[i] = completion[i] - arrival[i]
waiting[i] = turnaround[i] - burst[i]
# Output
print("Process\tArrival\tBurst\tCompletion\tTurnaround\tWaiting")
for i in range(n):
print(f"P{process_id[i]}\t{arrival[i]}\t{burst[i]}\t{completion[i]}\t\t{turnaround[i]}\t\t{waiting[i]}")
avgwt = sum(waiting) / n
avgtat = sum(turnaround) / n
print(f"\nAverage Turnaround Time = {avgtat:.2f}")
print(f"Average Waiting Time = {avgwt:.2f}")
# Test data
arrival_time = [3, 4, 0, 3, 5]
burst_time = [1, 5, 2, 7, 5]
fcfs(arrival_time, burst_time)
'''
#FCFS(WithoutAT)
'''
def fcfs_no_arrival(burst_times):
n = len(burst_times)
process_id = list(range(1, n + 1))
waiting = [0] * n
turnaround = [0] * n
completion = [0] * n
# First process starts at time 0
completion[0] = burst_times[0]
turnaround[0] = burst_times[0]
waiting[0] = 0
for i in range(1, n):
completion[i] = completion[i - 1] + burst_times[i]
turnaround[i] = completion[i]
waiting[i] = turnaround[i] - burst_times[i]
# Output
print("Process\tBurst\tCompletion\tTurnaround\tWaiting")
for i in range(n):
print(f"P{process_id[i]}\t{burst_times[i]}\t{completion[i]}\t\t{turnaround[i]}\t\t{waiting[i]}")
avg_wt = sum(waiting) / n
avg_tat = sum(turnaround) / n
print(f"\nAverage Turnaround Time = {avg_tat:.2f}")
print(f"Average Waiting Time = {avg_wt:.2f}")
# Example usage
burst_time = [5, 3, 8, 6]
fcfs_no_arrival(burst_time)
'''
#SJF
'''
def sjf(arrival, burst):
n = len(arrival)
process_id = range(1, n + 1)
completion = [0] * n
waiting = [0] * n
turnaround = [0] * n
remaining_time = list(burst)
arrival = list(arrival)
current_time = 0
completed = 0
while completed < n:
idx = -1
min_time = float('inf')
# Find process with minimum remaining time at current_time
for i in range(n):
if arrival[i] <= current_time and remaining_time[i] > 0:
if remaining_time[i] < min_time:
min_time = remaining_time[i]
idx = i
if idx != -1:
remaining_time[idx] -= 1
current_time += 1
if remaining_time[idx] == 0:
completed += 1
completion[idx] = current_time
turnaround[idx] = completion[idx] - arrival[idx]
waiting[idx] = turnaround[idx] - burst[idx]
else:
current_time += 1
# Output
print("Process\tArrival\tBurst\tCompletion\tTurnaround\tWaiting")
for i in range(n):
print(f"P{process_id[i]}\t{arrival[i]}\t{burst[i]}\t{completion[i]}\t\t{turnaround[i]}\t\t{waiting[i]}")
avg_wt = sum(waiting) / n
avg_tat = sum(turnaround) / n
print(f"\nAverage Turnaround Time = {avg_tat:.2f}")
print(f"Average Waiting Time = {avg_wt:.2f}")
# Example test case
arrival_time = [2, 1, 4, 0, 6]
burst_time = [6, 3, 2, 5, 4]
sjf(arrival_time, burst_time)
'''
#RoundRobin
'''
from collections import deque
def round_robin_preemptive(arrival, burst, quantum):
n = len(arrival)
process_id = list(range(1, n + 1))
remaining_burst = list(burst)
completion = [0] * n
waiting = [0] * n
turnaround = [0] * n
current_time = 0
completed = 0
queue = deque()
is_in_queue = [False] * n
while completed < n:
# Add newly arrived processes to queue
for i in range(n):
if arrival[i] <= current_time and not is_in_queue[i] and remaining_burst[i] > 0:
queue.append(i)
is_in_queue[i] = True
if queue:
idx = queue.popleft()
# Execute process for min(quantum, remaining burst)
time_to_run = min(quantum, remaining_burst[idx])
current_time += time_to_run
remaining_burst[idx] -= time_to_run
# Check for newly arrived processes during execution
for i in range(n):
if arrival[i] <= current_time and not is_in_queue[i] and remaining_burst[i] > 0:
queue.append(i)
is_in_queue[i] = True
if remaining_burst[idx] == 0:
completed += 1
completion[idx] = current_time
turnaround[idx] = completion[idx] - arrival[idx]
waiting[idx] = turnaround[idx] - burst[idx]
else:
queue.append(idx) # Put back into queue if not finished
else:
current_time += 1 # CPU idle, no ready process
# Output
print("Process\tArrival\tBurst\tCompletion\tTurnaround\tWaiting")
for i in range(n):
print(f"P{process_id[i]}\t{arrival[i]}\t{burst[i]}\t{completion[i]}\t\t{turnaround[i]}\t\t{waiting[i]}")
avgwt = sum(waiting) / n
avgtat = sum(turnaround) / n
print(f"\nAverage Turnaround Time = {avgtat:.2f}")
print(f"Average Waiting Time = {avgwt:.2f}")
# Example usage
arrival_time = [0, 1, 2, 3, 4]
burst_time = [6, 3, 5, 2, 4]
quantum = 2
round_robin_preemptive(arrival_time, burst_time, quantum)
'''
#PriorityScheduling
'''
def priority_scheduling_preemptive(arrival, burst, priority):
n = len(arrival)
process_id = list(range(1, n + 1))
remaining_time = list(burst)
completion = [0] * n
turnaround = [0] * n
waiting = [0] * n
is_completed = [False] * n
current_time = 0
completed = 0
while completed < n:
# Find process with highest priority (lowest value) that has arrived
idx = -1
highest_priority = float('inf')
for i in range(n):
if arrival[i] <= current_time and not is_completed[i] and remaining_time[i] > 0:
if priority[i] < highest_priority:
highest_priority = priority[i]
idx = i
if idx != -1:
# Execute the process for 1 unit of time
remaining_time[idx] -= 1
current_time += 1
if remaining_time[idx] == 0:
completion[idx] = current_time
turnaround[idx] = completion[idx] - arrival[idx]
waiting[idx] = turnaround[idx] - burst[idx]
is_completed[idx] = True
completed += 1
else:
# No process is ready, CPU is idle
current_time += 1
# Output
print("Process\tArrival\tBurst\tPriority\tCompletion\tTurnaround\tWaiting")
for i in range(n):
print(f"P{process_id[i]}\t{arrival[i]}\t{burst[i]}\t{priority[i]}\t\t{completion[i]}\t\t{turnaround[i]}\t\t{waiting[i]}")
avgwt = sum(waiting) / n
avgtat = sum(turnaround) / n
print(f"\nAverage Turnaround Time = {avgtat:.2f}")
print(f"Average Waiting Time = {avgwt:.2f}")
# Example usage
arrival_time = [0, 1, 2, 3, 4]
burst_time = [6, 3, 8, 2, 4]
priority = [2, 1, 4, 5, 3] # Lower value means higher priority
priority_scheduling_preemptive(arrival_time, burst_time, priority)
'''