I am study开发者_C百科ing for an exam and am having a difficult time understanding Rendezvous. Here's an example I'm looking a
While(1) {
select{
when a == TRUE :
accept A() {f1; b=FALSE}
when b == TRUE :
accept B() {f2; a=FALSE}
else {a=true; b=true}
}
}
The following calls arrive in the given order: A(), B(), B(), A(), A(), B()
In which order will the calls be accepted? And can caller of A or B starve?
I'd really appreciate any help. Thanks in advance.
That's not Ada. At all.
For some guidance on tasking with actual Ada read Chapter 14 of Ada Distilled.
And to be honest, if you didn't recognize your example as not Ada, you probably ought to start with Chapter 1.
Going with the logic of your problem rather than the syntax, I think the answer is 'it all depends'.
The task that's running this loop (call it Server
) is in a busy loop (most times round the loop, it'll end up setting A := True; B := True;
). This could use up all your CPU and starve the other tasks out.
Assuming that doesn't happen and you have 2 client tasks A_Caller
and B_Caller
which have higher priority than Server
and call their entries relatively infrequently, then you might get
A_Caller
andB_Caller
both run and call their respective entries.Server
enters theselect
and finds both guards open and the entries called; it chooses to acceptA
(it could have chosenB
). The guardB
is closed.- Next time round the loop, guard
A
is open but there is no caller; guardB
is closed; theelse
part is chosen, so guardB
is opened. - Next time round the loop,
Server
acceptsB
; guardA
is closed. - Next time round the loop, guard
B
is open but there is no caller; theelse
part is chosen, so guardA
is opened. - Next time round the loop, both guards are open but there is no caller, so the
else
part is chosen again and away we spin.
Obviously the exact sequence followed will depend on when the entry calls arrive with respect to Server
's loop. Assuming the entry calls in your question happen a second apart, the entries will be accepted in the order called.
I don't think you can get starvation unless one of the callers is running sufficiently frequently that it's called its entry again before Server
has got back round its loop. It would be hard to get this to happen under a general-purpose OS.
This does not look like ada syntax, Perhaps you could repost with ada code? Meanwhile i refer you to: http://en.wikibooks.org/wiki/Ada_Programming/Tasking#Rendezvous
精彩评论