Distributed Programming Concepts and Notations
Inter-process Communication Synchronous Messages Asynchronous Messages Select statement Remote procedure calls Atomic transactions
Message based programming languages No shared variables Each object has a caretaker Can operate on an object only by sending a message to its caretaker
Basic Constructs send expression-list to destination receive variable-list from source can be used for communication synchronization
Basic Issues How to specify designators ? How is communication synchronized ?
Designator Specification Direct Naming P1: P2: var a,x:integer; var b,y:integer; x:=2*a; receive b from P1; send x to P2 y:=y+b; Advantages simple efficient implementation Disadvantages must know at compile time receive from anyone
Mailboxes process simpleNS; nsmb: mailbox; request:message_type; begin create(nsmb); repeat forever receive request from nsmb; case request.mtype of. end; client: send myreq to nsmb; Advantages: generality Disadvantages: inefficient to implement
Buffering Synchronous vs Asynchronous communication Blocking send vs Non-blocking send Unbuffered send vs Unbounded buffering Advantages of synchronous communication easier implementation easy proof rules easy to synchronize fault-tolerance easier Disadvantages exploitation of concurrency difficult
Avoiding blocking on receive peek() selective receive receive var from designator about key
Selective Communication Guarded command: e.g. extension: guard = boolean expression and optionally a message passing statement. A guard may Succeed other side is also ready to communicate Fail other side has already terminated or May neither succeed nor fail.
CSP: Synchronous messages process buffer; var slots:array [0..N-1] of integer; head, tail:0..N-1; size:0..N; begin head, tail, size := 0,0,0; *[ size < N; receive slots[tail] from producer -> size := size+1; tail:=tail + 1 mod N; [] size > 0; send slots[head] to consumer -> size := size-1; head:=head + 1 mod N; ] end
Remote Procedure Calls Key idea: try to preserve the semantics of local procedure calls P1: call process.pname(parameters) P2: remote procedure pname(..) end;
Kinds of RPC Simple declaration Accept statements
Ada - rendezvous P1: call P2:accept Synchronization parameters transmitted execute body parameters transmitted back
Comparison Advantages of accept statement server has flexibility of when to serve can provide different services by different bodies can combine with guarded commands to provide non-determinism Disadvantages of accept statement difficult to understand or prove correctness
Ada example task buffer; entry deposit(in value:T); entry fetch(out value:T); var slots:array [0..N-1] of T; head, tail:0..N-1; size:0..N; begin head, tail, size := 0,0,0; loop select when size < N => accept deposit(in value:T); slots[tail] := value; size := size+1; tail:=tail + 1 mod N; or when size > 0 => accept fetch(out value:T); value := slots[head]; size := size-1; head:=head + 1 mod N; end select; end loop;
Ada example contd. task produce repeat item := produce(); call buffer.deposit(item); forever; end; task consumer; repeat call buffer.fetch(item); consume(item); forever; end
Care must be taken regarding global variables pointers as parameters representation of parameters at least once/ at most once semantics