Added fcfs, priority, round robin, sjf codes (a5).
This commit is contained in:
parent
c818e00447
commit
003d76229a
23
Codes/Python version/Assignment-A5 (CPU Scheduling)/FCFS.py
Normal file
23
Codes/Python version/Assignment-A5 (CPU Scheduling)/FCFS.py
Normal file
@ -0,0 +1,23 @@
|
||||
def fcfs_scheduling(processes, n):
|
||||
processes.sort(key=lambda x: x[1])
|
||||
completion_time = 0
|
||||
waiting_time = []
|
||||
turnaround_time = []
|
||||
|
||||
for process in processes:
|
||||
pid, arrival_time, burst_time = process
|
||||
|
||||
if completion_time < arrival_time:
|
||||
completion_time = arrival_time
|
||||
|
||||
completion_time += burst_time
|
||||
turnaround_time.append(completion_time - arrival_time)
|
||||
waiting_time.append(completion_time - arrival_time - burst_time)
|
||||
|
||||
print("\nFCFS Scheduling:")
|
||||
for i, process in enumerate(processes):
|
||||
print(f"Process {process[0]}: Waiting Time = {waiting_time[i]}, Turnaround Time = {turnaround_time[i]}")
|
||||
|
||||
# Input: Process ID, Arrival Time, Burst Time
|
||||
processes = [[1, 0, 5], [2, 1, 3], [3, 2, 8], [4, 3, 6]]
|
||||
fcfs_scheduling(processes, len(processes))
|
@ -0,0 +1,23 @@
|
||||
def priority_scheduling(processes, n):
|
||||
processes.sort(key=lambda x: (x[2], x[1]))
|
||||
completion_time = 0
|
||||
waiting_time = []
|
||||
turnaround_time = []
|
||||
|
||||
for process in processes:
|
||||
pid, arrival_time, priority, burst_time = process
|
||||
|
||||
if completion_time < arrival_time:
|
||||
completion_time = arrival_time
|
||||
|
||||
completion_time += burst_time
|
||||
turnaround_time.append(completion_time - arrival_time)
|
||||
waiting_time.append(completion_time - arrival_time - burst_time)
|
||||
|
||||
print("\nPriority (Non-Preemptive) Scheduling:")
|
||||
for i, process in enumerate(processes):
|
||||
print(f"Process {process[0]}: Waiting Time = {waiting_time[i]}, Turnaround Time = {turnaround_time[i]}")
|
||||
|
||||
# Input: Process ID, Arrival Time, Priority, Burst Time
|
||||
processes = [[1, 0, 1, 5], [2, 1, 3, 3], [3, 2, 2, 8], [4, 3, 4, 6]]
|
||||
priority_scheduling(processes, len(processes))
|
@ -0,0 +1,36 @@
|
||||
def round_robin(processes, n, quantum):
|
||||
remaining_time = [bt for _, _, bt in processes]
|
||||
t = 0
|
||||
waiting_time = [0] * n
|
||||
turnaround_time = [0] * n
|
||||
complete = [False] * n
|
||||
|
||||
while True:
|
||||
done = True
|
||||
|
||||
for i in range(n):
|
||||
if remaining_time[i] > 0:
|
||||
done = False
|
||||
|
||||
if remaining_time[i] > quantum:
|
||||
t += quantum
|
||||
remaining_time[i] -= quantum
|
||||
else:
|
||||
t += remaining_time[i]
|
||||
waiting_time[i] = t - processes[i][2] - processes[i][1]
|
||||
remaining_time[i] = 0
|
||||
|
||||
if done:
|
||||
break
|
||||
|
||||
for i in range(n):
|
||||
turnaround_time[i] = processes[i][2] + waiting_time[i]
|
||||
|
||||
print("\nRound Robin (Preemptive) Scheduling:")
|
||||
for i, process in enumerate(processes):
|
||||
print(f"Process {process[0]}: Waiting Time = {waiting_time[i]}, Turnaround Time = {turnaround_time[i]}")
|
||||
|
||||
# Input: Process ID, Arrival Time, Burst Time
|
||||
processes = [[1, 0, 10], [2, 1, 4], [3, 2, 5], [4, 3, 3]]
|
||||
quantum = 2
|
||||
round_robin(processes, len(processes), quantum)
|
48
Codes/Python version/Assignment-A5 (CPU Scheduling)/SJF.py
Normal file
48
Codes/Python version/Assignment-A5 (CPU Scheduling)/SJF.py
Normal file
@ -0,0 +1,48 @@
|
||||
import sys
|
||||
|
||||
def sjf_preemptive(processes, n):
|
||||
remaining_time = [bt for _, _, bt in processes]
|
||||
complete = 0
|
||||
t = 0
|
||||
minm = sys.maxsize
|
||||
shortest = 0
|
||||
finish_time = 0
|
||||
check = False
|
||||
waiting_time = [0] * n
|
||||
turnaround_time = [0] * n
|
||||
|
||||
while complete != n:
|
||||
for j in range(n):
|
||||
if processes[j][1] <= t and remaining_time[j] < minm and remaining_time[j] > 0:
|
||||
minm = remaining_time[j]
|
||||
shortest = j
|
||||
check = True
|
||||
|
||||
if not check:
|
||||
t += 1
|
||||
continue
|
||||
|
||||
remaining_time[shortest] -= 1
|
||||
minm = remaining_time[shortest] if remaining_time[shortest] > 0 else sys.maxsize
|
||||
|
||||
if remaining_time[shortest] == 0:
|
||||
complete += 1
|
||||
check = False
|
||||
finish_time = t + 1
|
||||
waiting_time[shortest] = finish_time - processes[shortest][2] - processes[shortest][1]
|
||||
|
||||
if waiting_time[shortest] < 0:
|
||||
waiting_time[shortest] = 0
|
||||
|
||||
t += 1
|
||||
|
||||
for i in range(n):
|
||||
turnaround_time[i] = processes[i][2] + waiting_time[i]
|
||||
|
||||
print("\nSJF (Preemptive) Scheduling:")
|
||||
for i, process in enumerate(processes):
|
||||
print(f"Process {process[0]}: Waiting Time = {waiting_time[i]}, Turnaround Time = {turnaround_time[i]}")
|
||||
|
||||
# Input: Process ID, Arrival Time, Burst Time
|
||||
processes = [[1, 0, 8], [2, 1, 4], [3, 2, 9], [4, 3, 5]]
|
||||
sjf_preemptive(processes, len(processes))
|
Loading…
Reference in New Issue
Block a user