220 likes | 429 Views
Chapter 9 Spaces with LINDA. Linda. Linda is an experimental programming concept unlike ADA or Occam which are fully developed production-quality languages. Linda has been experimentally embedded in several languages. Linda – Ada and Occam.
E N D
Linda • Linda is an experimental programming concept unlike ADA or Occam which are fully developed production-quality languages. • Linda has been experimentally embedded in several languages
Linda – Ada and Occam • Ada and Occam are based on the idea of synchronous communication channels between processes. • There is coupling in both time and space • Time: The synchronous rendezvous means that both processes must exist simultaneously in order to communicate (must synchronize to pass data). • Space: Addressing by process identification (task name in ADA, channel name in Occam) means that there must be some global scope where processes are defined (processes must know the identity to pass data).
Linda TupleSpace • Linda provides uncoupling in both time and space (unsynchronized and no identity check). • This is done by defining a tuple space (TS) which is some form of shared memory containing tuples • The shared data (tuples) is persistent, meaning that the data can exist after the termination of the process that created them and used by processes that are activated later
Tuple • Tuples are typed sequences of data like parameter lists in procedure calls. • The concurrent programming primitives of Linda work by pattern matching on the tuple signatures • Eg: (1,’A’) - (integer, character) (1,2) - (integer,integer) (‘A’,1) - (character,integer) These tuples can not be matched since their signatures are different.
The Linda Primitives(Based on the Terminology used in jBACI) • Tuple spaceis calleda board and a tupleis called a note • Postnote(v1, v2, ...) This statement creates a note from the values of the parameters and posts it on the board. If there are processes blocked waiting for a note matching this parameter signature, an arbitrary one of them is unblocked • Removenote(x1, x2, ...) The parameters must be variables. The statement removes a note that matches the parameter signature from the board and assigns its values to the parameters. If no matching note exists, the process is blocked. If there is more than one matching note, an arbitrary one is removed • Readnote(x1, x2, ...) Like removenote, but it leaves the note on the board
An Example • Suppose that the following statements are executed postnote(‘a’, 10, 20) postnote(‘a’, 30) postnote(‘a’, false, 40) postnote(‘a’, true, 50) • The contents of the board • Execute the statements below Integer m, integer n, boolean b • removenote(‘b’, m, n) process is blocked • removenote(‘a’, m, n) process matches and removes note (‘a’, 10, 20) • readnote(‘a’, m) process matches and reads note (‘a’, 30) – no removal • removenote(‘a’, n) note (‘a’, 30) is removed • removenote(‘a’, m, n) process is blocked • removenote(‘a’, b, n) one of the (‘a’, true, 50) or (‘a’, false, 40) is removed • removenote(‘a’, b, m) the other note is removed • postnote(‘b’, 60, 70) process blocked in (1) is unblocked and the note is removed
Implementing a General Semaphore in Linda • Post k notes as the initial value of a semaphore do K times postnote(‘s’) • Removenote is equivalent to wait and postnote to signal
Formal Parameters • Client posts a note ‘S’ with process ID, service request and parameters for the service and waits for a result note ‘R’ with matching ID and the result • The server removes a service note ‘S’ from some client with service request s and parameter p. When the service is over it posts a result note ‘R’ to the same client • Here, a server provides several different service requests at the same time
Match for a Specific Value • The server in the above algorithm waits for a specific service using a formal parameter ‘s=‘
Another Example • The formal parameter ‘count=‘ in the consumer makes sure that the values are consumed in the order they are produced
Linda in j BACI • A notein jBACI consists of a triple: a character and two integer values.. • The first parameter must be a character value, while the two optional parameters must be integer variables. • The operations can be called with one, two or three parameters; unused integer parameters are given the default value -32767: postnote('a'); { Equivalent to postnote('a', -32767, -32767) } postnote('a', 5); { Equivalent to postnote('a', 5, -32767) } postnote('a', 5, 10);
Linda in j BACI (Cont.) • For historical reasons, there are two syntaxes for matching the values of a tuple to the current values of the parameters: • If the equal sign “=“ appears after a variable, the value of the note in that position must match the current value of that variable. For example, the following statements will remove any tuple of the form ('a',...,5): i2 := 5; removenote('a',i1,i2=); • removenoteeq (readnoteeq) is like removenote (readnote), but the two parameters values of the note must match the current values of both variables. For example, the following statements will remove only the tuple ('a',1,2): i1 := 1; i2 := 2; removenoteeq('a',i1,i2); • Note that removenoteeq('a',i1,i2) is equivalent to removenote('a',i1=,i2=).
Matrix Multiplication Algorithm in Linda • Notes identified as • ‘E’ contain the elements passed from north to south • ‘S’ contain the partial sums passed from east to west
Matrix Multiplication in jBaci Multiply two matrices A and B to get matrix C A x B = C 1 2 3 1 0 1 7 8 1 4 5 6 x 0 1 0 = 16 17 4 7 8 9 2 2 0 25 26 7
Matrix Multiplication in jBaci program LindaMultiplication; {multiply two 3x3 matrices A and B - 9 Workers} var a,b :array[1..3,1..3] of integer; process initandprint; end; process worker; end; begin cobegin initandprint; worker; worker; worker; worker; worker; worker; worker; worker; worker; coend; end.
Matrix Multiplication in jBaci (Cont.) process initandprint; var c, i, j, k: integer; begin {array A} a[1,1]:=1; a[1,2]:=2; a[1,3]:=3; a[2,1]:=4; a[2,2]:=5; a[2,3]:=6; a[3,1]:=7; a[3,2]:=8; a[3,3]:=9; {array B} b[1,1]:=1; b[1,2]:=0; b[1,3]:=2; b[2,1]:=1; b[2,2]:=2; b[2,3]:=1; b[3,1]:=1; b[3,2]:=1; b[3,3]:=1; {post worker notes – column and row numbers} postnote('w',1,1); postnote('w',1,2); postnote('w',1,3); postnote('w',2,1); postnote('w',2,2); postnote('w',2,3); postnote('w',3,1); postnote('w',3,2); postnote('w',3,3); postnote('N',1); {Next job counter} { write the resulting matrix when computations are over} for i:= 1 to 3 do for j:=1 to 3 do begin k:=i*10+j; removenote('c',k,c); writeln('c',k,'=',c); end; end;
Matrix Multiplication in jBaci (Cont.) process worker; var element, row, col, r, v :integer; begin while true do begin removenote('N',element); postnote('N',element+1); if element > 3*3 then suspend; removenote('w',row,col); v:=0; for r:=1 to 3 do v:=v+a[row,r]*b[r,col]; r:= row*10+col; postnote('c',r,v); end; end;
Producer-Consumer with Linda • One producer and 3 consumers. • Producer puts a message on the Linda board • All consumers read the same message and the last one to read removes the note forever program producerandconsumers; const n=3; {3 consumers} process producer; end; process consumer; end; begin cobegin producer; consumer; consumer; consumer; coend; end.
Producer-Consumer with Linda (Cont.) process producer; var i: integer := 34; c: char; begin while (i < 126) do {characters 34 to 126 are printable characters} begin c:= i; {convert value to character - first parameter of postnote is a character!} i:= i+1; {increment for the next message} postnote(c,n) {post a message} end; end; process consumer; Var c :char; i :integer :=34; j :integer; begin while (i < 126) do begin c:= i; removenote(c,j); {remove the note} j:= j-1; if j <> 0 then postnote(c,j); {if not the last process then put note with one less count} i:= i+1 {increment for the next message} end; end;
Linda Simulation of Rendezvous • Ada rendezvous to be simulated accept Proc(I: in integer; C: in character; B: out boolean) do ....... end Proc; Proc(65, ‘X’, B); --- call to entry
Linda Simulation of Rendezvous integer pid, i; character c -- code for accepting process readnote(“Proc”, pid, i, c); Proc(I, C, B); -- accept “proc” code procedure postnote(pid, B); -- code for calling process readnote(“Proc”, 24, 65, ‘X’); postnote(24, B: Boolean);