• Nenhum resultado encontrado

INTERLEAVING

No documento The CSP Approach (páginas 59-65)

Synchronization on common events provides one form of parallel execution of processes.

Concurrent execution of processes P1and P2where no such synchronization is required is described by the combination

P1jjjP2

lift:nozzle:

1

replace:nozzle:

1

depress:trigger:

1

release:trigger:

1

PUMP

1

Fig. 2.7 Transitions for PUMP1

pronounced ‘P1interleave P2’. In this process, the components P1and P2execute completely independently of each other, and do not interact on any events apart from termination. Each event is performed by exactly one process. The operational semantics rules are straightforward:

P1

?

! P01

[

6

=

X

]

P1 jjjP2

?

! P01 jjjP2

P2jjjP1 ?! P2jjjP01

P1 ?!X P01 P2 ?!X P02

P1jjjP2 ?!X P01 jjjP02

Unlike synchronous parallel, there is no event that both P1and P2engage in simultane-ously (except termination).

Example 2.14

A garage has two petrol pumps: PUMP

1

and PUMP

2

. The petrol supply service offered by the garage is described as

FORECOURT

=

PUMP

1

jjjPUMP

2

A customer will always interact with only one pump on any transaction; and the pumps operate independently. The description of PUMP

1

is a sequential process, defined by a mutual recursion.

PUMP

1 =

lift:nozzle:

1

!READY

1

READY

1 =

replace:nozzle:

1

!PUMP

1

2

depress:trigger:

1

!release:trigger:

1

!READY

1

The state graph corresponding to PUMP

1

is given in Figure 2.7.

The description of PUMP

2

is entirely similar:

PUMP

2 =

lift:nozzle:

2

!READY

2

READY

2 =

replace:nozzle:

2

!PUMP

2

2

depress:trigger:

2

!release:trigger:

2

!READY

2

r:t:

2

d:t:

2

l:n:

2

r:t:

2

l:n:

2

r:n:

2

r:t:

2

l:n:

2

l:n:

1

r:t:

1

d:t:

1

r:t:

1

d:t:

1

r:n:

1

r:n:

1

l:n:

1

r:n:

1

l:n:

1

r:t:

1

d:t:

1

d:t:

2

r:n:

2

r:n:

2

d:t:

2

PUMP

1

jjj

PUMP

2

Fig. 2.8 Transitions for PUMP1j jjPUMP2

The executions of the two pumps are unrelated. The process PUMP

1

will remain ready to engage in the event lift:nozzle:

1

until it occurs, independently of the progress that PUMP

2

makes. The state transitions for the combination of processes are given in Figure 2.8. 2

When two processes P1and P2have alphabets

(

P1

)

and

(

P2

)

that do not intersect, then their parallel combination P1 (P1

) k

(P2

) P2will behave the same as their interleaved combination P1jjjP2. Since they do not have any events in common to interact on, execution of each component process will be independent in both cases. Given the definitions of PUMP

1

and PUMP

2

an alternative definition of FORECOURT would be PUMP

1

kPUMP

2

.

However, for the purposes of design, use of thejj joperator describes the design decision to provide the FORECOURT service as an interleaving of two processes, before those processes are described any further.

Interleaved processes do not synchronize on events even when their alphabets do overlap.

In the case where both components are able to perform the same event, only one of the processes will actually engage in any particular occurrence of that event. In such a case, the environment which is interacting with the interleaved combination has no control or influence over which of the two processes actually performs it.

Example 2.15

A company that offers a telephone ordering service will operate a number of phone lines.

LINEn

=

ring!connect:n!order!disconnect!LINEn

A line operated by person n will repeatedly take calls as follows: the phone will firstly ring, then a connection will be made to n, then the order takes place, and finally the call is disconnected, and n is ready to receive the next order.

The service employs Chris and Sandy to run one line each, both connected to the same phone number, modelled by the fact that they each have the same event ring in their alphabet.

The service is described by

SERVICE

=

LINEchrisjjjLINEsandy

In this situation, only one of the phones will ring when a customer dials in. The customer has no control over which, and hence has no control over whether the next event in the call will be connect:chris or connect:sandy. A customer who wishes to talk only to Sandy may be disappointed.

There is an internal choice between the two components over which of them performs the event ring. The choice of which of the LINE processes takes the call is made internally by SERVICE. It cannot be made externally, by the customer, since initially there is only ring on offer; a choice cannot be resolved externally if there is only one event to choose. 2 The interleaving operator is appropriate for describing a number of identical resources which are all available for use.

Example 2.16

A fax machine may be described as FAX

=

accept

?

d

:

DOCUMENT!print

!

d!FAX

The machine is initially ready to accept any document. After accepting a document d, it prints d and reverts to its initial state.

A collection of four fax machines may be connected to the same phone number (with four lines): any of them is suitable for processing incoming faxes.

FAXES

= (

FAXjjjFAX

)

jjj

(

FAXjjjFAX

)

This system provides the facility for processing up to four incoming faxes at the same time.

It can accept up to four faxes before printing. An incoming fax has no influence over which machine is actually chosen to process it, but in this case this makes no difference since all

choices have exactly the same behaviour. 2

Dynamic process creation

Dynamic process creation can be modelled by use of an interleaving construction within a recursive loop. In general, a fresh copy of the entire process description is generated every time a recursive invocation occurs. Dynamic process creation occurs when such invocations take place while the parent process continues to execute. This may be set up in a recursive definition which contains a number of paths, some of which concern execution of the parent process, and where others contain recursive calls.

Example 2.17

A mail forwarder receives messages on channel in and forwards them along channel out at some later stage. It should always be willing to accept messages.

One approach to designing such a process would be to allow it to accept a message, and then create two processes: one to take responsibility for sending the message on, and the other to be a fresh copy of the original. This approach is described in CSP as follows:

NODE

=

in

?

x!

(

NODEjjjOUT

(

x

))

The process OUT

(

x

)

describes the part of the process which deals with sending x along channel out.

There are two views with respect to dynamic process creation. The parent process might be considered as the process N

=

in

?

x!N which is responsible for accepting inputs; and a child output process is generated every time an input occurs. Alternatively, the parent process might be considered as in

?

x!OUT

(

x

)

, where a fresh version of the entire process, ready to handle the next input, is generated every time an input occurs. Both of these viewpoints is consistent with the description above.

This structure is given before the process OUT

(

x

)

is defined, and there are different possibilities for this process definition.

The expectation may be that the thread described by OUT

(

x

)

should finish after x has been output. This would be described by

OUT

(

x

) =

out

!

x!SKIP

After the output has occurred, then this thread of execution will finish—there is nothing further that it is required to do. All further activity of the process will come from other parallel components. Observe that any process PjjjSKIP has the same executions as P, so a garbage collector could remove component processes that have finished without affecting the execution.

An alternative approach to defining the spawned thread might allow it to continue with a fresh version of NODE after its initial output. This possibility is described by

OUT

(

x

) =

out

!

x!NODE

so that after one input and its corresponding output the result is two interleaved versions of NODE. The number of active threads of control in this process will grow without limit, with a fresh thread generated on every input, and no thread ever finishing. Despite this proliferation, this implementation has no more executions than the previous tidier definition.

A different requirement might be that x should be logged as well as output. This could be achieved sequentially as out

!

x!log

!

x!SKIP, or alternatively by two interleaved threads

out

!

x!SKIPjjjlog

!

x!SKIP. 2

Example 2.18

A book shop operates a system whereby customers pay for books at a cashier’s counter and collect them at a different counter where they had previously been lodged. The operation of the book counter may be described as follows:

BOOK

=

lodge!issue chit!BOOK

2

receive receipt!claim!BOOK

A customer may lodge a chosen book with the counter, and have a chit issued in return. In order to claim the book to take away, a receipt must be provided. This may be obtained from the cashier, described as follows:

CASHIER

=

receive chit!payment!issue receipt!CASHIER

Book chits must be issued by the book counter before they can be received by the cashier:

CHIT

=

issue chit!

(

CHITjjjreceive chit!SKIP

)

Similarly, receipts must be issued before they can be exchanged for books:

RECEIPT

=

issue receipt!

(

RECEIPTjjjreceive receipt!SKIP

)

Each of these components imposes some constraint on the customer; and they are all in place together. The complete payment system which the customer must navigate is captured as the parallel combination

CASHIERkBOOKkCHITkRECEIPT

The first two processes represent the parts of the system which the customer interacts with.

The other two processes describe the relevant properties of the objects used by the customer and by the book shop: that chits and receipts can be created only by the book counter and the cashier respectively. The book shop’s payment system relies on the fact that chits and receipts

cannot be forged. 2

Indexed interleaving

The interleaving parallel operator is associative, in the sense that P1jjj

(

P2jjjP3

)

has exactly the same execution possibilities as

(

P1 jjj P2

)

jj j P3; and commutative, in the sense that P1 jjjP2and P2j jjP1have the same execution possibilities. It can therefore be generalized to finite combinations of processes. The generalization takes the form

jjji2IPi

where I is a finite set of indexes, and Piis defined for each i2I. An alternative way of writing an indexed interleaving in the special case where the indexing set is an interval of integers

fijm6i6ngis

jjj

n i=mPi

Example 2.19

A node similar to the NODE process of Example 2.17, but which can hold a maximum of n messages, could be described as an interleaved combination of n versions of COPY:

n NODE

=

jj j06i<nC

(

i

)

where each C

(

i

)

is defined to be COPY, for

0

6i<n. To describe an interleaved combination of n copies of the same process P there is a convenient shorthand

jjj

06i<nP

so an alternative description of n NODE would be given by n NODE

=

jj j06i<nCOPY

2

No documento The CSP Approach (páginas 59-65)