Oe Question Preparation3
Oe Question Preparation3
import threading
import random4
import time
buffer_size= 5
buffer=[]
buffer_locks = threading.Lock()
empty_slots=threading.Semaphore(buffer_size)
full_slots = threading.Semaphore(0)
def producer():
for i in range(10):
item=random.randint(1,100)
empty_slots.acquire()
with buffer_locks:
buffer.append(item)
print(f"producer produced:{item}")
full_slots.release()
def consumer():
for i in range(10):
full_slots.acquire()
with buffer_locks:
item = buffer.pop(0)
print(f"consumer consumed {item}")
empty_slots.release()
if __name__ == "__main__":
producer_thread = threading .Thread(target=producer)
consumer_thread = threading.Thread(target=consumer)
producer_thread.start()
consumer_thread.start()
producer_thread.join()
consumer_thread.join()
pactical 2
# Input data
processes = [1, 2, 3, 4, 5]
bt = [11, 22, 33, 44, 55]
n = len(processes)
wt = [0] * n # Initialize waiting times
tt = [0] * n # Initialize turnaround times
# Print averages
print(f"\nAverage Waiting Time: {total_wt / n:.2f}")
print(f"Average Turnaround Time: {total_tt / n:.2f}")
os pract 3
import threading
import time
import random
BUFFER_SIZE = 5
NUM_ITEMS = 10
buffer = []
buffer_lock = threading.Lock()
not_full = threading.Condition(buffer_lock)
not_empty = threading.Condition(buffer_lock)
def producer():
for i in range(NUM_ITEMS):
item = f'item-{i}'
with not_full:
while len(buffer) == BUFFER_SIZE:
print("Buffer is full. Producer is waiting.")
not_full.wait()
buffer.append(item)
print(f'Produced: {item} | Buffer Index: {len(buffer) - 1}')
not_empty.notify()
time.sleep(random.uniform(0.1, 0.5))
def consumer():
for _ in range(NUM_ITEMS):
with not_empty:
while len(buffer) == 0:
print("Buffer is empty. Consumer is waiting.")
not_empty.wait()
item = buffer.pop(0)
print(f'Consumed: {item} | Remaining Items: {len(buffer)}')
not_full.notify()
time.sleep(random.uniform(0.1, 0.5))
if __name__ == "__main__":
producer_thread = threading.Thread(target=producer)
consumer_thread = threading.Thread(target=consumer)
producer_thread.start()
consumer_thread.start()
producer_thread.join()
consumer_thread.join()
#ospract4
import threading
class ReadersWriters:
def __init__(self):
self.read_count = 0
self.resource_lock = threading.Lock()
self.read_count_lock = threading.Lock()
if __name__ == "__main__":
rw = ReadersWriters()
threads = []
for i in range(3):
t = threading.Thread(target=rw.reader, args=(i+1,))
threads.append(t)
for i in range(2):
t = threading.Thread(target=rw.writer, args=(i+1,))
threads.append(t)
for t in threads:
t.start()
for t in threads:
t.join()
practical 8
#prctos 8
def print_tbl(alloc, max_d, avail, need):
print("\nP\tAlloc\t\tMax\t\t\tAvail\t\tNeed")
for i in range(len(alloc)):
print(f"P{i}\t{alloc[i]}\t{max_d[i]}\t{avail if i == 0 else ' '}\t{need[i]}")
def banker_algo():
p, r = 5, 3 # Number of processes and resources
alloc = [[0, 1, 0], [2, 0, 0], [3, 0, 3], [2, 1, 1], [0, 0, 2]]
max_d = [[7, 5, 3], [3, 2, 2], [9, 0, 5], [2, 2, 2], [4, 3, 3]]
avail = [3, 3, 2]
need = [[max_d[i][j] - alloc[i][j] for j in range(r)] for i in range(p)]
if all(req[j] <= need[req_p][j] for j in range(r)) and all(req[j] <= avail[j] for j in range(r)):
# Temporarily allocate resources
for j in range(r):
avail[j] -= req[j]
alloc[req_p][j] += req[j]
need[req_p][j] -= req[j]
# Check if the system is still in a safe state
safe, seq = is_safe(p, avail, alloc, need)
print("Request granted." if safe else "Request denied. Unsafe state.")
if not safe: # Rollback if unsafe
for j in range(r):
avail[j] += req[j]
alloc[req_p][j] -= req[j]
need[req_p][j] += req[j]
else:
print("Request denied. Not enough resources or exceeds need.")
if __name__ == "__main__":
banker_algo()
practical 9
#withtcmntpcos9
#1 2 3 4 1 2 5 1 2 3 4 5 iput me dalna hai
if flag == 0:
miss += 1
memory[mptr] = int(page)
mptr += 1
if mptr == msize:
mptr = 0
print(memory)
return miss
def main():
x = input("Enter the page string (space-separated): ").split()
m = int(input("Enter the size of memory: "))
page_faults = fifo(x, m)
if __name__ == "__main__":
main()
practical 10
"""practos10lru"""
from collections import OrderedDict
class LRUCache:
def __init__(self, capacity):
self.capacity = capacity
self.cache = OrderedDict()
def main():
n = int(input("Enter the number of pages: "))
frame_size = int(input("Enter the number of frames: "))
pages = []
print("Enter the reference string (space-separated):")
pages = list(map(int, input().split()))
cache = LRUCache(frame_size)
page_faults = 0
page_hits = 0
if __name__ == "__main__":
main()
class Process:
def __init__(self, name, burst_time, priority):
self.name = name
self.burst_time = burst_time
self.priority = priority
def priority_scheduling(processes):
processes.sort(key=lambda x: x.priority)
total_turnaround_time = 0
total_waiting_time = 0
print(f"{process.name:<10}{process.burst_time:<15}{process.priority:<10}{start_time:<12}
{finish_time:<12}{turnaround_time:<15}{waiting_time:<15}")
current_time = finish_time
total_turnaround_time += turnaround_time
total_waiting_time += waiting_time
process_list = [
Process('P1', 4, 3),
Process('P2', 5, 1),
Process('P3', 6, 4),
Process('P4', 2, 2),
]
priority_scheduling(process_list)
os pract 7
for i in range(n):
turnaround_time[i] = completion_time[i]
waiting_time[i] = turnaround_time[i] - burst_times[i]
pract05
for i in range(n):
if i == 0:
completion_time[i] = arrival_time[i] + burst_time[i]
else:
completion_time[i] = max(completion_time[i - 1], arrival_time[i]) + burst_time[i]
for i in range(n):
turnaround_time[i] = completion_time[i] - arrival_time[i]
waiting_time[i] = turnaround_time[i] - burst_time[i]
avg_tat = sum(turnaround_time) / n
avg_wt = sum(waiting_time) / n
processes = [1, 2, 3, 4]
arrival_time = [0, 2, 4, 6]
burst_time = [8, 4, 2, 1]