OS LAB NEW-1
OS LAB NEW-1
DATE : 16.12.24
PROGRAM
import os
def info(title):
print(title)
def f(name):
info('function f')
print('hello', name)
if __name__ == '__main__':
info('main line')
p = Process(target=f, args=('bob',))
p.start()
p.join()
main line
function f
hello bob
# Example Process ID
EX NO : 1(b) BASIC I/O PROGRAMMING – FILE OPERATION
DATE : 16.12.24
PROGRAM:
file1.write("Hello \n")
file1.writelines(L)
file1.close()
print()
file1.seek(0)
print()
file1.seek(0)
# Output of read function with a specified number of characters (9 characters in this case)
file1.seek(0)
# Output of readline function with a specified number of characters (9 characters in this case)
print()
file1.seek(0)
print()
file1.close()
OUTPUT
Hello
This is Delhi
This is Paris
This is London
Hello
This is D
This is
['Hello \n', 'This is Delhi \n', 'This is Paris \n', 'This is London \n']
EX NO: 2 SHORTEST JOB FIRST ALGORITHM
DATE : 03.01.25
PROGRAM
for i in range(n):
bt.sort(key=lambda x: x[1])
sumbt = sum(abt)
ready_queue = [proc for proc in bt if proc[1] <= current_time and proc[0] > 0]
if ready_queue:
shortest = ready_queue[0]
shortest_index = bt.index(shortest)
if bt[shortest_index][0] == 0:
else:
for i in range(n):
print('PNo\tBT\tAT\tCT\tTAT\tWT')
for i in range(n):
print(f"{i+1}\t{abt[i]}\t{at[i]}\t{ct[i]}\t{tat[i]}\t{wt[i]}")
Enter no of processes: 4
OUTPUT:
PNo BT AT CT TAT WT
1 6 2 14 12 6
2 8 0 22 22 14
3 7 1 20 19 12
4 3 3 16 13 10
DATE : 20.01.25
PROGRAM
for i in range(n):
bt.sort(key=lambda x: x[1])
sumbt = sum(abt)
ready_queue = [proc for proc in bt if proc[1] <= current_time and proc[0] > 0]
if ready_queue:
# Sort by burst time (smallest burst time first)
ready_queue.sort(key=lambda x: x[0])
shortest = ready_queue[0]
shortest_index = bt.index(shortest)
if bt[shortest_index][0] == 0:
else:
for i in range(n):
print('PNo\tBT\tAT\tCT\tTAT\tWT')
for i in range(n):
print(f"{i+1}\t{abt[i]}\t{at[i]}\t{ct[i]}\t{tat[i]}\t{wt[i]}")
Enter no of processes: 4
OUTPUT
1 24 0 24
2 3 24 27
3 3 27 30
DATE :20.01.25
PROGRAM
for i in range(n):
rem_bt[i] = bt[i]
t = 0 # Current time
while True:
done = True
for i in range(n):
if rem_bt[i] > 0:
t += quantum
rem_bt[i] -= quantum
else:
t = t + rem_bt[i]
wt[i] = t - bt[i]
rem_bt[i] = 0
if done:
break
for i in range(n):
total_wt = 0
total_tat = 0
for i in range(n):
total_wt += wt[i]
total_tat += tat[i]
# Driver code
if __name__ == "__main__":
# Process id's
proc = [1, 2, 3]
n=3
burst_time = [24, 3, 3]
# Time quantum
quantum = 4
1 24 0 24
2 3 24 27
3 3 27 30
DATE : 28.01.25
PROGRAM
wt[i] = processes[i - 1][1] + wt[i - 1] # Waiting time = sum of burst times of previous
processes
for i in range(n):
# Function to calculate and display average waiting time and average turnaround time
findWaitingTime(processes, n, wt)
total_wt = 0
total_tat = 0
for i in range(n):
total_wt += wt[i]
total_tat += tat[i]
for i in proc:
findavgTime(proc, n)
# Driver code
if __name__ == "__main__":
priorityScheduling(proc, n)
OUTPUT
132
1 10 0 10
3 8 10 18
2 5 18 23
DATE : 28.01.25
PROGRAM:
import threading
import time
class ReaderWriterProblem:
def __init__(self):
def reader(self):
while True:
self.wrt.acquire()
# Reading operation
self.wrt.release()
def writer(self):
while True:
print("-" * 20)
def main(self):
t1 = threading.Thread(target=self.reader)
t2 = threading.Thread(target=self.writer)
t3 = threading.Thread(target=self.reader)
t4 = threading.Thread(target=self.reader)
t5 = threading.Thread(target=self.reader)
t6 = threading.Thread(target=self.writer)
t1.start()
t2.start()
t3.start()
t4.start()
t5.start()
t6.start()
if __name__ == "__main__":
c = ReaderWriterProblem()
c.main()
OUTPUT
Reader 1 is
reading Reader 2
is reading Reader
4 is reading
Reader 3 is
reading Wrting
data.....
Wrting data.....
Reader 1 is
reading Reader 3
is reading Reader
2 is reading
Reader 4 is
reading
Wrting data.....
Wrting data.....
EX NO : 6 IMPLEMENT BANKER’S ALGORITHM FOR DEADLOCK AVOIDANCE
DATE : 05.02.25
PROGRAM:
P = 5 # Processes
R = 3 # Resources
for i in range(P):
for j in range(R):
need = []
for i in range(P):
l = []
for j in range(R):
l.append(0)
need.append(l)
finish = [0] * P
safeSeq = [0] * P
# Available resources
work = [0] * R
for i in range(R):
work[i] = avail[i]
count = 0 # Count of processes that have been allocated
found = False
for p in range(P):
if finish[p] == 0:
for j in range(R):
break
else:
for k in range(R):
work[k] += allot[p][k]
safeSeq[count] = p
count += 1
finish[p] = 1
found = True
break
if not found:
return False
return True
# Driver code
if __name__ == "__main__":
avail = [3, 3, 2]
# Maximum resources that can be allocated to each process
maxm = [
[7, 5, 3],
[3, 2, 2],
[9, 0, 2],
[2, 2, 2],
[4, 3, 3]
allot = [
[0, 1, 0],
[2, 0, 0],
[3, 0, 2],
[2, 1, 1],
[0, 0, 2]
OUTPUT:
PROGRAM:
# Driver code
if __name__ == '__main__':
pages = [3, 2, 1, 0, 3, 2, 4, 3, 2, 1, 0, 4] # Given page reference string
n = len(pages) # Total number of pages
capacity = 3 # Memory capacity (number of frames)
{3} {2, 3} {1, 2, 3} {0, 1, 2} {0, 1, 3} {0, 2, 3} {2, 3, 4} {2, 3, 4} {2, 3, 4} {1, 2, 4} {0, 1, 4} {0, 1, 4}
Page Fault Count: 9
Total Page Fault Count: 9
EX NO : 8 LEAST RECENTLY USED ALGORITHM
DATE : 19.02.25
PROGRAM:
# Python program for LRU (Least Recently Used) Page Replacement Algorithm
# Driver code
capacity = 3 # The number of frames available in memory
processList = [0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2] # Given page reference string
OUTPUT:
[0] [0, 1] [0, 1, 2] [0, 1, 2] [1, 2, 0] [2, 0, 3] [0, 3, 2] [0, 3, 2] [3, 2, 0] [2, 0, 3] [0, 3, 2] [0, 3, 2]
Total Page Faults: 9
EX NO : 9(a) IMPLEMENT FIRST FIT ALGORITHM FOR MEMORY MANAGEMENT
DATE : 19.02.25
PROGRAM:
FirstFit(block_Size, m, process_Size, n)
OUTPUT:
PROGRAM
# Driver code
if __name__ == "__main__":
blockSize = [100, 500, 200, 300, 600] # Memory block sizes
processSize = [212, 417, 112, 426] # Process sizes
m = len(blockSize) # Number of blocks
n = len(processSize) # Number of processes
PROGRAM:
# Check each block to find the worst fit (largest block that fits)
for j in range(m):
if blockSize[j] >= processSize[i]: # The block is big enough to accommodate the
process
if wstIdx == -1:
wstIdx = j # First block that can accommodate the process
elif blockSize[wstIdx] < blockSize[j]: # Choose the block with the largest size
wstIdx = j
1 212 2
2 417 5
3 112 4
4 426 Not Allocated
EX NO : 10 INTER PROCESS COMMUNICATION FOR SHARED MEMORY
DATE : 04.03.25
PROGRAM:
# Driver code
if __name__ == '__main__':
# Create a shared variable 'num' of type double ('d') initialized to 0.0
num = Value('d', 0.0)
# Create and start processes that will modify the shared memory
p1 = Process(target=f, args=(num, arr)) # Process to modify 'num' and 'arr'
p1.start()
p1.join() # Wait for process p1 to finish