This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
♦ Distributed Systems rely on exchanging data and achieving synchronization amongst autonomous distributed processes� Inter process communication (IPC)
– shared variables– message passing
� message passing in concurrent programming languages– language extensions– API calls
♦ Principles of IPC(see also [Andrews and Schneider 83] G. Andrews and F. Schneider, Concepts and Notations for Concurrent Programming, ACM Computing Surveys, Vol. 15, No. 1, March 1983)
� Concurrent programs: collections of two or more sequential programs executing concurrently
� Concurrent processes: collection of two or more sequential programs in operation, executing concurrently
♦ Destination Designation� direct naming: source and destination process names serve as designators
(a pair of source and destination designators defines a channel)send cur_status to monitor or monitor!cur_status
receive message from handler or handler?message
– easy to implement and use– allows a process easy control when to receive which message from
which other process– use to implement client/server applications
�well suited to implement client/server paradigm if there is one client and one server
�otherwise: server should be capable of accepting invocations from any client at any time, and a client should be allowed to invoke many services at a time if more than one server available
– impossible for a program to communicate along a channel not known at compile time
– inflexibility: if a process might ever need to communicate with a receipient, that channel must be available throughout the entire runtime of the sending programme
� dynamic (at runtime)– administrative overhead at runtime– more flexible allocation of communication resources
♦ Semantics of message passing primitives� Blocking
– non-blocking: the execution will never delay the invoking process – blocking: otherwise
� Synchronization– asynchronous message passing: message passing using buffers with
unbounded capacity�sender may race ahead an unbounded number of steps�sender never blocks�receiver blocks on empty queue
– synchronous message passing: no buffering between sender and receiver�sender blocks until receiver ready to receive�receiver blocks until sender ready to send
– buffered message passing: buffers with bounded, finite capacity�sender may race ahead a finite, bounded number of steps�sender blocks on full buffer�receiver blocks on empty buffer
♦ Sockets� Internet IPC mechanism of Unix and other operating systems (BSD Unix,
Solaris, Linux, Windows NT, Macintosh OS)� processes in these OS can send and receive messages via a socket� sockets are duplex� sockets need to be bound to a port number and an internet address in order
to be useable for sending and receiving messages� each socket has a transport protocol attribute (TCP or UDP)� messages sent to some internet address and port number can only be
received by a process using a socket that is bound to this address and port number
� UDP socket can be connected to a remote IP address and port number� processes cannot share ports (exception: TCP multicast)
– DatagramPacket constructor generating message for sending from array of bytes�message content (byte array)�length of message�Internet address and port number (destination)
– similar constructor for receiving a message– DatagramSocket class for sending and receiving of UDP datagrams
�one constructor with port number as argument, another without�no-argument constructor to use free local port�methods
* send and receive* setSoTimeout
* connect for connecting a socket to a particular remote Internet address and port
– process creates socket, sends message to server at port 6789, and waits to receive reply
import java.net.*;import java.io.*;public class UDPClient{
public static void main(String args[]){ // args give message contents and destination hostnametry {
DatagramSocket aSocket = new DatagramSocket(); // create socketbyte [] m = args[0].getBytes();InetAddress aHost = InetAddress.getByName(args[1]); // DNS lookupint serverPort = 6789;DatagramPacket request =
new DatagramPacket(m, args[0].length(), aHost, serverPort);aSocket.send(request); //send nessagebyte[] buffer = new byte[1000];DatagramPacket reply = new DatagramPacket(buffer, buffer.length);aSocket.receive(reply); //wait for replySystem.out.println("Reply: " + new String(reply.getData()));aSocket.close();
}catch (SocketException e){System.out.println("Socket: " + e.getMessage());}catch (IOException e){System.out.println("IO: " + e.getMessage());}} // can be caused by send
♦ IPC based on TCP streams� abstract service: stream of bytes to be written to or received from� features
– message size: no constraint, TCP decides when to send a transport layer message consisting of multiple application messages, immediate transmission can be forced
– connection oriented– retransmission to recover from message lost (timeout-bounded)– queue at destination socket– blocked on receive– flow control to block sender when overflow might occur– need to agree on data sent and received– server generates new thread for new connection
♦ API for streams� connection establishment using client/server approach, afterwards peer
communication– client: issue connect requests– server: has listening port to receive connect request messages– accept of a connection: create new stream socket for new connection
♦ Data Representation� data representation problem
– use agreed external representation, two conversions necessary– use sender’s or receiver’s format and convert at the other end
� transmission of structured data types– data types may not change during transmission– usage of a commonly understood “flattened” transfer format (structured
types are reduced to their primitive components)� data representation formats
– SUN Microsystems XDR– CORBA CDR– ASN.1 (OSI layer 6)
� marshalling/unmarshalling– marshalling: assembling a collection of data items in a form suitable for
transmission– unmarshalling: disassembling and recovery of original data items– usually performed automatically by middleware layer
�hand-programming error-prone�use of compilers for programs working directly at transport API
♦ Java Object Serialization� example: class Person
� serialization: – flattening an object into a linear form such that it can be stored in a file or
transmitted in a message– linear format must be such that deserialization routine is capable of
recovering the complete object structure and state�inclusion of
* handles (references to other objects)* name of class that an object belongs to* version number of class
– note: mark objects as non-serializable (“transient”) if they are not supposed to be serialized (e.g., socket references, files, etc.)
public class Person implements Serializable {private String name;private String place;private int year;public Person(String aName, String aPlace, int aYear){
♦ Java Object Serialization� example: class Person
� serialization example: – Person p = new Person(“Smith”, “London”, 1934)
�serialization: create instance of class ObjectOutputStream and invoke writeObject method, passing object to be serialized as argument
�deserialization: open ObjectInputStream on the serialized structure and use readObject method
public class Person implements Serializable {private String name;private String place;private int year;public Person(String aName, String aPlace, int aYear){
♦ Remote Object References� needed when a client invokes an object that is located on a remote server� reference needed that is unique over space and time
– space: where is the object located– time: correct version of an undeleted object
� a generic format proposal
– internet address/port number: process which created object– time: creation time– object number: local counter, incremented each time an object is created
in the creating process– interface: how to access the remote object (if object reference is passed