Reader Writer monitor in JAVA

S

Spliff Monkey

I am trying to program a simple monitor for a multiple reader single
writer problem in JAVA based on the pseudo code given at the bottom of
this post. A link to my code is also given bellow.

I get deadlock. I basically have producer and consumer classes which
do nothing except call the Start_Read/Write and End_Read/Write from
the monitor. I also have created a condition class.

It seems as if, say, a consumer finishes its synchronized start_read.
Then, say, a producer tries to write but is told to wait. Then the
consumer just stops and never enters its end_read.

The consumer should enter the end_read and then signal the producer.

Does the producer waiting in the synchronized start_write, prevent the
consumer entering the synchronized end_read?

If so how do I get around it.

My code is here:

http://www.maths.tcd.ie/~z/MainClass.java

Based in the following pseudo code for a monitor:

monitor Reader_Writer_Monitor is
Readers: Integer := 0;
Writing: Boolean := False;
OK_to_Read, OK_to_Write: Condition;

procedure Start_Read is
begin
if Writing or Non_Empty(OK_to_Write) then
Wait(OK_to_Read);
end if;
Readers := Readers + 1;
Signal(OK_to_Read);
end Start_Read;

procedure End_Read is
begin
Readers := Readers - 1;
if Readers = 0 then Signal(OK_to_Write); end if;
end End_Read;

procedure Start_Write is
begin
if Readers /= 0 or Writing then
Wait(OK_to_Write);
end if;
Writing := True;
end Start_Write;

procedure End_Write is
begin
Writing := False;
if Non_Empty(OK_to_Read) then
Signal(OK_to_Read);
else
Signal(OK_to_Write);
end if;
end End_Write;
end Reader_Writer_Monitor;
 
A

A. Bolmarcich

I am trying to program a simple monitor for a multiple reader single
writer problem in JAVA based on the pseudo code given at the bottom of
this post. A link to my code is also given bellow.

I get deadlock. I basically have producer and consumer classes which
do nothing except call the Start_Read/Write and End_Read/Write from
the monitor. I also have created a condition class.

It seems as if, say, a consumer finishes its synchronized start_read.
Then, say, a producer tries to write but is told to wait. Then the
consumer just stops and never enters its end_read.

The consumer should enter the end_read and then signal the producer.

Does the producer waiting in the synchronized start_write, prevent the
consumer entering the synchronized end_read?

Yes. In the sequence of events that you described, the Start_Read()

is synchronized on its Monitor object
invokes OK_to_Read.wait_() which
is synchronized on its Condition object (OK_to_Read)
invokes wait on its Condition object

This wait releases the synchronization lock on the Condition object; it
does not also release any other synchronization locks the thead has.
If so how do I get around it.

You get around it by redesigning your solution so it does not deadlock.

In your code, your probably do not want the Start_Read method to use
the statement

readers +=readers;
 
S

Spliff Monkey

This wait releases the synchronization lock on the Condition object; it
does not also release any other synchronization locks the thead has.
How do I get it so that when the producer waits it releases the lock
so that end_read may execute?

In your code, your probably do not want the Start_Read method to use
the statement

readers +=readers;

Yeah, that should be readers+=1;

Thank you for your time
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top