Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement the Producer-Consumer Problem programming assignment at the end of Chapter 5 in the textbook using the programming language Java instead of the described C

Implement the Producer-Consumer Problem programming assignment at the end of Chapter 5 in the textbook using the programming language Java instead of the described C code. You must use Java with Threads instead of Pthreads. A brief overview is below.

This program should work as follows: The user will enter on the command line the sleep time, number of producer threads, and the number of consumer threads. One example of the Java application from the command line could be java ProducerConsumer 20 10 1. The code must use threads, mutexes, and semaphores. Note that the number of producer and consumer threads does not have to be equal. The ProducerConsumer application will create the specified number of producer and consumer threads and then sleep letting both producer(s) and consumer(s) place 100 random integers into a bounded buffer that has a finite number of slots to hold data. Each producer and consumer thread will also choose a random amount of time to sleep using Random() (make sure to look up what kind of random number Random() provides) and Thread.sleep() that is less than the given sleep time as it places 100 random integers into this one bounded buffer. Set the sleep time of each producer and consumer thread to be a random amount of time between 0 to 0.5 seconds because we will test it over a 20-second period. Each producer and consumer thread will print the random number that it placed in the bounded buffer or removed from the bounded buffer to the screen. Set the bounded buffer equal to five.

The producer will 100 times go through the cycle of sleeping a random amount of time, creating a random integer, inserting it into the bounded buffer of size 5, and then printing the random integer.

The consumer will 100 times go through the cycle of sleeping a random amount of time, consuming an item from the bounded buffer, and then printing the random integer.

Use a mutex variable named mutex to protect the buffer and counting semaphores named empty and full so that a consumer does not try to remove an item from an empty buffer (empty) and a producer does not try to place an item into a full buffer (full). At the beginning initialize empty = 5 and full = 0 before creating the producer and consumer threads. Below is the usage and an example of executing this application.

Usage.

java ProducerConsumer

The following command creates 5 producer threads and 1 consumer thread and lets them run for 20 seconds. Notice the pattern!

$ java ProducerConsumer 20 5 1

Using arguments from command line

Sleep time = 20

Producer threads = 5

Producer threads = 1

Producer produced 6595

Consumer consumed 6595

Producer produced 97415

Consumer consumed 97415

Producer produced 50798

Producer produced 25626

Producer produced 95610

Producer produced 88100

Producer produced 14130

Consumer consumed 50798

Producer produced 95845

Consumer consumed 25626

Producer produced 71052

Consumer consumed 95610

Producer produced 64862

Consumer consumed 88100

Producer produced 26986

Consumer consumed 14130

Producer produced 17117

Consumer consumed 95845

Producer produced 33333

Consumer consumed 71052

Producer produced 28460

Consumer consumed 64862

Producer produced 11266

Consumer consumed 26986

Producer produced 46761

Consumer consumed 17117

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

Advances In Databases And Information Systems 22nd European Conference Adbis 2018 Budapest Hungary September 2 5 2018 Proceedings Lncs 11019

Authors: Andras Benczur ,Bernhard Thalheim ,Tomas Horvath

1st Edition

3319983970, 978-3319983974

More Books

Students also viewed these Databases questions

Question

=+What is the most that you should pay to complete development?

Answered: 1 week ago