0% found this document useful (0 votes)
7 views6 pages

Exp 7

The document discusses two key concepts in computer science: the Producer-Consumer problem and the Banker's algorithm. The Producer-Consumer problem involves synchronization between a producer that generates items and a consumer that consumes them using a shared memory buffer, while the Banker's algorithm is a resource allocation method that prevents deadlock by ensuring safe resource allocation through a simulation of resource distribution. Both concepts are illustrated with Python code examples demonstrating their implementation.
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)
7 views6 pages

Exp 7

The document discusses two key concepts in computer science: the Producer-Consumer problem and the Banker's algorithm. The Producer-Consumer problem involves synchronization between a producer that generates items and a consumer that consumes them using a shared memory buffer, while the Banker's algorithm is a resource allocation method that prevents deadlock by ensuring safe resource allocation through a simulation of resource distribution. Both concepts are illustrated with Python code examples demonstrating their implementation.
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/ 6

Shrishti Raina A009

Btech CE Producer consumer problem and bankers


algorithm
Experiment 7

Producer consumer problem


The Producer-Consumer problem is a classical multi-process synchronization problem, that is we
are trying to achieve synchronization between more than one process.
There is one Producer in the producer-consumer problem, Producer is producing some items,
whereas there is one Consumer that is consuming the items produced by the Producer. The same
memory buffer is shared by both producers and consumers which is of fixed-size.
The task of the Producer is to produce the item, put it into the memory buffer, and again start
expitems.
producing 7.docx
Whereas the task of the Consumer is to consume the item from the memory
buffer.
import threading
import time
import queue

shared_queue = queue.Queue()

def producer():
for i in range(5):
item = f"Item {i}"
print(f"Producing {item}")
shared_queue.put(item)
time.sleep(1)

def consumer():
for i in range(5):
item = shared_queue.get()
print(f"Consuming {item}")
shared_queue.task_done()
time.sleep(1)

producer_thread = threading.Thread(target=producer)
consumer_thread = threading.Thread(target=consumer)

producer_thread.start()
consumer_thread.start()

producer_thread.join()
consumer_thread.join()

print("Producer and consumer threads have finished.")


Bankers algorithm
Banker's algorithm is a resource allocation and deadlock avoidance algorithm developed by Edsger
Dijkstra that tests for safety by simulating the allocation of predetermined maximum possible
amounts of all resources, and then makes an "s-state" check to test for possible deadlock conditions
for all other pending activities, before deciding whether allocation should be allowed to continue.
Also, when a process gets all its requested resources it must return them in a finite amount of time.

class BankersAlgorithm:
def __init__(self, n, m):

self.n = n
self.m = m
self.available = []
self.max_claim = []
self.allocation = []
self.need = []

def input_data(self):
print("Enter available resources:")
self.available = list(map(int, input().split()))

print("Enter maximum resource claim for each process:")


for i in range(self.n):
max_claim = list(map(int, input().split()))
self.max_claim.append(max_claim)

print("Enter resources currently allocated to processes:")


for i in range(self.n):
allocation = list(map(int, input().split()))
self.allocation.append(allocation)

for i in range(self.n):
need = [self.max_claim[i][j] - self.allocation[i][j]
for j in range(self.m)]
for j in range(self.m)]
self.need.append(need)

def is_safe(self):
work = self.available.copy()
finish = [False] * self.n
sequence = []

while True:

found = False
for i in range(self.n):
if not finish[i]:
if all(self.need[i][j] <= work[j] for j in range(self.m)):

for j in range(self.m):
work[j] += self.allocation[i][j]
finish[i] = True
sequence.append(i)
found = True

if not found:

break

if all(finish):
print("Safe sequence:", sequence)
return True
else:
print("Unsafe state, no safe sequence exists.")
return False

if __name__ == "__main__":
n = int(input("Enter the number of processes: "))
m = int(input("Enter the number of resources: "))
banker = BankersAlgorithm(n, m)
banker.input_data()
banker.is_safe()

safe state

You might also like