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 x1and x2into local
variables d1and d2without locking, thus not blocking the writer.
Before writing to the shared variables x1and x2,the writer increments a
counter c.It 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 i(.e.,another write has not begun).If 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 c,x1,x20
reader
writer
integer c0,d1,d2
integer d1,d2
loop forever
loop forever
p1: repeat
q1: d1get()
p2: repeat
q2: d2get()
p3: c0c
q3: c c+1
p4: until c(0mod 2=0)
q4: x1d1
p5: d1x1
q5: x2d2
p6: d2x2
q6: c c+1
p7: until c(0=c)
q7:
p8: used(1,d2)
q8:
1(b Extend the algorithm with a third type of process called an incre)
menter. An incrementer increments the values of x1and x2,i.e.,adds
11to them. It is a low priority process and so should not block writers
while reading x1and x2,and synchronisation mechanisms should give
preference to writers over incrementers.
Implement your algorithm from Question 1(b in Java. You should have)
5reader threads, 5writer threads and 5incrementer threads. Each writer
and incrementer waits for a random time between (0and 10milliseconds)
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 (0and 10milliseconds before each read. When all readers)
have read the final update, the entire program should terminate gracefully,
i.e.,all threads should reach the end of their run methods. Your program
should produce output by calling the appropriate methods of the provided
2class A2Event.java. For testing purposes, it is a requirement that you call
the A2Event class every time one of the events occurs. In particular, the
writers and incrementers must call the A2Event class before another writer
or incrementer begins to update the results. It is also important that you
do not modify the provided class.
include 4parts
Java classes implementing your design
Appropriate use of synchronisation mechanisms
Program producing correct behaviour
readme file
/**
* Class A2Event
* 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 A2Event {
/**
* Call this event method when a reader reads the current data
*
* @param reader id (value between 0 and 4 inclusive)
* @param x1(the value of x1 read)
* @param x2(the value for x2 read)
*
*/
public static void readData(int reader, int x1, int x2){
System.out.println("Reader #"+reader+" has read values: x1"+x1+" x2"+x2);
}
/**
* Call this event method when a writer writes to the data
*
* @param writer id (value between 0 and 4 inclusive)
* @param int x1(the value written to x1)
* @param int x2(the value written to x2)
*
*/
public static void writeData(int writer, int x1, int x2){
System.out.println("Writer #"+writer+" has written values: x1"+x1+" x2"+x2);
}
/**
* Call this event method when an incrementer increments the data
*
* @param incrementer id (value between 0 and 4 inclusive)
* @param int x1(the incremented value of x1)
* @param int x2(the incremented value of x2)
*
*/
public static void incrementData(int incrementer, int x1, int x2){
System.out.println("Incrementer #"+incrementer+" has incremented values: x1"+x1+" x2"+x2);
}
/**
* Call this event method when all 5 readers have read the final result
*
*/
public static void terminateReadWrite(){
System.out.println("All readers have read the final data.");
}
}```
* Class A2Event
* 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 A2Event {
/**
* Call this event method when a reader reads the current data
*
* @param reader id (value between 0 and 4 inclusive)
* @param x1(the val
The following readerwriter algorithm works for

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!