Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
2 views3 pages

OS Lab Programs

The document contains Python programs for various operating system scheduling algorithms, including FCFS, SJF, Priority Scheduling, and Round Robin Scheduling, as well as implementations of the Banker's Algorithm and FIFO/LRU page replacement methods. Each program prompts the user for input, calculates waiting time and turnaround time, and outputs the results. The code demonstrates fundamental concepts in process scheduling and memory management.

Uploaded by

amolchormale49
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)
2 views3 pages

OS Lab Programs

The document contains Python programs for various operating system scheduling algorithms, including FCFS, SJF, Priority Scheduling, and Round Robin Scheduling, as well as implementations of the Banker's Algorithm and FIFO/LRU page replacement methods. Each program prompts the user for input, calculates waiting time and turnaround time, and outputs the results. The code demonstrates fundamental concepts in process scheduling and memory management.

Uploaded by

amolchormale49
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/ 3

Operating System Lab Programs (Python)

1. FCFS (First Come First Serve) Scheduling


n = int(input("Enter number of processes: "))
bt = []
for i in range(n):
bt.append(int(input(f"Enter burst time for P{i+1}: ")))

wt = [0]*n
tat = [0]*n

for i in range(1, n):


wt[i] = wt[i-1] + bt[i-1]

for i in range(n):
tat[i] = wt[i] + bt[i]

print("\nProcess\tBT\tWT\tTAT")
for i in range(n):
print(f"P{i+1}\t{bt[i]}\t{wt[i]}\t{tat[i]}")

print("\nAverage WT =", sum(wt)/n)


print("Average TAT =", sum(tat)/n)

2. SJF (Shortest Job First) Scheduling


n = int(input("Enter number of processes: "))
bt = []
for i in range(n):
bt.append(int(input(f"Enter burst time for P{i+1}: ")))

p = [i+1 for i in range(n)]


p = [x for _,x in sorted(zip(bt,p))]
bt.sort()

wt = [0]*n
tat = [0]*n

for i in range(1, n):


wt[i] = wt[i-1] + bt[i-1]

for i in range(n):
tat[i] = wt[i] + bt[i]

print("\nProcess\tBT\tWT\tTAT")
for i in range(n):
print(f"P{p[i]}\t{bt[i]}\t{wt[i]}\t{tat[i]}")

print("\nAverage WT =", sum(wt)/n)


print("Average TAT =", sum(tat)/n)

3. Priority Scheduling
n = int(input("Enter number of processes: "))
bt = []
pr = []
for i in range(n):
bt.append(int(input(f"Enter burst time for P{i+1}: ")))
pr.append(int(input(f"Enter priority for P{i+1}: ")))

p = [i+1 for i in range(n)]


p = [x for _,x in sorted(zip(pr,p))]
bt = [x for _,x in sorted(zip(pr,bt))]
pr.sort()

wt = [0]*n
tat = [0]*n

for i in range(1, n):


wt[i] = wt[i-1] + bt[i-1]

for i in range(n):
tat[i] = wt[i] + bt[i]

print("\nProcess\tBT\tPR\tWT\tTAT")
for i in range(n):
print(f"P{p[i]}\t{bt[i]}\t{pr[i]}\t{wt[i]}\t{tat[i]}")

4. Round Robin Scheduling


n = int(input("Enter number of processes: "))
bt = []
for i in range(n):
bt.append(int(input(f"Enter burst time for P{i+1}: ")))

qt = int(input("Enter time quantum: "))

rem_bt = bt[:]
t = 0
wt = [0]*n
tat = [0]*n

while True:
done = True
for i in range(n):
if rem_bt[i] > 0:
done = False
if rem_bt[i] > qt:
t += qt
rem_bt[i] -= qt
else:
t += rem_bt[i]
wt[i] = t - bt[i]
rem_bt[i] = 0
if done:
break

for i in range(n):
tat[i] = wt[i] + bt[i]

print("\nProcess\tBT\tWT\tTAT")
for i in range(n):
print(f"P{i+1}\t{bt[i]}\t{wt[i]}\t{tat[i]}")

5. Banker's Algorithm
n = 3
m = 3

alloc = [[0,1,0],[2,0,0],[3,0,2]]
maxm = [[7,5,3],[3,2,2],[9,0,2]]
avail = [3,3,2]

need = [[maxm[i][j]-alloc[i][j] for j in range(m)] for i in range(n)]


f = [0]*n
ans = []

def isSafe():
work = avail[:]
count = 0
while count < n:
found = False
for i in range(n):
if f[i] == 0:
if all(need[i][j] <= work[j] for j in range(m)):
for k in range(m):
work[k] += alloc[i][k]
ans.append(i)
f[i] = 1
found = True
count += 1
if not found:
return False
return True

if isSafe():
print("System is in Safe State.")
print("Safe Sequence:", ["P"+str(i) for i in ans])
else:
print("System is not in Safe State.")

6. FIFO Page Replacement


pages = [7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2]
capacity = 3
s = []
page_faults = 0

for i in pages:
if i not in s:
if len(s) < capacity:
s.append(i)
else:
s.pop(0)
s.append(i)
page_faults += 1

print("Total Page Faults (FIFO):", page_faults)

7. LRU Page Replacement


pages = [7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2]
capacity = 3
s = []
page_faults = 0

for i in range(len(pages)):
if pages[i] not in s:
if len(s) < capacity:
s.append(pages[i])
else:
lru = min(s, key=lambda x: pages[:i][::-1].index(x))
s.remove(lru)
s.append(pages[i])
page_faults += 1

print("Total Page Faults (LRU):", page_faults)

You might also like