Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Programming Project 2 1. Introduction In this project, you will write a program to simulate producer-consumer problem using bounded buffer whose size is 10. (this

image text in transcribed

image text in transcribed

Programming Project 2 1. Introduction In this project, you will write a program to simulate producer-consumer problem using bounded buffer whose size is 10. (this means the buffer only can store 10 messages.) For this, there will be one program creating two threads (one for producer and the other for consumer) which act as different processes. After 15 seconds of running, the program (both producer and consumer threads) should be terminated gracefully without any error. 2. Producer Thread Producer thread generates integers (014) which are treated as a message. The thread first checks if the buffer is full or not. At this moment, make sure the producer thread always waits random period of time (10100 milliseconds) before checking the buffer. This is to create randomness in the program execution. If the buffer is full, it waits another random period of time (10-100 milliseconds). If the buffer is not full, then it places an integer (014) in sequence into the buffer. "In sequence, herein, means first the producer places in the buffer. Next time, it produces 1, 2, 3, ... and so on until it produces 14 and places it in the buffer. Once the producer places 14 in the buffer, it produces 0 again. Every time producer thread places a message and integer) in the buffer, it makes a log in the file (say, producer.txt) in the following format (where current time is in millisecond using System.currentTimeMillis()): Producer "current time, Placing in the buffer location 0. "current time, Placing 1 in the buffer location 1. "current time, Placing 2 in the buffer location 2. "current time, Placing 9 in the buffer location 9. "current time, Placing 10 in the buffer location 0. "current time, Placing 11 in the buffer location 1. "current time", Placing 14 in the buffer location 4. "current time, Placing 0 in the buffer location 5. "current time, Placing 1 in the buffer location 6. "current time, Placing 2 in the buffer location 7. 3. Consumer Thread Consumer thread consumes integers (messages), 0-14, placed in the buffer by the producer thread. The consumer thread first checks if the buffer is empty or not. At this moment, make sure the consumer thread always waits random period of time (10-100 milliseconds) before checking the buffer. This is to create randomness in the program execution. If the buffer is empty, it waits another random period of time (10100 milliseconds). If the buffer is not empty, then it takes an 3. Consumer Thread Consumer thread consumes integers (messages), 0-14, placed in the buffer by the producer thread. The consumer thread first checks if the buffer is empty or not. At this moment, make sure the consumer thread always waits random period of time (10-100 milliseconds) before checking the buffer. This is to create randomness in the program execution. If the buffer is empty, it waits another random period of time (10-100 milliseconds). If the buffer is not empty, then it takes an integer (message), which is placed in the buffer at the earliest time, from the buffer. (based on the FIFO policy) Every time consumer thread consumes a message and integer) from the buffer, it makes a log in the file (say, consumer.txt) in the following format (where current time is in millisecond): Consumer "current time", Consuming 0 from the buffer location 0. "current time", Consuming 1 from the buffer location 1. "current time", Consuming 2 from the buffer location 2. "current time", Consuming 9 from the buffer location 9. "current time", Consuming 10 from the buffer location 0. "current time", Consuming 11 from the buffer location 1. "current time", Consuming 14 from the buffer location 4. "current time", Consuming 0 from the buffer location 5. "current time", Consuming 1 from the buffer location 6. "current time", Consuming 2 from the buffer location 7

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_2

Step: 3

blur-text-image_3

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

Securing SQL Server Protecting Your Database From Attackers

Authors: Denny Cherry

2nd Edition

1597499471, 978-1597499477

More Books

Students also viewed these Databases questions

Question

sharing of non-material benefits such as time and affection;

Answered: 1 week ago