Question: You are asked to implement a producer and a simple scheduler for N consumers. The producer and the consumers are represented as threads. The producer

You are asked to implement a producer and a simple scheduler for N consumers. The producer and the consumers are represented as threads. The producer thread produces  different integer numbers and put them in a queue. Design a simple scheduler that will handle the synchronization of the consumer threads. The scheduler should make sure that only one consumer at a time consumes the integer by printing it on the screen. The other consumers should sleep for some time while the thread consumer is consuming. When a thread wakes up, it tries to consume an integer from the queue. Make sure that all consumers get a chance to consume a number. No consumer should make the other ones starve. The producer should not stop producing numbers. 


Your program should have one command-line argument, which specifies the number N of threads. When the program starts, it creates N+1 threads that place themselves in a thread pool. The main thread issues the initial notification to the producer thread to start producing integers and then notify the scheduler to start doing its job as described above by ensuring that the N consumer threads have a chance to consume numbers.

Step by Step Solution

3.41 Rating (148 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

the implementation of the producer and scheduler for N consumers IN PYTHON import threading import time import queue def producerqueue while True num randomrandint1 100 queueputnum printfProducer Prod... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Operating System Questions!