Question

Redo the produce/consumer program so that it allows multiple consumers. The program will prompt the user...

Redo the produce/consumer program so that it allows multiple consumers. The program will prompt the user for the number of consumers. Each consumer must be able to consume the same data before the producer produces more data. The program will prompt the user for the number of accesses (data items produced and consumed). The program will contain three Classes, SharedCell, Consumer, and Producer. Use the code presented in section 1 of the textbook as a starting point. Use the updated SharedCell code on page 408.

Run the program with the following data:

Run 1 – Consumers = 4; Accesses =4

Run 2 – Consumers =6, accesses = 2

Run 3 – Consumers = 4; Accesses = 8

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below is your code

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\n" % 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\n" % self.getName())
for count in range(self.accessCount):
time.sleep(random.randint(1, self.sleepInterval))
value = self.cell.getData()
print("%s is done consuming\n" % 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()

main()

indentation

import time, random from threading import Thread, currentThread, Condition class SharedCell (object) Shared data that sequendef getData (self) Caller must wait until someone has produced the data before accessing it. self.condition.acquire ) whileclass Consumer (Thread) A consumer of data in a shared cell. def init (self, cell, accessCount, sleepInterval) Thread . ini

Add a comment
Know the answer?
Add Answer to:
Redo the produce/consumer program so that it allows multiple consumers. The program will prompt the user...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Producer and Consumer Code in C++ The program will contain two methods- one each for the...

    Producer and Consumer Code in C++ The program will contain two methods- one each for the producer and consumer. Both methods share access to an integer array buffer of size 50 with all values initialized to 0 at the beginning and an integer variable counter initialized to 0. As given below, the producer accesses the buffer elements and updates the element to 1 ( indicating production). The consumer changes a buffer elements to 0 (indicating consumption). Enhancements and modifications: You...

  • write a C/C++ program which simulates the Producer/Consumer program in Figure 5.16 with a buffer size...

    write a C/C++ program which simulates the Producer/Consumer program in Figure 5.16 with a buffer size of one thousand. Allow the Producer to generate one million items. Use ten consumers. The program needs to perform a normal exit process after all items are consumed. Both the Producer (singular) and Consumers are to be runs as separate processes generated via fork(). The program must us Linux semaphores. The program must clean up the semaphores used and zombies created before termination. Report...

  • C++ Loops homework Prompt the user for a desired password, input the password. Your program may...

    C++ Loops homework Prompt the user for a desired password, input the password. Your program may assume without checking that there is no input failure and that the password contains no white space. Let's say the rules for a legal password are: # chars must be in [4, 8] # digs must be >= 2 The password must contain at least one letter of each case The password must contain at least one char that's not a letter or digit...

  • Write a PYTHON program that allows the user to navigate the lines of text in a...

    Write a PYTHON program that allows the user to navigate the lines of text in a file. The program should prompt the user for a filename and input the lines of text into a list. The program then enters a loop in which it prints the number of lines in the file and prompts the user for a line number. Actual line numbers range from 1 to the number of lines in the file. If the input is 0, the...

  • Code in C++ Modify “Producer and Consumer Problem” from lecture note so that it can use...

    Code in C++ Modify “Producer and Consumer Problem” from lecture note so that it can use all buffer space, not “buffersize – 1” as in the lecture note. This program should work as follows: 1.The user will run the program and will enter two numbers on the command line. Those numbers will be used for buffersize and counter limit. 2. The program will then create a separate threads, producer and consumer thread. 3. Producer thread generates a random number through...

  • need help please! thanks!! C++ programming Your program should do the following Prompt the user for...

    need help please! thanks!! C++ programming Your program should do the following Prompt the user for the first number (int data type) Read the first number Prompt the user for the second number (int data type) Read the second number Write code to calculate the quotient and remainder when the first number is divided by the second number Make sure to used variables in your output statements. /* OUTPUT Enter the first number: 126 Enter the second number: 31 126...

  • Can someone help create this program for Linux. Part 3. IPC (InterProcess Communication) Programming In Part...

    Can someone help create this program for Linux. Part 3. IPC (InterProcess Communication) Programming In Part 3, you are asked to develop a multithreaded program and a Makefile to automate the compilation on Linux platform. This assignment assists you for better understanding of processes and threads management, multithreaded programming, and enhancing programming skills and experience with programming on a Unix-like environment. You are asked to implement a multithreaded producer-consumer problem with PThreads library in this programming assignment. The producer-consumer is...

  • in Python: Write a program that allows the user to enter a number between 1-5 and...

    in Python: Write a program that allows the user to enter a number between 1-5 and returns an animal fact based on what the user entered. The program should 1. Continue to run until the user enters “quit” to terminate the program, and 2. Validate the user’s input (the only acceptable input is 1, 2, 3, 4, 5 or “quit”). You can use the following animal facts: An octopus is a highly intelligent invertebrate and a master of disguise. Elephants...

  • I need to creatre a C++ program that can modify the producer and consumer. My main...

    I need to creatre a C++ program that can modify the producer and consumer. My main programmig language is C++. Modify "Producer and Consumer Problem" from lecture note so that it can use all buffer space, not "buffersize – 1" as in the lecture note. This program should work as follows: 1. The user will run the program and will enter two numbers on the command line. Those numbers will be used for buffersize and counter limit. 2. The main...

  • Write a PYTHON program that reads a file (prompt user for the input file name) containing...

    Write a PYTHON program that reads a file (prompt user for the input file name) containing two columns of floating-point numbers (Use split). Print the average of each column. Use the following data forthe input file: 1   0.5 2   0.5 3   0.5 4   0.5 The output should be:    The averages are 2.50 and 0.5. a)   Your code with comments b)   A screenshot of the execution Version 3.7.2

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT