producerconsumer3.py. Bounded Buffer Producer/Consumer Solution using semaphores. Download the solution on to a workstat

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

producerconsumer3.py. Bounded Buffer Producer/Consumer Solution using semaphores. Download the solution on to a workstat

Post by answerhappygod »

Producerconsumer3 Py Bounded Buffer Producer Consumer Solution Using Semaphores Download The Solution On To A Workstat 1
Producerconsumer3 Py Bounded Buffer Producer Consumer Solution Using Semaphores Download The Solution On To A Workstat 1 (127.09 KiB) Viewed 59 times
Producerconsumer3 Py Bounded Buffer Producer Consumer Solution Using Semaphores Download The Solution On To A Workstat 2
Producerconsumer3 Py Bounded Buffer Producer Consumer Solution Using Semaphores Download The Solution On To A Workstat 2 (111.84 KiB) Viewed 59 times
producerconsumer3.py. Bounded Buffer Producer/Consumer Solution using semaphores. Download the solution on to a workstation capable of running python programs. Run the program first to ensure that it runs correctly. Comment out any one call to semWait or equivalent on any of the semaphores in either the procuder_func() or consumer_func() and document your observations. Comment out any call to semSignal or equivalent and again document your observations. Please be specific and use the terms that you learned in the lecture and your own studies. Please the recall the following regarding the semaphore solution to this problem: The semaphore "s" enforces mutual exclusion. The semaphore "n" is the number of items in the buffer. The semaphore "e" keeps track of the number of empty spaces.

#!/usr/bin/python # # File: producerconsumer3.py # Author: Theodore Farrison # Course: Operating Systems # # Description: A solution to the Bounded - Buffer Producer/Consumer # using semaphores # from random import randrange from threading import Semaphore, Thread import queue import time # For instructional purposes only. # globals You really don't want to use buffer_size = 10 last_item_consumed = False S = Semaphore(1) n = Semaphore(0) e = Semaphore(buffer_size) buffer = queue. Queue (buffer_size) def producer_func(): item = 31 done = False while not done: time. sleep(0.5) item += 1 e.acquire() s.acquire() append(item) s.release) n.release if item == Ox7E: done = True def append(i): buffer.put(i) output = "produce item print (output, flush=True) 11 + chr(i) + Itqueue size: + str(buffer.qsize()) def consumer_func(): done = False global last_item_consumed while not done: time. sleep(random_gen(500, 2000)) if last_item_consumed: break n.acquire() S.acquire() i = take()

s.release) e.release() if i == 126: done = True last_item_consumed = True def take(): i = buffer.get() output = "consume item print (output, flush=True) return i 11 + chr(i) + " \tqueue size: " + str(buffer.qsize()) def random_gen(min, max): return randrange (min, max) / 1000 def main(): th1 = Thread(target=producer_func) th2 = Thread(target=consumer_func) th3 = Thread(target=consumer_func) th1.start() th2.start() th3.start() th1.join() th2.join() th3.join() time. sleep(1) print("\n... \nEnd of program producerconsumer3 (py)") if == 11 _main_": _name main()
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply