[Cst-1b] CST1-b Question: Java 98/4/3 (solution)
Martin Harper
mcnh2@cam.ac.uk
Thu, 04 May 2000 00:47:42 +0100
"M.Y.W.Y.B." wrote:
> CST 98/4/3
> http://www.cl.cam.ac.uk/tripos/y1998p4q3.pdf
>
> Hi!
>
> Has anyone solved the last part of the question in 98/4/3?
> "Show how a synchronised buffer holding a single value could be implemented
> using this new scheme".
>
> I assume that you are not allowed to use "synchronized", "wait()",
> "notify()" etc but only TicketMachine and Scheduler methods. But how?
>
> Thanks
>
> Moritz
Previous response was sent to personal email by accident - but the gist of it
was
1) you can create critical regions (from the question)
2) hence you can create semaphores (from notes)
3) hence you can build a shared 1-slot buffer (from notes)
This is something of a clumsy answer, however - and I'm not sure if it would
lose marks for that. Something better can be done.
class Buffer {
// value of the buffer
int val;
// queue of people who want to read from the buffer
TicketMachine rm = ..
Scheduler rs = ...
// queue of people who want to write to the buffer
TicketMachine wm = ...
Scheduler ws = ...
void store( int i ) {
int writernum = wm.turn(); // we have to assume that this is an
atomic operation...
ws.queue(writernum); // queue up to write.
val = i;
rs.next(); // please would the next reader move along to be
served...
}
int read() {
int readernum = rm.turn();
rs.queue(readernum);
int temp = val;
rs.next();
return temp;
}
}
Martin.