Compare commits
3 Commits
c818e00447
...
4966a50eee
Author | SHA1 | Date | |
---|---|---|---|
4966a50eee | |||
71d10df55e | |||
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))
|
@ -0,0 +1,28 @@
|
||||
def best_fit(memory_blocks, process_sizes):
|
||||
allocation = [-1] * len(process_sizes)
|
||||
|
||||
for i in range(len(process_sizes)):
|
||||
best_idx = -1
|
||||
best_size = float('inf')
|
||||
|
||||
for j in range(len(memory_blocks)):
|
||||
if memory_blocks[j] >= process_sizes[i] and memory_blocks[j] - process_sizes[i] < best_size:
|
||||
best_size = memory_blocks[j] - process_sizes[i]
|
||||
best_idx = j
|
||||
|
||||
if best_idx != -1:
|
||||
allocation[i] = best_idx
|
||||
memory_blocks[best_idx] -= process_sizes[i]
|
||||
|
||||
print("\nBest Fit Allocation:")
|
||||
for i in range(len(process_sizes)):
|
||||
if allocation[i] != -1:
|
||||
print(f"Process {i+1} allocated to Block {allocation[i]+1}")
|
||||
else:
|
||||
print(f"Process {i+1} not allocated")
|
||||
|
||||
# Example Memory Blocks and Process Sizes
|
||||
memory_blocks = [100, 500, 200, 300, 600]
|
||||
process_sizes = [212, 417, 112, 426]
|
||||
|
||||
best_fit(memory_blocks, process_sizes)
|
@ -0,0 +1,22 @@
|
||||
def first_fit(memory_blocks, process_sizes):
|
||||
allocation = [-1] * len(process_sizes)
|
||||
|
||||
for i in range(len(process_sizes)):
|
||||
for j in range(len(memory_blocks)):
|
||||
if memory_blocks[j] >= process_sizes[i]:
|
||||
allocation[i] = j
|
||||
memory_blocks[j] -= process_sizes[i]
|
||||
break
|
||||
|
||||
print("\nFirst Fit Allocation:")
|
||||
for i in range(len(process_sizes)):
|
||||
if allocation[i] != -1:
|
||||
print(f"Process {i+1} allocated to Block {allocation[i]+1}")
|
||||
else:
|
||||
print(f"Process {i+1} not allocated")
|
||||
|
||||
# Example Memory Blocks and Process Sizes
|
||||
memory_blocks = [100, 500, 200, 300, 600]
|
||||
process_sizes = [212, 417, 112, 426]
|
||||
|
||||
first_fit(memory_blocks, process_sizes)
|
@ -0,0 +1,25 @@
|
||||
def next_fit(memory_blocks, process_sizes):
|
||||
allocation = [-1] * len(process_sizes)
|
||||
next_index = 0
|
||||
|
||||
for i in range(len(process_sizes)):
|
||||
while next_index < len(memory_blocks):
|
||||
if memory_blocks[next_index] >= process_sizes[i]:
|
||||
allocation[i] = next_index
|
||||
memory_blocks[next_index] -= process_sizes[i]
|
||||
next_index = (next_index + 1) % len(memory_blocks) # Move to next block
|
||||
break
|
||||
next_index += 1
|
||||
|
||||
print("\nNext Fit Allocation:")
|
||||
for i in range(len(process_sizes)):
|
||||
if allocation[i] != -1:
|
||||
print(f"Process {i+1} allocated to Block {allocation[i]+1}")
|
||||
else:
|
||||
print(f"Process {i+1} not allocated")
|
||||
|
||||
# Example Memory Blocks and Process Sizes
|
||||
memory_blocks = [100, 500, 200, 300, 600]
|
||||
process_sizes = [212, 417, 112, 426]
|
||||
|
||||
next_fit(memory_blocks, process_sizes)
|
@ -0,0 +1,28 @@
|
||||
def worst_fit(memory_blocks, process_sizes):
|
||||
allocation = [-1] * len(process_sizes)
|
||||
|
||||
for i in range(len(process_sizes)):
|
||||
worst_idx = -1
|
||||
worst_size = -1
|
||||
|
||||
for j in range(len(memory_blocks)):
|
||||
if memory_blocks[j] >= process_sizes[i] and memory_blocks[j] > worst_size:
|
||||
worst_size = memory_blocks[j]
|
||||
worst_idx = j
|
||||
|
||||
if worst_idx != -1:
|
||||
allocation[i] = worst_idx
|
||||
memory_blocks[worst_idx] -= process_sizes[i]
|
||||
|
||||
print("\nWorst Fit Allocation:")
|
||||
for i in range(len(process_sizes)):
|
||||
if allocation[i] != -1:
|
||||
print(f"Process {i+1} allocated to Block {allocation[i]+1}")
|
||||
else:
|
||||
print(f"Process {i+1} not allocated")
|
||||
|
||||
# Example Memory Blocks and Process Sizes
|
||||
memory_blocks = [100, 500, 200, 300, 600]
|
||||
process_sizes = [212, 417, 112, 426]
|
||||
|
||||
worst_fit(memory_blocks, process_sizes)
|
31
Codes/Python version/Assignment-A7 (Page replacement)/LRU.py
Normal file
31
Codes/Python version/Assignment-A7 (Page replacement)/LRU.py
Normal file
@ -0,0 +1,31 @@
|
||||
class LRU:
|
||||
def __init__(self, capacity):
|
||||
self.capacity = capacity
|
||||
self.cache = {}
|
||||
self.order = []
|
||||
|
||||
def refer(self, page):
|
||||
if page not in self.cache:
|
||||
if len(self.cache) >= self.capacity:
|
||||
lru_page = self.order.pop(0) # Remove least recently used page
|
||||
del self.cache[lru_page]
|
||||
else:
|
||||
self.order.remove(page) # Remove page to update its order
|
||||
self.cache[page] = True
|
||||
self.order.append(page)
|
||||
|
||||
def display(self):
|
||||
print("Current Pages in Memory (LRU):", list(self.cache.keys()))
|
||||
|
||||
# Simulate LRU
|
||||
def lru_simulation(pages, capacity):
|
||||
lru = LRU(capacity)
|
||||
for page in pages:
|
||||
lru.refer(page)
|
||||
lru.display()
|
||||
|
||||
# Example Pages and Capacity
|
||||
pages = [7, 0, 1, 2, 0, 3, 0, 4]
|
||||
capacity = 3
|
||||
print("LRU Page Replacement Simulation:")
|
||||
lru_simulation(pages, capacity)
|
@ -0,0 +1,44 @@
|
||||
class Optimal:
|
||||
def __init__(self, capacity):
|
||||
self.capacity = capacity
|
||||
self.cache = []
|
||||
|
||||
def refer(self, page, future):
|
||||
if page not in self.cache:
|
||||
if len(self.cache) < self.capacity:
|
||||
self.cache.append(page)
|
||||
else:
|
||||
farthest_page = self.find_farthest_page(future)
|
||||
index = self.cache.index(farthest_page)
|
||||
self.cache[index] = page
|
||||
|
||||
def find_farthest_page(self, future):
|
||||
farthest = -1
|
||||
farthest_page = None
|
||||
for page in self.cache:
|
||||
if page not in future:
|
||||
return page
|
||||
try:
|
||||
index = future.index(page)
|
||||
if index > farthest:
|
||||
farthest = index
|
||||
farthest_page = page
|
||||
except ValueError:
|
||||
continue
|
||||
return farthest_page
|
||||
|
||||
def display(self):
|
||||
print("Current Pages in Memory (Optimal):", self.cache)
|
||||
|
||||
# Simulate Optimal Page Replacement
|
||||
def optimal_simulation(pages, capacity):
|
||||
optimal = Optimal(capacity)
|
||||
for i in range(len(pages)):
|
||||
optimal.refer(pages[i], pages[i + 1:])
|
||||
optimal.display()
|
||||
|
||||
# Example Pages and Capacity
|
||||
pages = [7, 0, 1, 2, 0, 3, 0, 4]
|
||||
capacity = 3
|
||||
print("\nOptimal Page Replacement Simulation:")
|
||||
optimal_simulation(pages, capacity)
|
Loading…
Reference in New Issue
Block a user