Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python Redo the producer/consumer program so that it allows multiple consumers. Each consumer must be able to consume the same data before the producer produces

Python

Redo the producer/consumer program so that it allows multiple consumers. Each consumer must be able to consume the same data before the producer produces more data. these are the 2 consumer programs given not sure what to change.

""" File: producerconsumer1.py Producer-consumer demo with no synchronization. Producer and consumer both access shared data a given number of times. They sleep a random interval before each access. The data must be produced before it is consumed, and be produced and consumed just once. However, on some runs, the producer and consumer may access the data out of order. """

import time, random from threading import Thread, currentThread

class SharedCell(object): """Shared data for the producer/consumer problem.""" def __init__(self): """Data undefined at startup.""" self.data = -1

def setData(self, data): """Producer's method to write to shared data.""" print("%s setting data to %d" % \ (currentThread().getName(), data)) self.data = data

def getData(self): """Consumer's method to read from shared data.""" print("%s accessing data %d" % \ (currentThread().getName(), self.data)) return self.data

class Producer(Thread): """A producer of data in a shared cell."""

def __init__(self, cell, accessCount, sleepMax): """Create a producer with the given shared cell, number of accesses, and maximum sleep interval.""" Thread.__init__(self, name = "Producer") self.accessCount = accessCount self.cell = cell self.sleepMax = sleepMax

def run(self): """Resets the data in the cell and goes to sleep, the given number of times.""" print("%s starting up" % self.getName()) for count in range(self.accessCount): time.sleep(random.randint(1, self.sleepMax)) self.cell.setData(count + 1) print("%s is done producing " % self.getName())

class Consumer(Thread): """A consumer of data in a shared cell."""

def __init__(self, cell, accessCount, sleepMax): """Create a consumer with the given shared cell, number of accesses, and maximum sleep interval.""" Thread.__init__(self, name = "Consumer") self.accessCount = accessCount self.cell = cell self.sleepMax = sleepMax

def run(self): """Announce start-up, sleep and write to shared cell the given number of times, and announce completion.""" print("%s starting up " % self.getName()) for count in range(self.accessCount): time.sleep(random.randint(1, self.sleepMax)) value = self.cell.getData() print("%s is done consuming " % self.getName())

def main(): accessCount = int(input("Enter the number of accesses: ")) sleepMax = 4 cell = SharedCell() p = Producer(cell, accessCount, sleepMax) c = Consumer(cell, accessCount, sleepMax) print("Starting the threads") p.start() c.start()

main()

""" File: producerconsumer2.py Producer-consumer demo with synchronization. Producer and consumer both access shared data a given number of times. They sleep a random interval before each access. The data must be produced before it is consumed, and be produced and consumed just once. The condition and Boolean flag on the shared data guarantee that the producer and consumer access the data in the correct order. """

import time, random from threading import Thread, currentThread, Condition

class SharedCell(object): """Shared data that sequences writing before reading.""" def __init__(self): """Can produce but not consume at startup.""" self.data = -1 self.writeable = True self.condition = Condition()

def setData(self, data): """Second caller must wait until someone has consumed the data before resetting it.""" self.condition.acquire() while not self.writeable: self.condition.wait() print("%s setting data to %d" % \ (currentThread().getName(), data)) self.data = data self.writeable = False self.condition.notify() self.condition.release()

def getData(self): """Caller must wait until someone has produced the data before accessing it.""" self.condition.acquire() while self.writeable: self.condition.wait() print("%s accessing data %d" % \ (currentThread().getName(), self.data)) self.writeable = True self.condition.notify() self.condition.release() return self.data

class Producer(Thread): """A producer of data in a shared cell."""

def __init__(self, cell, accessCount, sleepInterval): Thread.__init__(self, name = "Producer") self.accessCount = accessCount self.cell = cell self.sleepInterval = sleepInterval

def run(self): """Resets the data in the cell and goes to sleep, the given number of times.""" print("%s starting up" % self.getName()) for count in range(self.accessCount): time.sleep(random.randint(1, self.sleepInterval)) self.cell.setData(count + 1) print("%s is done producing " % self.getName())

class Consumer(Thread): """A consumer of data in a shared cell."""

def __init__(self, cell, accessCount, sleepInterval): Thread.__init__(self, name = "Consumer") self.accessCount = accessCount self.cell = cell self.sleepInterval = sleepInterval

def run(self): """Accesses the data in the cell and goes to sleep, the given number of times.""" print("%s starting up " % self.getName()) for count in range(self.accessCount): time.sleep(random.randint(1, self.sleepInterval)) value = self.cell.getData() print("%s is done consuming " % self.getName())

def main(): accessCount = int(input("Enter the number of accesses: ")) cell = SharedCell() p = Producer(cell, accessCount, 4) c = Consumer(cell, accessCount, 4) print("Starting the threads") p.start() c.start()

if __name__ == "__main__": main()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Relational Database Design With Microcomputer Applications

Authors: Glenn A. Jackson

1st Edition

0137718411, 978-0137718412

Students also viewed these Databases questions

Question

* What is the importance of soil testing in civil engineering?

Answered: 1 week ago

Question

Explain the concept of shear force and bending moment in beams.

Answered: 1 week ago