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;
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;