Upload
maegan
View
24
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Chapter 8 Channels. Concurrent Programming Constructs. So far we have seen contructs based on shared memory concept (shared directly – buffer - or through a shared service of an OS – semaphores and monitors) - PowerPoint PPT Presentation
Citation preview
1
Chapter 8Chapter 8
ChannelsChannels
2
Concurrent Programming Concurrent Programming ConstructsConstructs
So far we have seen contructs based on So far we have seen contructs based on shared shared memorymemory concept (shared directly – buffer - or concept (shared directly – buffer - or through a shared service of an OS – semaphores through a shared service of an OS – semaphores and monitors)and monitors)
Now, we will see constructs based on Now, we will see constructs based on communications communications (processes send and receive (processes send and receive messages) which may bemessages) which may be SynchronousSynchronous in which the exchange of message is an in which the exchange of message is an
atomic operation requiring participation of both the atomic operation requiring participation of both the sender and the receiver (receiver is blocked is message sender and the receiver (receiver is blocked is message is not send yet or sender is blocked if receiver is not is not send yet or sender is blocked if receiver is not ready yetready yet
AsynchronousAsynchronous in which messages are saved somewhere in which messages are saved somewhere and sender nor receiver is blocked and sender nor receiver is blocked
This chapter is on synchronous communication This chapter is on synchronous communication methods (channelsmethods (channels and ADA and ADA rendezvous)rendezvous)
3
ChannelsChannels
A channel connects a sending process A channel connects a sending process with a receiving processwith a receiving process
Channels are typed, meaning that they Channels are typed, meaning that they must be declaredmust be declared
The exchange of messages is The exchange of messages is synchronous, that is sender and receiver synchronous, that is sender and receiver have to wait for each otherhave to wait for each other
Channels in operating systems are called Channels in operating systems are called pipespipes
4
Producer - Consumer Using a Producer - Consumer Using a ChannelChannel
At p2 and q1 both processes must wait At p2 and q1 both processes must wait for each otherfor each other
5
Conway’s ProblemConway’s Problem
inCinC is the input channel for a sequence of is the input channel for a sequence of characters send by some processcharacters send by some process
outCoutC is the output channel for a sequence of is the output channel for a sequence of transformed characters to be received by some transformed characters to be received by some other processother process
TransformationTransformation 2 ≤ 2 ≤ nn ≤ 9 occurrences of the same character is ≤ 9 occurrences of the same character is
replaced by replaced by nn and the character ( and the character (compresscompress process) process) A newline character is appended following every A newline character is appended following every K’thK’th
character (character (outputoutput process) process) PipePipe is the channel connecting is the channel connecting compresscompress to to outputoutput
6
Conway’s ProblemConway’s Problem
7
Transputer and OCCAM Transputer and OCCAM LanguageLanguage
The transputer was a microprocessor The transputer was a microprocessor developed by Inmos Corporation to support developed by Inmos Corporation to support the OCCAM programming model. the OCCAM programming model.
The IMS 800 transputer contained a fast 32-The IMS 800 transputer contained a fast 32-bit processor, a floating point processor, a bit processor, a floating point processor, a small amount of on-chip memory (4K) and small amount of on-chip memory (4K) and four bi-directional high-speed serial four bi-directional high-speed serial communication links. communication links.
Each of these communication links implement Each of these communication links implement a pair of occam channels (for example, north, a pair of occam channels (for example, north, east, south and west). east, south and west).
OCCAM was a parallel computation language OCCAM was a parallel computation language designed for the transputer architecturesdesigned for the transputer architectures
8
IMS 800 Transputer IMS 800 Transputer ArchitectureArchitecture
x
External MemoryInterface
9
A Matrix Multiplication A Matrix Multiplication ExampleExample To multiply two matrices A * X, a process is allocated for To multiply two matrices A * X, a process is allocated for
each A[i,j]. each A[i,j]. The elements of X are fed from the top (an input channel)The elements of X are fed from the top (an input channel) The partial sums are passed from right to left until they The partial sums are passed from right to left until they
emerge from the array of processes with the final result emerge from the array of processes with the final result At the right, a set of processes feed in zeros to initialize At the right, a set of processes feed in zeros to initialize
the sumsthe sums At the bottom, sink processes absorb the elements of X At the bottom, sink processes absorb the elements of X
as they leave the bottom row. This is necessary since as they leave the bottom row. This is necessary since matrix element processes are identical and need not to matrix element processes are identical and need not to know if they are at a particular edge of the arrayknow if they are at a particular edge of the array
Matrices:Matrices:1 2 31 2 3 1 0 1 0 22 4 2 64 2 64 5 64 5 6 x 0 1 x 0 1 22 = = 10 5 1810 5 187 8 97 8 9 1 0 1 0 00 16 8 16 8 3030
10
Computation of One Computation of One ElementElement
22
7 8 9 x 2 = 7x2+8x2+9x0 = 14+16+0 7 8 9 x 2 = 7x2+8x2+9x0 = 14+16+0 = 30= 30
00
11
Processor Array for Processor Array for MultiplicationMultiplication
12
Multiplier Processes Multiplier Processes (one for each A[i,j] .. 9 in total)(one for each A[i,j] .. 9 in total)
13
Occam SyntaxOccam Syntax Every statement in occam is a process. It is up to the Every statement in occam is a process. It is up to the
programmer to indicate whether the statements will be programmer to indicate whether the statements will be combined in sequence or parallel. For example,combined in sequence or parallel. For example,
SEQSEQstatement_1statement_1statement_2statement_2statement_3statement_3
PARPARstatement_1statement_1statement_2statement_2statement_3statement_3
Indentation is used instead of punctuation (';') to show Indentation is used instead of punctuation (';') to show program structure. Input and output statements have the formprogram structure. Input and output statements have the form
channel ? variablechannel ? variablechannel ! value channel ! value
respectively.respectively.
14
Multiplier Process in Occam Multiplier Process in Occam SyntaxSyntax
WHILE TRUEWHILE TRUE
SEQSEQ
north ? xnorth ? x
south ! xsouth ! x
east ? sumeast ? sum
sum := sum + a * xsum := sum + a * x
west ! sumwest ! sum
15
Multiplier Process Made More Multiplier Process Made More ParalelParalel
north ? x -- read first xnorth ? x -- read first xWHILE TRUEWHILE TRUE
SEQSEQPARPAR
south ! xsouth ! xeast ? sumeast ? sumtemp := a * xtemp := a * x
PARPARwest ! sum + tempwest ! sum + tempnorth ? Xnorth ? X
After reading the x value, transferring value to south, reading After reading the x value, transferring value to south, reading the partial sum from east and multiplication of a*x can be the partial sum from east and multiplication of a*x can be done in parallel. Similarly, transferring new sum and reading done in parallel. Similarly, transferring new sum and reading next x value can be done in parallel after waiting for the first next x value can be done in parallel after waiting for the first set of parallel computations.set of parallel computations.
16
The Dining Philosophers with The Dining Philosophers with ChannelsChannels
A A forkfork is a process which is connected by a channel to the philosopher is a process which is connected by a channel to the philosopher on its left and righton its left and right
Before eating a philosopher waits for an input from the left and right Before eating a philosopher waits for an input from the left and right channels (value is not important – but both forks must be available) in channels (value is not important – but both forks must be available) in p2p2 and and p3p3
A fork process sends a “true” value down the channel when the fork is A fork process sends a “true” value down the channel when the fork is free (free (q1q1) and then waits for the fork to become free again after eating ) and then waits for the fork to become free again after eating ((q2q2))
The philosopher releases the forks after eating in steps The philosopher releases the forks after eating in steps p5p5 and and p6p6
17
Rendezvous in ADARendezvous in ADA Communication in ADA is synchronous and Communication in ADA is synchronous and
unbufferedunbuffered Two processes (tasks in ADA) must meet in a Two processes (tasks in ADA) must meet in a
rendezvous in order to communicate. This means rendezvous in order to communicate. This means one of the two processes must wait for the other one of the two processes must wait for the other to come to the rendezvous pointto come to the rendezvous point
One of the two processes is called the One of the two processes is called the callingcalling task and the other as an task and the other as an acceptingaccepting task task The The callingcalling task must know the task must know the identity of the identity of the
accepting taskaccepting task and the name of the and the name of the rendezvous location called the rendezvous location called the entryentry
The The acceptingaccepting task does not know the identity task does not know the identity of the calling taskof the calling task
18
ADA TasksADA Tasks
An ADA task has two sections:An ADA task has two sections: A A specificationspecification (declaration) section (declaration) section
contains the declarations of the contains the declarations of the entries (their names)entries (their names)
The The bodybody (code) section contains the (code) section contains the entry points and the code to be entry points and the code to be executed in the rendezvousexecuted in the rendezvous
19
Comparison of Communication Comparison of Communication MethodsMethods
ADA WayADA Way Other MethodsOther Methods
ConsumerProducer
BufferProcess
ConsumerProducer
Buffer Data Structure
20
Producer-Consumer With a Degenerate Producer-Consumer With a Degenerate Bounded BufferBounded Buffer
(Task Specification)(Task Specification)
task buffer istask buffer isentry append(i: in integer);entry append(i: in integer);entryentry take(i: out integer);take(i: out integer);
end buffer;end buffer;
The above task specification declares The above task specification declares two entry points (append, take) for the two entry points (append, take) for the accepting taskaccepting task
21
Producer-Consumer With a Degenerate Producer-Consumer With a Degenerate Bounded BufferBounded Buffer
(Task Body)(Task Body)task body buffer istask body buffer is
bufbuf :array(0..n-1) of integer;:array(0..n-1) of integer;in_ptr, out_ptrin_ptr, out_ptr :integer:= 0;:integer:= 0;countcount :integer:= 0;:integer:= 0;
beginbeginlooploop
accept append(i: in integer) doaccept append(i: in integer) dobuf(in_ptr) := i;buf(in_ptr) := i;
end append;end append;
count:= count+1;count:= count+1;in_ptr := (in_ptr+1) mod N;in_ptr := (in_ptr+1) mod N;
accept take(i : out integer) doaccept take(i : out integer) doi:= buf(out_ptr);i:= buf(out_ptr);
end take;end take;
count:= count-1;count:= count-1;out_ptr:= (out_ptr+1) mod N;out_ptr:= (out_ptr+1) mod N;
end loop;end loop;end buffer;end buffer;
22
Producer and Consumer Producer and Consumer ProcessesProcesses
procedure producer;procedure producer;var ivar i :integer;:integer;beginbegin
repeatrepeatproduce(i);produce(i);buffer.append(i);buffer.append(i);
until falseuntil falseend;end;procedure consumer;procedure consumer;var ivar i :integer;:integer;beginbegin
repeatrepeatbuffer.take(i);buffer.take(i);consume(i);consume(i);
until falseuntil falseend;end;
23
CommentsComments
Producer and consumer processes interact Producer and consumer processes interact with the buffer task using the entry points with the buffer task using the entry points buffer.append(i)buffer.append(i) and and buffer.take(i)buffer.take(i)
Producer, consumer and buffer tasks Producer, consumer and buffer tasks execute concurrently. Sometime later, one of execute concurrently. Sometime later, one of the processes, producer or consumer, try to the processes, producer or consumer, try to make a rendezvous with the buffer task by make a rendezvous with the buffer task by calling the appropriate entry point. The calling the appropriate entry point. The process then stops and waits until the process then stops and waits until the rendezvous is accomplishedrendezvous is accomplished
24
Comments (Cont.)Comments (Cont.) The buffer process executes until execution The buffer process executes until execution
reaches one of the accept statements reaches one of the accept statements (append or take). Let us assume that it (append or take). Let us assume that it reaches the reaches the accept appendaccept append. This entry point . This entry point is for the producer to pass a value. is for the producer to pass a value. If the producer had called buffer.append(i) before, If the producer had called buffer.append(i) before,
the buffer task executes its accept append bodythe buffer task executes its accept append body If the producer had not called buffer.append(i), If the producer had not called buffer.append(i),
the buffer task stops and waits for the rendezvousthe buffer task stops and waits for the rendezvous When the accept body execution is finished, When the accept body execution is finished,
the value from the producer is stored in the the value from the producer is stored in the buffer task and both processes (consumer buffer task and both processes (consumer and buffer) start to execute concurrentlyand buffer) start to execute concurrently
25
Comments (Cont.)Comments (Cont.) In the buffer process, the task makes two In the buffer process, the task makes two
rendezvous. Note that, buffer process works in rendezvous. Note that, buffer process works in a synchronized fashion. Producer produces a a synchronized fashion. Producer produces a value, puts it in buffer by calling value, puts it in buffer by calling buffer.append(i). The next append rendezvous buffer.append(i). The next append rendezvous happens after a take rendezvous. happens after a take rendezvous.
Other producer processes may call Other producer processes may call buffer.append(i) causing them to be queued in buffer.append(i) causing them to be queued in a FIFO manner. Similarly, consumer processes a FIFO manner. Similarly, consumer processes are queued up at take entries assuming that are queued up at take entries assuming that there are several producers and consumers there are several producers and consumers active at the same timeactive at the same time
26
The The SelectSelect Statement Statement
The previous solution is a degenerate because The previous solution is a degenerate because it has no provisions for refusing a rendezvous it has no provisions for refusing a rendezvous with a full or empty buffer. with a full or empty buffer.
Furthermore, there is a strict alternation Furthermore, there is a strict alternation between producing and consumingbetween producing and consuming
The The selectselect statement allows a task to statement allows a task to select select an entry call amoung several alternativesan entry call amoung several alternatives (like a case statement in Pascal) and also (like a case statement in Pascal) and also conditionally accept an entry callconditionally accept an entry call
27
Task Body with a Select Task Body with a Select StatementStatement
task body buffer istask body buffer isbufbuf :array(0..n-1) of integer;:array(0..n-1) of integer;in_ptr, out_ptrin_ptr, out_ptr :integer:= 0;:integer:= 0;countcount :integer:= 0;:integer:= 0;
beginbeginlooploop selectselect
when count < N =>when count < N =>accept append(i: in integer) doaccept append(i: in integer) do buf(in_ptr) := i;buf(in_ptr) := i;end append;end append;count:= count+1;count:= count+1;in_ptr := (in_ptr+1) mod N;in_ptr := (in_ptr+1) mod N;
ororwhen count > 0 =>when count > 0 =>
accept take(i : out integer) doaccept take(i : out integer) do i:= buf(out_ptr);i:= buf(out_ptr);end take;end take;count:= count-1;count:= count-1;out_ptr:= (out_ptr+1) mod N;out_ptr:= (out_ptr+1) mod N;
end select;end select;end loop;end loop;
end buffer;end buffer;
28
CommentsComments The boolean expressions prefixed to the accept The boolean expressions prefixed to the accept
statements are called statements are called quardsquards. If the expression is . If the expression is true, the alternative is an open alternative permitting true, the alternative is an open alternative permitting a rendezvous else it is a closed alternative denying a a rendezvous else it is a closed alternative denying a rendezvousrendezvous
When the buffer is empty (count = 0), the When the buffer is empty (count = 0), the taketake alternative is closed. When the buffer is full (count = alternative is closed. When the buffer is full (count = N), only the N), only the taketake alternative is open alternative is open
When “0 < count < N”, buffer task will wait for the When “0 < count < N”, buffer task will wait for the first caller that calls an entry. If there are calling first caller that calls an entry. If there are calling tasks waiting in both entry queues, the task is chosen tasks waiting in both entry queues, the task is chosen according to an implementation algorithm (first open according to an implementation algorithm (first open alternative for example). In the bounded buffer case, alternative for example). In the bounded buffer case, first open alternative is a good choice to fill an empty first open alternative is a good choice to fill an empty buffer or empty a full bufferbuffer or empty a full buffer
29
General Form of a Select General Form of a Select StatementStatement
Arbitrary number of quarded accept Arbitrary number of quarded accept statements separated with statements separated with oror’s’s
The quard may be missing implying The quard may be missing implying “when true =>”“when true =>”
The last alternative may beThe last alternative may be ElseElse followed by a sequence of statements followed by a sequence of statements Delay TDelay T followed by a sequence of followed by a sequence of
statementsstatements TerminateTerminate
30
SemanticsSemantics The quards are evaluated to determine open The quards are evaluated to determine open
alternatives. There must be at least one open alternatives. There must be at least one open alternative else it is a fatal error. alternative else it is a fatal error. The quards are The quards are not re-evaluated during the execution of the not re-evaluated during the execution of the select statementselect statement
If all open alternative queues are empty, the If all open alternative queues are empty, the accepting task is suspended until a caller asks for accepting task is suspended until a caller asks for a rendezvousa rendezvous
If open alternative queues are not empty, the first If open alternative queues are not empty, the first process from one of the alternative queue is process from one of the alternative queue is chosen (depending on the implementation chosen (depending on the implementation algorithm)algorithm)
31
Programming with Programming with RendezvousRendezvous
A simple rendezvous is a remote A simple rendezvous is a remote procedure call. The calling task calls procedure call. The calling task calls a procedure which belongs to another a procedure which belongs to another task, namely the accepting tasktask, namely the accepting task
Producer
Buffer.append(i)
Buffer
Accept append
End append;
Producer
Buffer.append(i)
32
Emulation of Binary Emulation of Binary Semaphores in ADASemaphores in ADA
Null accept bodies may be used for Null accept bodies may be used for synchronization of tasks. As an example, synchronization of tasks. As an example, consider the following emulation of binary consider the following emulation of binary semaphores in ADAsemaphores in ADA
Task body semaphore isTask body semaphore isBeginBegin
looploopaccept wait;accept wait;accept signal;accept signal;
end loop;end loop;End semaphore;End semaphore;
33
Semaphore Example Semaphore Example Continued ..Continued ..
Task body producer isTask body producer isBeginBegin
looploopproduce;produce;semaphore.wait;semaphore.wait;put-in-buffer;put-in-buffer;semaphore.signal;semaphore.signal;
end loop;end loop;End producer;End producer;
Task body consumer isTask body consumer isBeginBegin
looploopsemaphore.wait;semaphore.wait;get-from-buffer;get-from-buffer;semaphore.signal;semaphore.signal;consume;consume;
end loop;end loop;End consumer;End consumer;