PROGRAM 3
PROGRAM 3
PRODUCER
while (true) {
CONSUMER
while (true) {
while (count == 0)
; // do nothing
nextConsumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
count--;
/* consume the item in nextConsumed
}
import time
CAPACITY = 10
in_index = 0
out_index = 0
mutex = threading.Semaphore()
empty = threading.Semaphore(CAPACITY)
full = threading.Semaphore(0)
def run(self):
items_produced = 0
counter = 0
empty.acquire()
mutex.acquire()
counter += 1
buffer[in_index] = counter
mutex.release()
full.release()
time.sleep(1)
items_produced += 1
class Consumer(threading.Thread):
def run(self):
global CAPACITY, buffer, in_index, out_index, counter
items_consumed = 0
full.acquire()
mutex.acquire()
item = buffer[out_index]
mutex.release()
empty.release()
time.sleep(2.5)
items_consumed += 1
producer = Producer()
consumer = Consumer()
consumer.start()
producer.start()
producer.join()
consumer.join()
OUTPUT: