Lecture 4 Interprocess Communication Haibin Zhu Ph D
Lecture 4: Interprocess Communication Haibin Zhu, Ph. D. Assistant Professor Department of Computer Science Nipissing University © 2002 1
Figure 4. 1 Middleware layers 2
Message Passing z • The primitives: ysend expression_list to destination_identifier; yreceive variable_list from source_identifier; z • Variations: yguarded receive: xreceive variable_list from source_id when B; yselective receive: xselect • receive var_list from source_id 1; • |receive var_list from source_id 2; • |receive var_list from source_id 3; xend 3
Sockets and ports Socket is a communication mechanism that contains two endpoints. Each point belongs to one of the processes involved. socket any port agreed port socket message client server other ports Internet address = 138. 37. 94. 248 Internet address = 138. 37. 88. 249 4
Semantics of Message-Passing Primitives z • blocking vs. non-blocking z • buffered vs. unbuffered z • reliablevs. unreliable z • fixed-size vs. variable-size messages z • direct vs. indirect communication 5
Blocking vs. Non-Blocking Primitives Client blocked Client running Trap to kernel, process blocked Blocking Client running Message being sent Return from kernel, Process released Client blocked Client running Non-Blocking Client running Trap Return Message copied to kernel buffer Message being sent 6
Blocking vs. Non-Blocking Primitives blocking non-blocking send Returns control to user only after message has been sent, or until acknowledgment has been received. Returns control as soon as message queued or copied. receive Returns only after message has been received. Signals willingness to receive message. Buffer is ready. problems • Reduces concurrency. • Need buffering: • still blocking • deadlocks! • Tricky to program. 7
Buffered vs. Unbuffered Primitives Clien t Server A Address refers to a process kernel Clien t kernel Server Address refers to a mailbox A 8
9
10
11
12
13
UDP client import java. net. *; import java. io. *; public class UDPClient{ public static void main(String args[]){ // args give message contents and server hostname Datagram. Socket a. Socket = null; try { a. Socket = new Datagram. Socket(); byte [] m = args[0]. get. Bytes(); Inet. Address a. Host = Inet. Address. get. By. Name(args[1]); int server. Port = 6789; Datagram. Packet request = new Datagram. Packet(m, args[0]. length(), a. Host, server. Port); a. Socket. send(request); byte[] buffer = new byte[1000]; Datagram. Packet reply = new Datagram. Packet(buffer, buffer. length); a. Socket. receive(reply); System. out. println("Reply: " + new String(reply. get. Data())); } catch (Socket. Exception e){System. out. println("Socket: " + e. get. Message()); } catch (IOException e){System. out. println("IO: " + e. get. Message()); } finally {if(a. Socket != null) a. Socket. close(); } } 14 }
UDP server import java. net. *; import java. io. *; public class UDPServer{ public static void main(String args[]){ Datagram. Socket a. Socket = null; try{ a. Socket = new Datagram. Socket(6789); byte[] buffer = new byte[1000]; while(true){ Datagram. Packet request = new Datagram. Packet(buffer, buffer. length); a. Socket. receive(request); Datagram. Packet reply = new Datagram. Packet(request. get. Data(), request. get. Length(), request. get. Address(), request. get. Port()); a. Socket. send(reply); } } catch (Socket. Exception e){System. out. println("Socket: " + e. get. Message()); } catch (IOException e) {System. out. println("IO: " + e. get. Message()); } finally {if(a. Socket != null) a. Socket. close(); } } } 15
TCP client import java. net. *; import java. io. *; public class TCPClient { public static void main (String args[]) { // arguments supply message and hostname of destination Socket s = null; try{ int server. Port = 7896; s = new Socket(args[1], server. Port); Data. Input. Stream in = new Data. Input. Stream( s. get. Input. Stream()); Data. Output. Stream out = new Data. Output. Stream( s. get. Output. Stream()); out. write. UTF(args[0]); // UTF is a string encoding Universal Transfer Format String data = in. read. UTF(); System. out. println("Received: "+ data) ; }catch (Unknown. Host. Exception e){ System. out. println("Sock: "+e. get. Message()); }catch (EOFException e){System. out. println("EOF: "+e. get. Message()); }catch (IOException e){System. out. println("IO: "+e. get. Message()); } }finally {if(s!=null) try {s. close(); }catch (IOException e){System. out. println("close: "+e. get. Message()); }} } } 16
TCP server import java. net. *; import java. io. *; public class TCPServer { public static void main (String args[]) { try{ int server. Port = 7896; Server. Socket listen. Socket = new Server. Socket(server. Port); while(true) { Socket client. Socket = listen. Socket. accept(); Connection c = new Connection(client. Socket); } } catch(IOException e) {System. out. println("Listen : "+e. get. Message()); } } } // this figure continues on the next slide 17
TCP server (continued) class Connection extends Thread { Data. Input. Stream in; Data. Output. Stream out; Socket client. Socket; public Connection (Socket a. Client. Socket) { try { client. Socket = a. Client. Socket; in = new Data. Input. Stream( client. Socket. get. Input. Stream()); out =new Data. Output. Stream( client. Socket. get. Output. Stream()); this. start(); } catch(IOException e) {System. out. println("Connection: "+e. get. Message()); } } public void run(){ try { // an echo server String data = in. read. UTF(); out. write. UTF(data); } catch(EOFException e) {System. out. println("EOF: "+e. get. Message()); } catch(IOException e) {System. out. println("IO: "+e. get. Message()); } finally{ try {client. Socket. close(); } catch (IOException e){/*close failed*/} } 18
External Data Representation and Marshalling z External Data Representation: y. An agreed standard for the representation of data structures and primitive values. z Marshalling: the process of taking a collection of data items and assembling them into a form suitable for transmission. 19
CORBA CDR(Common Data Representation) for constructed types 15 primitive types: short, long, unsigned short, … Type Representation sequence length (unsigned long) f llowed by elements in order o length (unsigned long) followed by characters in order (can also string can have wide characters) array elements in order (no length specified because it is fixed) struct in the order of declaration of the components enumerated unsigned long (the values are specified by the order declared) union type tag followed by the selected member 20
CORBA CDR message index in sequence of bytes 0– 3 4– 7 8– 11 12– 15 16– 19 20 -23 24– 27 4 bytes 5 "Smit" "h___" 6 "Lond" "on__" 1934 notes on representation length of string ‘Smith’ Padded with 0 s length of string ‘London’ Padded with 0 s unsigned long The flattened form represents a Person struct with value: {‘Smith’, ‘London’, 1934} 21
Indication of Java serialized form Person p = new Person(”Smith”, “London”, 1934); Explanation Serialized values Person 8 -byte version number h 0 class name, version number 3 int year java. lang. String number, type and name of name: place: instance variables 1934 5 Smith 6 London h 1 values of instance variables The true serialized form contains additional type markers; h 0 and h 1 are handles 22
Representation of a remote object reference An identifier for a remote object that is valid throughout a distributed system 32 bits Internet address port number 32 bits time 32 bits object number interface of remote object 23
C/S communication: The request-reply protocol Client do. Operation Server Request message (wait) (continuation) Reply message get. Request select object execute method send. Reply 24
Operations of the request-reply protocol public byte[] do. Operation (Remote. Object. Ref o, int method. Id, byte[] arguments) sends a request message to the remote object and returns the reply. The arguments specify the remote object, the method to be invoked and the arguments of that method. public byte[] get. Request (); acquires a client request via the server port. public void send. Reply (byte[] reply, Inet. Address client. Host, int client. Port); sends the reply message reply to the client at its Internet address and port. 25
Request-reply message structure message. Type int (0=Request, 1= Reply) request. Id int object. Reference Remote. Object. Ref method. Id int or Method arguments array of bytes 26
RPC exchange protocols Nam e M es sag es sent b y Client Serve r R Requ es t Reply RRA Requ es t Reply Client A ck no wledg e re ply 27
HTTP messages Get Head Post Put Delete Options Trace --------Page 152 Connection (port) Send a request Send a reply Close connection method GET URL or pathname HTTP version headers message body //www. nipissingu. ca/faculty/haibinz HTTP/ 1. 1 HTTP version HTTP/1. 1 status code reason headers message body 200 OK resource data 28
Group Communication z Multicast Messages: y. Fault Tolerance based on replicated services y. Finding the discovery servers in spontaneous networking: y. Better performance through replicated data: y. Propagation of event notifications 29
IP Multicast (Class D) z IPv 4: y. Multicast routers y. Multicast address allocation z Java Multicast: y. Multicastsocket yjoin. Group() yleave. Group() z Reliability: y. Omission Failure y. Ordering 30
Multicast peer joins a group and sends and receives datagrams import java. net. *; import java. io. *; public class Multicast. Peer{ public static void main(String args[]){ // args give message contents & destination multicast group (e. g. "228. 5. 6. 7") Multicast. Socket s =null; try { Inet. Address group = Inet. Address. get. By. Name(args[1]); s = new Multicast. Socket(6789); s. join. Group(group); byte [] m = args[0]. get. Bytes(); Datagram. Packet message. Out = new Datagram. Packet(m, m. length, group, 6789); s. send(message. Out); // this figure continued on the next slide 31
Figure 4. 17 continued // get messages from others in group byte[] buffer = new byte[1000]; for(int i=0; i< 3; i++) { Datagram. Packet message. In = new Datagram. Packet(buffer, buffer. length); s. receive(message. In); System. out. println("Received: " + new String(message. In. get. Data())); } s. leave. Group(group); } catch (Socket. Exception e){System. out. println("Socket: " + e. get. Message()); } catch (IOException e){System. out. println("IO: " + e. get. Message()); } finally {if(s != null) s. close(); } } } 32
UNIX Sockets #include <sys/type. h> #include <sys/socket. h> int socket (int domain, int type; int protocal); int bind (int s, const struct sockaddr * address, size_t address_len); int listen (int s, int backlog_size); int accept (int s, struct sockaddr * address, size_t address_len); struct hostent *gethostbyaddr(const void *addr, size_t len, int type); int send (int s, const char* buf, int len, int flag); int recv (int s, const char* buf, int len, int flag); int close(int s); 33
Sockets used for datagrams Sending a message Receiving a message s = socket(AF_INET, SOCK_DGRAM, 0) bind(s, Client. Address) bind(s, Server. Address) sendto(s, "message", Server. Address) amount = recvfrom(s, buffer, from) Server. Address and Client. Address are socket addresses 34
Sockets used for streams Requesting a connection s = socket(AF_INET, SOCK_STREAM, 0) connect(s, Server. Address) Listening and accepting a connection s = socket(AF_INET, SOCK_STREAM, 0) bind(s, Server. Address); listen(s, 5); s. New = accept(s, Client. Address); write(s, "message", length) n = read(s. New, buffer, amount) Server. Address and Client. Address are socket addresses 35
Summary z Messages y. Different primitives z Communication y. UDP Datagram y. TCP Stream z EDR and Marshalling z Request-reply-protocol z Group Communication z UNIX Socket 36
- Slides: 36