Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, I'm suppose to write a program that involves making the use of threads and synchronization control. Background: Modern operating systems provide features for a

Hello, I'm suppose to write a program that involves making the use of threads and synchronization control.

Background: Modern operating systems provide features for a process to contain multiple threads of control and allow them to cooperate together. Manipulating shared data requires careful synchronization. Two widely used synchronization mechanisms are mutex lock (spinlock) and semaphore.

Programming Task: In the classic producer-consumer problem, a producer thread inserts items into a buffer, and a consumer thread consumes items from the buffer simultaneously. In this project, you are required to program in Java and use Java Threads to solve a M producer-N consumer problem by generating N producer threads and M consumer threads to simultaneously access the buffer.

In specific, the program should satisfy the folllowing requirements (and need to be explained using comments):

image text in transcribed

Of course, each record logs when and which thread produced an item into or consumed an item from an entry in the shared buffer.

(1) Your program should accept 4 input parameters, namely "the number of producer threads", "the number of consumer threads, 'the size of the buffer", and "the number of items to be produced". For example, the following command starts your program with 4 producer threads, 5 consumer threads, a buffer of 10 items, and generate 1000 items in total S/producer-consumer 4 5 10 1000 (2) Similar to the producer-consumer problem, a buffer is maintained in your program and shared among all producers and consumers. Each item in buffer is an integer (3) Each producer thread generates a random integer each time, inserts it into the buffer, increments a counter, num produced, which is shared by all producers to track how many items being created, and records the time and the item being produced in an event log. The event log should record the events in the order of timestamps (4) Each consumer thread consumes the items of the buffer in the First-In-First-Out (FIFO) order, meaning that if item A is inserted into the buffer earlier than item B, item A should be consumed earlier than item B, increments a counter, num consumed. which is shared by all consumers to track how many blocks are consumed, and record the time and the item being consumed in an event log. The event log should record the events in the order of timestamps (5) Use either semaphore or mutex lock (spinlock) to synchronize threads working on the shared resources n your program (6) When the specified number of items are produced and consumed, your program should exit and generate the following output: (i print the total number of produced items, (ii) print the total number of consumed items, and (iii) output the even log files, named "producer-event.log" and "consumder- event.log". Each record of the log file should be formatted as the following

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

Databases Illuminated

Authors: Catherine M Ricardo, Susan D Urban

3rd Edition

1284056945, 9781284056945

More Books

Students also viewed these Databases questions

Question

Explain the concept of employment at will.

Answered: 1 week ago