Concepts+Techniques+and+Models+of+Computer+Programming_Part15

Concepts+Techniques+and+Models+of+Computer+Programming_Part15

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 378 Message-Passing Concurrency fun {Timer} {NewPortObject2 proc {$ Msg} case Msg of starttimer(T Pid) then thread {Delay T} {Send Pid stoptimer} end end end } end fun {Controller Init} Tid={Timer} Cid={NewPortObject Init fun {$ Msg state(Motor F Lid)} case Motor of running then case Msg of stoptimer then {Send Lid ´ at ´ (F)} state(stopped F Lid) end stopped then case Msg of step(Dest) then if F==Dest then state(stopped F Lid) elseif F<Dest then {Send Tid starttimer(5000 Cid)} state(running F+1 Lid) else % F>Dest {Send Tid starttimer(5000 Cid)} state(running F-1 Lid) end end end end } in Cid end Figure 5.8: Implementation of the timer and controller components Copyright c 2001-3 by P. Van Roy and S. Haridi. All rights reserved. 5.4 Program design for concurrency 379 stoptimer / notcalled called doorsopen (Ack) arrive(Ack) / starttimer(5000 Fid) to Tid arrive(Ack) / call / call(F) to random Lid call / - arrive(A) / A=Ack call / - starttimer(5000 Fid) to Tid Ack=unit Figure 5.9: State diagram of a floor The lift Lifts are the most complicated of all. Figure 5.11 gives the state diagram of lift Lid. Each lift can be in one of four states: empty schedule and lift stopped (idle), nonempty schedule and lift moving past a given floor, waiting for doors when moving past a scheduled floor, and waiting for doors when idle at a called floor. The way to understand this figure is to trace through some execution scenarios. For example, here is a simple scenario. A user presses the call button at floor 1. The floor then sends call(1) to a lift. The lift receives this and sends step(1) to the controller. Say the lift is currently at floor 3. The controller sends ´ at ´ (2) to the lift, which then sends step(1) to the controller again. The controller sends ´ at ´ (1) to the lift, which then sends arrive(Ack) to floor 1 and waits until the floor acknowledges that it can leave. Each lift can receive a call(N) message and an ´ at ´ (N) message. The lift can send an arrive(Ack) message to a floor and a step(Dest) message to its controller. After sending the arrive(Ack) message, the lift waits until the floor acknowledges that the door actions have finished. The acknowledgement is done by using the dataflow variable Ack as a one-shot wire. The floor sends an acknowledgement by binding Ack= unit and the lift waits with {Wait Ack} . The source code of the lift component is shown in Figure 5.12. It uses a series of if statements to implement the conditions for the different transitions. It uses Browse to display when a lift will go to a called floor and when the lift arrives at a called floor. The function {ScheduleLast L N} implements the scheduler: it adds N to the end of the schedule L and returns the new schedule....
View Full Document

Page1 / 30

Concepts+Techniques+and+Models+of+Computer+Programming_Part15

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online