Lecture 38: Concurrent & Distributed Programming Message Passing
Total Page:16
File Type:pdf, Size:1020Kb
Lecture 38: Concurrent & Distributed Programming CSC 131 Fall, 2006 Message Passing Ada Tasks • Must provide send and receive operations w/ message and receiver/sender. • Synchronous message passing • Synchronous or asynchronous? • Tasks have some features of monitors • If asynchronous then use !mailbox" to store • Exports entry names #w/ parameters$ • If synchronous then sender and receiver must • Entry names have FIFO queues !rendezvous" before either can proceed. task body Buffer is MaxBufferSize: constant INTEGER := 50; Store: array(1..MaxBufferSize) of CHARACTER; BufferStart: INTEGER := 1; Accepting an entry BufferEnd: INTEGER := 0; BufferSize: INTEGER := 0; begin loop select when BufferSize < MaxBufferSize => accept insert(ch: in CHARACTER) do Ca!er only blocked Store(BufferEnd) := ch; in accep " end insert; select BufferEnd := BufferEnd mod MaxBufferSize + 1; BufferSize := BufferSize + 1; [when <cond> =>] <select alternative> or when BufferSize > 0 => accept delete(ch: out CHARACTER) do {or [when <cond> =>] <select alternative>} ch := Store(BufferStart); end delete; [else <statements>] BufferStart := BufferStart mod MaxBufferSize + 1; BufferSize := BufferSize -1; or end select accept more (notEmpty: out BOOLEAN) do notEmpty := BufferSize > 0; end more; or terminate; end select; end loop end Buffer; task type Producer; task body Producer is Comparing Mechanisms ch: CHARACTER; begin loop TEXT_IO.GET(ch); • Shared memory concurrency Buffer.insert(ch); end loop; ' Semaphores very low level. end Producer; ' Monitors passive regions encapsulating resources to be shared #mutual exclusion$. Cooperation enforced task type Consumer; by wait and signal statements. task body Consumer is ch: CHARACTER; begin • Distributed Systems loop ' Everything active in Ada tasks #resources and Buffer.delete(ch); processes$ TEXT_IO.PUT(ch); end loop; ' Monitors and processes can easily be structured as end Consumer; Ada tasks and vice'versa. Remote Method Invocation Where use RMI? • Distributed object applications need to:* • Typical example is client'server interaction ' Locate remote objects where server is remote ' Communicate with remote objects • Remote object presents remote interface ' Load class bytecodes for objects that are passed as parameters or return values declaring methods of remote object • More complex than w/RPC as it is harder to • Allows client code to use same syntax as local send objects #esp. from unknown classes$ call. *From Sun RMI documentatio# Di(erences from local objects Using java.rmi.Remote • Clients only interact directly with remote • public interface Remote%& // no methods interfaces, not classes. • But remote interfaces must extend it. • Remote arguments and return values are passed by copy, rather than being shared. • Remote methods must include !throws RemoteException" #or super'Exception$ • Remote object itself is shared, not copied. ' It is thrown when remote invocation fails, e.g., communication failure, failure during value • Life gets more complicated when things fail marshalling or unmarshalling, or protocol errors How it works RMI vs CORBA • Obtain access to remote object from registry • Send messages to remote object • CORBA ' Common Object Request Broker Architecture • Stub marshalls arguments #must implement Serializable$ and sends to server. Stub ' Cross'platform and cross'language unmarshalls return value. Objects can be sent! ' Only handles primitive data, no objects as parameters or return values • If new classes speci)ed as parameters or return type, dynamically load appropriate classes. Next Week • Brief discussion of logic programming • Programming language design issues.