Question: The following readerwriter algorithm works for multiple readers and a - single writer. It allows reading of the shared variables x 1 and x 2
The following readerwriter algorithm works for multiple readers and a
single writer. It allows reading of the shared variables xand xinto local
variables dand dwithout locking, thus not blocking the writer.
Before writing to the shared variables xand xthe writer increments a
counter cIt then proceeds to write to the variables, and finally increments
c again. The two increments of c ensure that it is odd when the process
is writing to the variables, and even otherwise. Hence, when a reader
wishes to read the shared variables, it waits in a loop until c is even before
reading them. Also, before returning it checks that the value of c has not
changed ieanother write has not begunIf it has changed, the process
starts over. This ensures the pair of values read corresponds to a single
occurrence of a write.
Nonblocking readerwriter
integer cxx
reader
writer
integer cdd
integer dd
loop forever
loop forever
p: repeat
q: dget
p: repeat
q: dget
p: cc
q: c c
p: until cmod
q: xd
p: dx
q: xd
p: dx
q: c c
p: until cc
q:
p: usedd
q:
b Extend the algorithm with a third type of process called an incre
menter. An incrementer increments the values of xand xieadds
to them. It is a low priority process and so should not block writers
while reading xand xand synchronisation mechanisms should give
preference to writers over incrementers.
Implement your algorithm from Question b in Java. You should have
reader threads, writer threads and incrementer threads. Each writer
and incrementer waits for a random time between and milliseconds
then updates the shared variables just once and terminates. The writers
can update the variables with random values. The readers wait a random
time between and milliseconds before each read. When all readers
have read the final update, the entire program should terminate gracefully,
ieall threads should reach the end of their run methods. Your program
should produce output by calling the appropriate methods of the provided
class AEvent.java. For testing purposes, it is a requirement that you call
the AEvent class every time one of the events occurs. In particular, the
writers and incrementers must call the AEvent class before another writer
or incrementer begins to update the results. It is also important that you
do not modify the provided class.
include parts
Java classes implementing your design
Appropriate use of synchronisation mechanisms
Program producing correct behaviour
readme file
Class AEvent
DO NOT MODIFY THIS CLASS
This class is used to print all events happening during the lifetime of your program
NOTE : Please make sure that you have called EVERY event method below in your main program
when ever such an event happens.
This class will be replaced with a similar class that will validate the program flow
automatically.
public class AEvent
Call this event method when a reader reads the current data
@param reader id value between and inclusive
@param xthe value of x read
@param xthe value for x read
public static void readDataint reader, int x int x
System.out.printlnReader #reader has read values: xx xx;
Call this event method when a writer writes to the data
@param writer id value between and inclusive
@param int xthe value written to x
@param int xthe value written to x
public static void writeDataint writer, int x int x
System.out.printlnWriter #writer has written values: xx xx;
Call this event method when an incrementer increments the data
@param incrementer id value between and inclusive
@param int xthe incremented value of x
@param int xthe incremented value of x
public static void incrementDataint incrementer, int x int x
System.out.printlnIncrementer #incrementer has incremented values: xx xx;
Call this event method when all readers have read the final result
public static void terminateReadWrite
System.out.printlnAll readers have read the final data.";
Class AEvent
DO NOT MODIFY THIS CLASS
This class is used to print all events happening during the lifetime of your program
NOTE : Please make sure that you have called EVERY event method below in your main program
when ever such an event happens.
This class will be replaced with a similar class that will validate the program flow
automatically.
public class AEvent
Call this event method when a reader reads the current data
@param reader id value between and inclusive
@param xthe val
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
