Title: Communicating Sequential Processes
1Communicating Sequential Processes
- Paper by C.A.R. Hoare - 1978
- Presentation by Chuck Ahern
2Introduction
- The traditional computer has been primarily for a
single sequential program (especially back in
1978!) - Desire for greater speed has led to the
introduction of parallelism (more true today) - Every attempt has been made to disguise
parallelism from the programmer (still true
today)
3Introduction
- A multiprocessor machine that can be easily
programmed to fully take advantage of this
architecture may become more powerful, efficient,
reliable, and economical than a uniprocessor or a
multiprocessor disguised as a monoprocessor
4Introduction
- In order to efficiently use such a machine
efficiently on a single task, the component
processors must be able to communicate and
synchronize with each other
5Topics
- 1. Commands
- Parallel Commands
- Assignment Commands
- I/O Commands
- Alternative and Repetitive Commands
- 2. Coroutines
- 3. Subroutines
- 4. Monitors and Scheduling
- 5. Miscellaneous extra examples
- 6. Conclusion
6Commands - structure
- An example ltprocess labelgt
ltemptygtlltidentifiergt Iltidentifiergt(ltlabel
subscriptgt,ltlabel subscriptgt) - consists of
- or
- indicate none or more repetitions of the
enclosed material - ? Input command
- ! Output command
- besides multiply, also used for loops
- used between an if, else statement
7Commands language style
- ltcommandgt ltsimple commandgtltstructured
commandgt - ltsimple commandgt ltnull commandgtltassignment
commandgtltinput commandgtltoutput commandgt - ltstructured commandgt ltalternative
commandgtltrepetitive commandgtltparallel commandgt - ltnull commandgt skip
- ltcommand listgt ltdeclarationgt ltcommandgt
ltcommandgt
8Commands parallel commands
- ltparallel commandgt ltprocessgt IIltprocessgt
- ltprocessgt ltprocess labelgt ltcommand listgt
- ltprocess labelgt ltemptygtlltidentifiergt
Iltidentifiergt(ltlabel subscriptgt,ltlabel
subscriptgt) - ltlabel subscriptgt ltinteger constantgtlltrangegt
- ltinteger constantgt ltnumeralgtlltbound variablegt
- ltbound variablegt ltidentifiergt
- ltrangegt ltbound variablegtltlower
boundgt..ltupper boundgt - ltlower boundgt ltinteger constantgt
- ltupper boundgt ltinteger constantgt
9ltparallel commandgt ltprocessgt IIltprocessgt
- Each process of a parallel command must be
disjoint from every other process of the command
(cannot mention any variable which occurs as a
target variable in any other process) - A parallel command specifies concurrent execution
of its constituent processes. They all start
simultaneously and the parallel command
terminates successfully only if and when they all
have successfully terminated.
10Parallel Command - examples
- cardreader?cardimage lineprinter!lineimage
- Performs two constituent commands in parallel
- Terminates when both are complete
- westDISASSEMBLE XSQUASH
eastASSEMBLE - The three processes have the names west, X,
and east. - Capitalized words are lists of commands
11Parallel Command - examples
- roomROOM fork(i0..4)FORK
phil(i0..4)PHIL - There are eleven processes
- The behavior of room is specified by the
command list ROOM - The behavior of fork(0), fork(1), fork(4) is
specified by the command list FORK, within which
the bound variable i indicates the identity of
the particular fork. - Similar remarks apply to the 5 phil processes
12Commands - Assignment Commands
- ltassignment commandgt lttarget variablegt
ltexpressiongt - ltexpressiongt ltsimple expressiongtlltstructured
expressiongt - ltstructured expressiongt ltconstructorgt(ltexpress
ion listgt) - ltconstructorgt ltidentifiergtlltemptygt
- ltexpression listgt ltemptygtlltexpressiongt,ltexpre
ssiongt - lttarget variablegt ltsimple variablegtlltstructure
d targetgt - ltstructured targetgt ltconstructorgt(lttarget
variable listgt) - lttarget variable listgt ltemptygtlttarget
variablegt,lttarget variablegt
13Assignment Command - examples
- (1) x x 1
- (2) (x, y) (y, x)
- (3) x cons(left, right)
- (4) cons(left, right) x
- (5) insert(n) insert(2x 1)
- (6) c P()
- (7) P() c
- (8) insert(n) has(n)
- the value of x after the assignment is the same
as the value of x 1 before. - exchanges the values of x and y.
- constructs a structured value and assigns it to
x. - fails if x does not have the form cons(y, z) but
if it does, then y is assigned to left, and z is
assigned to right. - equivalent to n 2x 1.
- assigns to c a "signal" with constructor P, and
no components. - fails if the value of c is not P() otherwise has
no effect. - fails, due to mismatch.
14Commands I/O commands
- ltinput commandgt ltsourcegt?lttarget variablegt
- ltoutput commandgt ltdestinationgt!ltexpressiongt
- ltsourcegt ltprocess namegt
- ltdestinationgt ltprocess namegt
- ltprocess namegt ltidentifiergtltidentifiergt(ltsubs
criptsgt) - ltsubscriptsgt ltinteger expressiongt,ltinteger
expressiongt
15I/O Commands
- I/O commands specify communication between two
concurrently operating sequential processes - These processes may be implemented in hardware as
a special-purpose device (e.g. cardreader or
lineprinter) - May be specified by one of the constituent
processes of a parallel command
16I/O Commands - Communication
- Communication between processes of a parallel
command occurs when - An input command in one process specifies as its
source the process name of the other process - An output command in the other process specifies
as its destination the process name of the first
process - The target variable of the input command matches
the value denoted by the expression of the output
command
17I/O Commands - Communication
- When all three conditions are met, the input and
output commands are said to correspond - Commands that correspond are executed
simultaneously, and their combined effect is to
assign the value of the expression of the output
command to the target variable of the input
command
18I/O Commands - Communication
- An input command fails if its source is
terminated - An output command fails if its destination is
terminated or if its expression is undefined - Synchronization if input and output means that
the implementation will have to delay whichever
of the commands that happens to be ready first.
(Can lead to deadlock or failures)
19I/O Commands - examples
- cardreader?cardimage
- lineprinter!lineimage
- X?(x, y)
- DIV!(3a b, 13)
- console(i)?c
- console(j-1)!"A"
- x(i)?V( )
- sem!P( )
- from cardreader, read a card and assign its value
(an array of characters) to the variable
cardimage - to lineprinter, send the value of lineimage for
printing - from process named X, input a pair of values and
assign them to x and y - to process DIV, output the two specified values.
- Note If a process named DIV issues command (3),
and a process named X issues command (4), these
are executed simultaneously, and have the same
effect as the assignment (x,y) (3a b, 13)
(mx3aby 13). - from the/th element of an array of consoles,
input a value and assign it to c - to the (j - l)th console, output character "A"
- from the/th of an array of processes X, input a
signal V( ) refuse to input any other signal - to sem output a signal P( )
20Commands alternative and repetitive commands
- ltrepetitive commandgt ltalternative commandgt
- ltalternative commandgt ltguarded commandgt
(ltguarded commandgt - ltguarded commandgt ltguardgt -gt, ltcommand listgt
(ltrangegt,ltrangegt)ltguardgt -gt ltcommand listgt - ltguardgt ltguard listgt ltinput commandgt
- ltguard listgt ltguard elementgt(ltguard
elementgt - ltguard elementgt ltbool expressiongt l
ltdeclarationgt
21Altern. Rep. Commands - examples
- x gt y -gt m x
- y gt x -gt m y
- If x gt y, assign x to m if y gt x assign y to
m if both x gt y and y gt x, either assignment
can be executed.
22Coroutines
- In parallel programming, coroutines appear as a
more fundamental program structure than
subroutines, which can be regarded as a special
case
23Coroutines - Copy
- Problem Write a process X to copy chars output
by process west to process east - Solution
- X ccharacter west ? c -gt east ! c
- Notes
- When west terminates, the input west?c will
fail, causing termination of the repetitive
command, and of process X. - Process X acts as a single-char buffer between
west and east. It permits west to work on
production of the next character before east is
ready to input the previous one.
24Coroutines - Disassemble
- Problem to read cards from a cardfile and output
to process X the stream of characters they
contain. An extra space should be inserted at
the end of each card. - Solution
- cardimage(1..80)character cardfile ?
cardimage -gt iinteger i 1 - i lt 80 -gt X!cardimage(i) i i 1
- X!space
25Coroutines - Assemble
- Problem To read a stream of characters from
process X and print them in lines of 125
characters on a lineprinter. The last line
should be completed with spaces if necessary. - Solution
- Lineimage(1..125)character
- iinteger i 1
- ccharacter X?c -gt
- lineimage(i) c
- i lt 124 -gt i i1
- i 125 -gt lineprinter ! Lineimage i
1 -
-
- i 1 -gt skip
- i gt 1 -gt I lt 125 -gt lineimage(i)
space i i 1 - lineprinter ! Lineimage
-
- Note When X terminates, so will the first
repetitive command of this process. The last
line will then be printed, if it has any
characters.
26Coroutines - Reformat
- Problem Read a sequence of 80 characters each,
and print the characters on a lineprinter with
125 characters per line. Every card should be
followed by an extra space, and the last line
should be completed with spaces if necessary. - Solution
- westDISASSEMBLEXCOPYeastASSEMBLE
- Notes
- The capitalized names stand for program text
defined previously - The parallel command is designed to terminate
after the cardfile has terminated - This elementary problem is difficult to solve
elegantly without coroutines
27Subroutines
- A conventional nonrecursive subroutine can be
readily implemented as a coroutine, provided
that - Its parameters are called by value and by
result - It is disjoint from its calling program
- A recursive subroutine can be simulated by an
array of processes, one for each level of
recursion. The user process is level zero. Each
activation communicates its parameters and
results with its predecessor and calls its
successor if necessary.
28Subroutines Division with Remainder
- Problem
- construct a process to represent a function-type
subroutine, which accepts a positive dividend and
divisor, and returns their integer quotient and
remainder. - Solution
- DIVx,yinteger X?(x,y) -gt
- quot, rem integer quot 0 rem x
- rem gt y -gt rem rem y quot quot
1 - X!(quot,rem)
-
- XUSER
29Monitors and Scheduling
- A monitor can be regarded as a single process
which communicates with more than one user
process. - However, each user process must have a different
name or a different subscript and each
communication with a user must identify its
source or destination uniquely.
30Monitors and Sched. - example
- Bounded Buffer
- Problem Construct a buffering process X to
smooth variations in the speed of output of
portions by a producer process and input by a
consumer process. The consumer contains pairs of
commands X!more( ) X?p, and the producer
contains commands of the form X!p. The buffer
should contain up to ten portions.
31Bounded Buffer - Solution
- X
- buffer(0..9) portion
- in,outinteger
- in 0 out 0
- comment 0 lt out lt in lt out 10
- in lt out 10
- producer?buffer(in mod 10) -gt in in 1
- out lt in consumer?more() -gt
consumer!buffer(out mod 10) - out out 1
-
- Notes
- When out lt in lt out10, the selection of the
alternative in the repetitive command will depend
on whether the producer produces before the
consumer consumes, or vice versa. - When out in, the buffer is empty and the second
alternative cannot be selected even if the
consumer is ready with its command X!more().
However, after the producer has produced its next
portion, the consumers request can be granted on
the next iteration. - Similar remarks apply to the producer, when in
out10. - X is designed to terminate when out in and the
producer is terminated
32Monitors and Sched. - example
- Integer Semaphore
- Problem to implement an array X(i1..100) of
client processes. Each process may increment the
semaphore by S!V( ) or decrement it by S!P( ),
but the latter command must be delayed if the
value of the semaphore is not positive.
33Integer Semaphore - Solution
- Svalinteger
- val 0
- (i1..100)X(i)?V( ) -gt val val1
- (i1..100)val gt 0 X(i)?P( ) -gt val val
1 -
- Notes
- In this process, no use is made of knowledge of
subscript i of the calling process. - The semaphore terminates only when all hundred
processes of the process array X have terminated.
34Dining Philosophers
- Five philosophers spend their lives thinking and
eating. The philosophers share a common dining
room where there is a circular table surrounded
by five chairs, each belonging to one
philosopher. In the center is a bowl of rice,
and the table has five chopsticks.
35Dining Philosophers
- On feeling hungry, a philosopher enters the room,
sits in his own chair, and picks up a chopstick
on the left of his plate. He then picks up the
right chopstick. When he has finished, he puts
down both chopsticks and leaves the room.
36Dining Philosophers
- The behavior of the ith philosopher may be
described as follows - PHIL during ith lifetime -gt
- THINK
- room!enter()
- chopstick(i)!pickup()
- chopstick((i1)mod5)!pickup()
- EAT
- chopstick(i)!putdown()
- chopstick((i1)mod5)!putdown()
- room!exit()
-
37Dining Philosophers
- The fate of the ith chopstick is to be picked up
and put down by a philosopher sitting on either
side of it - CHOPSTICK
- phil(i)?pickup() -gt phil(i)?putdown()
- phil((i-1)mod5)?pickup() -gt
phil((i-1)mod5)putdown() -
- The room can be expressed as
- ROOM occupancyinteger occupancy 0
- (i0..4)phil(i)?enter() -gt occupancy
occupancy 1 - (i0..4)phil(i)?exit() -gt occupancy
occupancy -1 -
38Dining Philosophers
- All these components can operate in parallel
-
- roomROOM
- chopstick(i0..4)CHOPSTICK
- phil(i0..4)PHIL
-
39Dining Philosophers
- Note
- This solution does not prevent all five
philosophers from entering the room, each picking
up a chopstick, and starving to death because he
cannot pick up his right chopstick. - To prevent this, you would have to modify the
code to prevent more than 4 philosophers from
entering the room at one time.
40Misc Examples
- Matrix Multiplication
- Problem A square matrix A of order 3 is given.
Three streams are to be input, each stream
representing a column of an array IN. Three
streams are to be output, each representing a
column of the product matrix IN x A. After an
initial delay, the results are to be produced at
the same rate as the input is consumed.
Consequently, a high degree of parallelism is
required.
41- Each of the 9 non-border nodes inputs a vector
component from the west and a partial sum from
the north. - Each node outputs the vector component to its
east, and an updated partial sum to the south. - The input data is produced by the west border
nodes, and the desired results are consumed by
the east border nodes. The north border node is
a constant source of 0s and the east border is
just a sink. - No provision need to be made for termination nor
for changing the values of array A.
42Matrix Mult - Solution
- There are 21 nodes, in 5 groups, comprising the
central square and the 4 borders - M(i1..3, 0)WEST
- M(0, j1..3)NORTH
- M(i1..3, 4)EAST
- M(4, j1..3)SOUTH
- M(i1..3, j1..3)CENTER
43Matrix Mult Solution (cont)
- The WEST and SOUTH borders are processes of the
user program the remaining processes are - NORTH true -gt M(1,j)!0
- EAST xreal M(i,3)?x -gt skip
- CENTER xreal M(i,j-1)?x -gt
- M(i,j1)!x sumreal
- M(i-1,j)?sum M(i1,j)!(A(i,j)x sum)
44Conclusion
- This paper has suggested that input, output, and
concurrency should be regarded as primitives for
programming, which underlie many familiar and
less-familiar programming concepts.
45Conclusion
- 1. Commands
- Parallel Commands
- Assignment Commands
- I/O Commands
- Alternative and Repetitive Commands
- 2. Coroutines
- 3. Subroutines
- 4. Monitors and Scheduling
46Source
- Hoare, C.A.R. Communicating Sequential Processes.
Communications of the ACM 21, 8. (August 1978),
666-677.