Limited Offer Get 25% off — use code BESTW25
No AI No Plagiarism On-Time Delivery Free Revisions
Claim Now
Prices start at $5 per page · plagiarism-free papers · free revisions for 14 days 24/7 support: support@bestwriters.com
Uncategorized

Producer Consumer Assignment

  • Posted
  • Authoradmin
  • Length843 words
  • Reading time4 min
Write My Assignment FREE

Page 1 of 4
Producer Consumer Assignment
Design
1. You will implement Java code for the bounded buffer problem.
2. The buffer is a large array of n integers, initialized to all zeros.
3. The producer and the consumer are separate concurrent threads in your program.
4. The producer executes short bursts of random duration. During each burst of length k1, the producer adds a
1 to the next k1 slots of the buffer, modulo n.
5. The consumer also executes short bursts of random duration. During each burst of length k2, the consumer
reads the next k2 slots, modulo n, and resets each to 0.
6. If any slot contains a number greater than 1, then a race condition has been detected: the consumer was unable
to keep up and thus the producer has added a 1 to a slot that has not yet been reset.
7. Both producer and consumer sleep periodically for random time intervals to emulate unpredictable execution
speeds.
8. The producer thread pseudo code: while (true)
generate random integer k1 using
k.
for i from 0 to (k1 – 1)
buffer[(next_in + i) mod n]
+= 1
next_in = (next_in + k1) mod n
generate random integer t1 using t
sleep for t1 seconds

9. The consumer thread pseudo code: while (true)

generate random integer t2 using t
sleep for t2 seconds
generate random integer k2 using k
for i from 0 to (k2 – 1)

Page 2 of 4
data = buffer[(next_out + i) mod n] if
(data > 1) exit and report race condition
buffer[(next_out + i) mod n] = 0

next_out = (next_out + k2) mod n

10. A thread which executes a call to the Thread.sleep(m) method sleeps, does nothing, for m milliseconds. Since
the values t1 and t2 are the number of seconds to put a thread to sleep then that value must be multiplied by
1,000 before passing it as the parameter to the Thread.sleep method.
11. Create a driver class and make the name of the driver class Assignment2 containing only one method:
public static void main(String args[]).
The main method receives, via the command line arguments, the values for n, k, and t, in that order. The main
method creates and initializes the buffer array, then creates the producer and consumer objects, then starts the
producer and consumer objects, and then executes a join on the producer and the consumer objects. Executing
a join on a thread object is done by calling its join() method.
I compile and run your program via the command line using the Java JDK. Therefore, the command I type
to execute your program is java Assignment1. I will test your program with my own values for n, k, and t.
For example, if n = 100, k = 22, and t = 60 then the command to run the program is:
java Assignment1 100 22 60
12. Experiment with different values of n, k, and t until you observe a race condition. Reporting the race condition
means the consumer prints the message that a race condition occurred. Your program will not terminate on
its own. You will terminate it yourself when you experiment with the different values of n, k, and t. If, after
a reasonable amount of time, your program does not print the race condition message, then just stop your
program. If your program does print the race condition message, then just stop your program. When you find
a combination of values for n, k, and t that cause a race condition, add a comment to the top of your
Assignment2.java file listing those values. It will be at this point that you can submit your assignment.
13. You must declare public each class you create which means you define each class in its own file.
14. You must declare private all the data members in every class you create.
15. You can use “implements Runnable” or “extends Thread” to implement the classes defining the threads in
this assignment. You cannot use extends in this assignment in defining any class except if you are using
“extends Thread” to define a thread class.
16. Tip: Make your program as modular as possible, not placing all your code in one .java file. You can create
as many classes as you need in addition to the classes described above. Methods being reasonably small
follow the guidance that “A function does one thing and does it well.” You will lose a lot of points for code
Page 3 of 4
readability if you don’t make your program as modular as possible. But, do not go overboard on creating
classes and methods. Your common sense guides your creation of classes and methods.
17. Do NOT use your own packages in your program. If you see the keyword package on the top line of any of
your .java files then you created a package. Create every .java file in the src folder of your Eclipse project, if
you’re using Eclipse.
18. Do NOT use any graphical user interface code in your program!
19. Do NOT type any comments in your program except as stated in number 12 above. If you do a good job of
programming by following the advice in number 16 above then it will be easy for me to determine the task of
your code.
Page 4 of 4

The post Producer Consumer Assignment appeared first on My Assignment Online.

Plagiarism Free Assignment Help

Expert Help With This Assignment — On Your Terms

✓ Native UK, USA & Australia writers ✓ Deadline from 3 hours ✓ 100% Plagiarism-Free — Turnitin included ✓ Unlimited free revisions ✓ Free to submit — compare quotes

admin

Academic writer and editor at BestWriters.

Get your paper off your plate tonight

Post your instructions for free. Prices start at $5 per page.

Place an order