Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Interprocess Communication


Published on

Interprocess Communication

Published in: Education
  • Be the first to comment

  • Be the first to like this

Interprocess Communication

  1. 1. DISTRIBUTED SYSTEMS B. Tech IV/IV, II Semester COMPUTER SCIENCE & ENGINEERING 2/6/2017 © Copyright Material 1
  2. 2. UNIT III 2/6/2017 2 Distributed Systems Foundation Middleware Support Systems Algorithms & Shared Data Characterization of Distributed Systems System Models Inter Process Communication Distributed Objects Remote Invocation Operating System Distributed File System Coordination & Agreement Transactions & Replications © Copyright Material
  3. 3. What we learnt in Unit II A. An introduction to a System Model B. What are the types of System Models C. An introduction to a Physical Model D. Understanding the need for an Architectural Model E. Types of System Architectures available F. Different variations in a Client-Server model G. Need & role of an Interfaces & an Object H. Illustration of a Fundamental Model & its types I. Ways of handling Interaction, Failures J. Addressing the security concerns in a Distributed System 2/6/2017 3© Copyright Material
  4. 4. Topic of Contents 1. Introduction to Interprocess communication 2. The Characteristics of Interprocess communication 3. Understanding what is a Socket & its purpose 4. Fundamentals of programming using Internet Protocols 5. Types of Communication channels 1. UDP Datagram Communication 2. TCP Stream Communication 6. Need for External Data Representation & Marshalling 7. How to establish Client Server Communication 8. Introduction to Group Communication 9. IP Multicast- an implementation of group communication 10. Reliability and Ordering of Multicast. 2/6/2017 4© Copyright Material
  5. 5. Learning Objective Upon completion of this unit, students will be able to: • Learn the need for Inter process communication in a Distributed Environment • Acquire the foundation for developing applications using Internet Protocols • Understand the characteristics of any interprocess communication • Analyze the role of a Socket • Apply the fundamentals of Datagram & Stream Programming • Understand the uses of TCP & UDP implementations • Illustrate the need for External Data Representation • Learn the ways of achieving External Data Representation • Show the concerns associated with respect to client server communication • Understand the need for Group Communication • A version of achieving Multicasting using IP – IP Multicasting • Ensure the reliability & Ordering of multicast messages 2/6/2017 5© Copyright Material
  6. 6. Remote Method Invocation allows allows an Object to invoke a method in a Remote Process Similarly, Remote Procedure Call allows a client to call a procedure in a remote server • Request-Reply protocols are used to support client-server communication in the form of either RPC or RMI • API to UDP provides a message passing abstraction & TCP provides the abstraction of a 2 way stream between pairs of processes Layers of Communication 2/6/2017 6© Copyright Material MIDDLEWARE LAYERS
  7. 7. 1. Interprocess Communication Distributed Systems rely on exchanging data & achieving synchronization amongst autonomous distributed Processes Inter process communication (IPC) achieves this using – Shared variables – Message passing Message passing in concurrent programming languages is achieved through API calls Principles of IPC Concurrent programs: collections of two or more sequential programs executing concurrently Concurrent processes: collection of two or more sequential Programs in operation, executing concurrently 2/6/2017 7© Copyright Material
  8. 8. 2. Characteristics of Interprocess Communication What are the prerequisites for initiating a Communication a. Who are the source & destination systems, how to identify them? b. How do they transfer data? c. How are the messages transferred? These are the few concerns/characteristics of any communication to Happen 2/6/2017 8© Copyright Material
  9. 9. a. How will the processes know the destination? • Using Direct Naming techniques: Ex. Used in a single Client- single Server application • Using Global Names: Ex. Mailboxes – any mailing applications where mailbox have the application & is globally known • Using Ports: Ex. Many senders & one receiver. b. How should the communication happen & will the communication follow any order? • Packet Switching (Click here) • Circuit Switching (Next Slide) 2/6/2017 9© Copyright Material 2. Characteristics of Interprocess Communication
  10. 10. 2/6/2017 10© Copyright Material A C E F G KIH B 1. A communication channel is established initially2.Packets travel through the same path Packets are delivered in Order 2. Characteristics of Interprocess Communication
  11. 11. 2/6/2017 11© Copyright Material 2. Characteristics of Interprocess Communication c. Message Passing Primitives • Will the execution be delayed or not by the invoking process – Blocking – Non-Blocking • Synchronization Techniques? – Asynchronous Message passing – Synchronous Message passing – Buffered Message passing
  12. 12. 3. Socket??? 2/6/2017 12© Copyright Material
  13. 13. 3. What is a Socket  • Socket is an endpoint of an inter-process communication flow across a computer network. • Today, most communication between computers is based on the Internet Protocol; therefore most network sockets are Internet sockets. • Both forms of communication (UDP and TCP) use the Socket abstraction, which provides an endpoint for communication between processes • Interprocess communication consists of transmitting a message between a socket in one process and a socket in another process • For a process to receive messages, its socket must be bound to a local port and one of the Internet addresses of the computer on which it runs 2/6/2017 13© Copyright Material
  14. 14. 3. How is Socket Used? From the earlier discussion, lets understand where are these sockets created? 2/6/2017 14© Copyright Material CLIENT Facebook FB Chat PORT NO PORT NO SERVER Facebook FB Chat PORT NO PORT NO PORTS IP Address: IP Address: MESSAGE ANYPORT AGREED PORT
  15. 15. • Socket: – Socket(String hostName, int port) throws UnknownHostException, IOException //Creates a socket connected to the named host and port. – Socket(InetAddress ipAddress, int port) throws IOException //Creates a socket using a preexisting InetAddress object and a port. • ServerSocket: – ServerSocket(int port) throws IOException //Creates server socket on the specified port with a queue length of 50. – ServerSocket(int port, int maxQueue) throws IOException //Creates a server socket on the specified port with a maximum queue length of maxQueue. 2/6/2017 15© Copyright Material 4. Fundamentals of Programming
  16. 16. DatagramSocket: – DatagramSocket( ) throws SocketException // Constructs a datagram socket and binds it to any available port on the local host machine. – DatagramSocket(int port) throws SocketException – DatagramSocket(int port, InetAddress ipAddress) throws SocketException MulticastSocket: – MulticastSocket() //Create a multicast socket. – MulticastSocket(int port) //Create a multicast socket and bind it to a specific port. – MulticastSocket(SocketAddress bindaddr) //Create a MulticastSocket bound to the specified socket address. 2/6/2017 16© Copyright Material 4. Fundamentals of Programming
  17. 17. 5. Types of Communication Channels i. Understanding of UDP ii. Terminology of UDP communication iii. Java program to create a UDP transmission iv. Understanding of TCP v. Terminology of TCP communication vi. Java program to create a TCP transmission 2/6/2017 17© Copyright Material
  18. 18. 5.(i) Understanding of UDP • UDP datagram properties: no guarantee of order preservation, message loss and duplications are possible • Steps to follow for creating a UDP Communication – create socket – bind socket to a port and local Internet address • client: arbitrary free port • server: server port – receive method: returns Internet address and port of sender, plus message – message size: IP allows for messages of up to 216 bytes • most implementations restrict this to around 8 kbytes • larger application messages: application’s responsibility to perform fragmentation/reassembling • if arriving message is too big for array allocated to receive message content, truncation occurs 2/6/2017 18© Copyright Material
  19. 19. UDP Communication using UNIX System calls 2/6/2017 19© Copyright Material 5.(i) Understanding of UDP
  20. 20. Java API for UDP datagrams • Classes – 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 2/6/2017 20© Copyright Material 5.(ii) Terminology using UDP
  21. 21. import*; // Java package for accessing sockets import*; // Java package for handling IO operations public class UDPClientDemo { public static void main(String args[]) throws SocketException,, IOException { // args are used to read the message and name of the server DatagramSocket UDP = new DatagramSocket(); // create a UDP socket byte [] m = args[0].get Bytes(); //Byte array to read the data InetAddress aHost = InetAddress.getByName(args[1]); //DNS lookup for the server int serverPort = 6789; //Some port number on which server has an application running DatagramPacket request = new DatagramPacket(m, args[0].length(), aHost, serverPort); //creating a datagram packet which has the data, server port number & server address UDP.send(request); //sending the datagram packet through the socket byte[] buffer = new byte[1000]; //temporary buffer to store the response from server DatagramPacket reply = new DatagramPacket(buffer, buffer.length); //Creating an empty datagram packet with the buffer to store the response UDP.receive(reply); //waiting for the reply System.out.println("Reply: " + new String(reply.getData())); UDP.close(); } } 2/6/2017 21© Copyright Material 5.(iii) Java Program for UDP
  22. 22. 5.(iv) Understanding of TCP • TCP establishes the channel before initiating the communication & hence will be slower than UDP – Ex: Stream of bytes to be written to or received from • Characteristics – 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, as mentioned earlier – 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 2/6/2017 22© Copyright Material
  23. 23. • Steps to follow for creating a UDP Communication 1. Connection establishment using client/server approach, afterwards peer communication 2. client: issue connect requests to the server 3. server: has listening port to receive connect request 4. messages 5. accept of a connection: server creates a new stream socket for every new connection • The next slide demonstrates a flowchart on how UNIX system calls enable a valid IPC 2/6/2017 23© Copyright Material 5.(iv) Understanding of TCP
  24. 24. TCP Communication using UNIX System calls 2/6/2017 24© Copyright Material 5.(iv) Understanding of TCP
  25. 25. Classes available through Java API ServerSocket class: create socket at server side to listen for connect requests Socket class: for processes with connections • constructor to create a socket and connect it to remote host and port of a server • methods for accessing input and output stream These were the classes briefly discussed earlier in types of Sockets 2/6/2017 25© Copyright Material 5.(v) Terminology using TCP
  26. 26. 2/6/2017 26© Copyright Material 5.(vi) Java Program for TCP import*; // Java package for accessing sockets import*; // Java package for handling IO operations public class TCPClient { public static void main (String args[]) throws IOException { Socket cs=new Socket("localhost",1600); //Client socket getting connected to a server running in the same system, with the application hosted on port no: 1600 OutputStream os=cs.getOutputStream(); //Applying outputstream on the socket that was accepted by the server os.write(args[0].getBytes());//Writing the data to the server through the socket } }
  27. 27. 2/6/2017 27© Copyright Material 5.(vi) Java Program for TCP import*; // Java package for accessing sockets import*; // Java package for handling IO operations public class TCPServer { public static void main (String args[]) throws IOException { int serverPort = 1600; // the server port ServerSocket listenSocket = new ServerSocket(serverPort); // new server port generated to which all client requests are requested to while(true) { Socket clientSocket = listenSocket.accept(); // listen for a new client to establish a new connection InputStream is=clientSocket.getInputStream(); //Applying inputstream on the accepted socket byte data[]=new byte[50];;//reading the received data through the socket System.out.println(new String(data));//Display received message } } }
  28. 28. What have we learning till now?? 1. The need for a Distributed System 2. The Challenges of such a system 3. Types of system models helpful for architecting such a system & now……… 4. How to make these system talk to each other??? We have seen in this chapter how to achieve Interprocess communication   But what if systems are Heterogeneity     ? ? ? ? ? ? ? ? ? Systems Indeed are HETEROGENEOUS Concerns with heterogeneity in Hardware • Big or Little endian? • 16, 32, 64 bit integers ? • ASCII characters vs Unicode • floating point values, IEEE? • Instruction-sets Concerns with heterogeneity in Software • Internal representation of data-structures (padding) 2/6/2017 28© Copyright Material 6.External Data Representation
  29. 29. • An agreed standard for the representation of data structures and primitive values is called an External Data Representation. • Therefore, data representation use sender’s or receiver’s format and convert at the other end • Few types of Data representation formats are – SUN Microsystems XDR – CORBA CDR – Object Serialization & Remote Object Interfaces • These conversions exclusively are termed as – Marshalling: assembling a collection of data items in a form suitable for transmission – Unmarshalling: disassembling and recovery of original data items • These are usually performed automatically by middleware layer using – hand-programming error-prone – Compilers for programs working directly at transport API 2/6/2017 29© Copyright Material 6.External Data Representation
  30. 30. CORBA XDR • The type of a data item is not given with the data representation in the message in either the CORBA CDR or the Sun XDR standard • This is because it is assumed that the sender and recipient have common knowledge of the order and types of the data items in a message • We might use CORBA IDL to describe the data structure in the message • The CORBA interface compiler generates appropriate marshalling and unmarshalling operations for the arguments and results of remote methods from the definitions of the types of their parameters and results. 2/6/2017 30© Copyright Material 6.External Data Representation
  31. 31. JAVA RMI • In Java RMI, both objects and primitive data values may be passed as arguments and results of method invocations • Serialization refers to the activity of flattening an object or a connected set of objects into a serial form that is suitable for storing on disk or transmitting in a message, for example, as an argument or the result of an RMI • A class implementing the Serializable interface has the effect of allowing its instances to be serialized • When an object is serialized, all the objects that it references are serialized together with it to ensure that when the object is reconstructed, all of its references can be fulfilled at the destination. • Serialization and deserialization of the arguments and results of remote invocations are generally carried out automatically by the middleware, without any participation by the application programmer 2/6/2017 31© Copyright Material 6.External Data Representation
  32. 32. Remote Object References • A Remote Object Reference is an identifier for a remote object that is valid throughout a distributed system. • It is 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 • To allow remote objects to be relocated into a different process on a different computer, the remote object reference should not be used as the address of the remote object 2/6/2017 32© Copyright Material 6.External Data Representation
  33. 33. Communication is often built over UDP datagrams – client-server protocol consists of request/response pairs, hence no acknowledgements at transport layer are necessary – avoidance of connection establishment overhead – no need for flow control due to small amounts of data transferred example (for RPC or RMI communication): REQUEST – REPLY PROTOCOL 2/6/2017 33© Copyright Material 7. Client Server Communication Request ServerClient doOperation (wait) (continuation) Reply message getRequest execute method message select object sendReply © Pearson Education 2005
  34. 34. REQUEST – REPLY PROTOCOL • public byte[] doOperation (RemoteObjectRef o, int methodId, 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[] getRequest (); – acquires a client request via the server port. • public void sendReply (byte[] reply, InetAddress clientHost, int clientPort); – sends the reply message reply to the client at its Internet address and port. 2/6/2017 34© Copyright Material 7. Client Server Communication
  35. 35. Format of a Protocol Message 1. Message Type : Request or Reply? 2. Request ID: 3. Sending process ID( IP address or Port Number) 4. Sequence number incremented by sender with every request 5. Reference of the Object 6. Method ID 7. Arguments Having all of the above parameters, the communication between client-server would carry forward. 2/6/2017 35© Copyright Material 7. Client Server Communication
  36. 36. Example: Hyper Text Transfer Protocol (HTTP) • It is a lightweight request - reply protocol for the exchange of network resources between web clients and web servers Steps included in the protocol – connection establishment between client and server (likely TCP, but any reliable transport protocol is acceptable) – client sends request & server sends reply – connection closure Inefficient scheme, therefore HTTP 1.1 allows “persistent transport connections” (remains open for successive request/reply pairs) Resources can have mime-type data types, e.g. – text/plain, text/html, image/jpeg Data is marshalled into ASCII transfer syntax 2/6/2017 36© Copyright Material 7. Client Server Communication
  37. 37. • Till now we have learnt the solution for communication between a Client & Server • Will it always be the case for constructing a Distributed System?   • Pairwise exchange of messages is not the best model for communication from one process to a group of other processes, which may be necessary • Multicast Operation is more appropriate & few examples of such systems – Fault tolerance based on replicated services: – Discovering services in spontaneous networking – Better performance through replicated data – Propagation of event notifications 2/6/2017 37© Copyright Material 8. Group Communication
  38. 38. • Java API provides a means for IP Multicasting through MulticastSocket, as explained in here • IP multicast allows the sender to transmit a single IP packet to a set of computers that form a multicast group • A Multicast Group is specified by a Class D Internet address that is, an address whose first 4 bits are 1110 in IPv4. • At the application programming level, IP multicast is available only via UDP. • When a multicast message arrives at a computer, copies are forwarded to all of the local sockets that have joined the specified multicast address and are bound to the specified port number & the following details are specific to IPv4 – Multicast Routers – Multicast Address Allocation • Multicast addresses may be permanent or temporary. Permanent groups exist even when there are no members 2/6/2017 38© Copyright Material 9. IP Multicasting
  39. 39. import*; // Java package for accessing sockets import*; // Java package for handling IO operations public class Multi { public static void main(String[] args) throws IOException { MulticastSocket s = new MulticastSocket(6789); //Port which is used for Multicasting InetAddress group = InetAddress.getByName(""); //A Multicast group s.joinGroup(group);//Add the socket to the Multicast Group byte[] m = new byte[10];//Temporary Buffer to read the input message m = args[0].getBytes(); DatagramPacket p = new DatagramPacket(m,m.length,group,6789); //Constructing a Datagram Packet which is send to the application running in the group s.send(p); //Datagram Packet Sent byte[] rec = new byte[10]; for(int i=0;i<3;i++) { DatagramPacket q = new DatagramPacket(rec,rec.length); //creating a empty datagrampacket to copy the receiving contents s.receive(q);//Receiving the content through the same socket System.out.println("Received:" +new String(q.getData())); } } } 2/6/2017 39© Copyright Material 9. IP Multicasting
  40. 40. • IP multicast, which suffers from omission failures – A datagram sent from one multicast router to another may be lost, thus preventing all recipients beyond that router from receiving the message – If a multicast router fails, the group members beyond that router will not receive the multicast message – IP packets sent over an internetwork do not necessarily arrive in the order in which they were sent. • Some applications require a multicast protocol that is more reliable than IP multicast • There is a need for Reliable Multicast, in which any message transmitted is either received by all members of a group or by none of them • Totally Ordered Multicast, ensures all of the messages transmitted to a group reach all of the members in the same order. 2/6/2017 40© Copyright Material 10. Reliability & Ordering
  41. 41.  Understood the need for Interprocess communication  Learnt the features of Interprocess communication  Studied what is a Socket & its role in IPC  Practiced various types of Sockets supported by Java API  Evaluated the 2 types of Communication channels  Understood the architecture of Datagram Communication  Understood the architecture of Stream Communication  Illustrated the need for XDR formats’ & the role of Marshalling  Learnt the ways of establishing Client-Server communication  Introduced Group Communication & the role of IP-Multicast  Elaborated on the drawbacks of IP-multicasting and the ways of achieving Reliability and Ordering of Multicast 2/6/2017 41© Copyright Material SUMMARY
  42. 42. 1. Which of these would be the layers of a Middleware a. RMI/RPC b. Request-Reply Protocol c. TCP/UDP d. a&b 2. Interprocess communication in Distributed Systems is achieved through a. Shared Memory b. Message Passing c. a&b d. None 3. _______ is a methodology of that establishes a dedicated communications channel in a network a. Circuit Switching b. Packet Switching c. Multiplexing d. Message Passing 2/6/2017 42© Copyright Material QUIZ
  43. 43. 4. Which of the following synchronization technique, blocks the sender until receiver is ready to receive a. Synchronous b. Asynchronous c. Buffered d. a&b 5. A Socket represents a. Port number b. IP Address c. Combination of a&b d. Physical Address 6. The following is the well known port number for HTTP a.21 b. 80 c.143 d.25 2/6/2017 43© Copyright Material QUIZ
  44. 44. 7. Multicasting refers to a. transmit a single message to a select group of recipients in the network. b. transmit a single message to a selected receiver in the network. c. transmit a single message to all the recipients in the network. d. transmit a individual messages to a select group of recipients in the network. 8. Java API gives DatagramSocket constructors. Pick out the invalid option a. DatagramSocket(int port) b. DatagramSocket( ) c. DatagramSocket( InetAddress ipAddress) d. DatagramSocket(int port, InetAddress ipAddress) 2/6/2017 44© Copyright Material QUIZ
  45. 45. 9. Which of the following is a reliable communication protocol a.TCP b. UDP c. IP d. BGP 10. ____ is the process of recovering the original data items with respect to External Data Representation a. Unmarshalling b. Marshalling c. RPC d. CDR 11. Multicast group belongs to which class internet addressing a. Class C b. Class A c. Class B d. Class D 2/6/2017 45© Copyright Material QUIZ
  46. 46. 1. D 2. C 3. A 4. A 5. C 6. B 7. A 8. A 9. C 10. A 11. D 2/6/2017 46© Copyright Material KEY
  47. 47. • Application Programming Interface (API): specifies a set of functions or routines that accomplish a specific task or are allowed to interact with a specific software component • Message passing: is a technique for invoking behavior (i.e., running a program) on a computer. In contrast to the traditional technique of calling a program by name, message passing uses an object model to distinguish the general function from the specific implementations • Direct Naming: Process names of source and destination serve as identifiers(a pair of source and destination systems defines a channel) • Packet Switching: is a digital networking communications method that groups all transmitted data – regardless of content, type, or structure – into suitably sized blocks, called packets • Asynchronous Message Passing: message passing is done using buffers with unbounded capacity where sender may race ahead an unbounded number of steps, sender never blocks & receiver blocks on empty queue. 2/6/2017 47© Copyright Material GLOSSARY
  48. 48. Synchronous Message Passing: No buffering system is available between sender and receiver. A sender blocks until receiver is ready to receive & a receiver blocks until sender is ready to send Buffered Message Passing: Buffers with bounded, finite capacity. Each sender may race ahead a finite, bounded number of steps and a sender only blocks on full buffer while a receiver blocks on an empty buffer Socket is an endpoint of an inter-process communication flow across a computer network. ServerSocket: A server socket waits for requests to come in over the network. It performs some operation based on that request, and then possibly returns a result to the requester. DatagramSocket: A datagram socket is the sending or receiving point for a packet delivery service. Each packet sent or received on a datagram socket is individually addressed and routed. Multiple packets sent from one machine to another may be routed differently, and may arrive in any order. 2/6/2017 48© Copyright Material GLOSSARY
  49. 49. MulticastSocket: The multicast datagram socket class is useful for sending and receiving IP multicast packets. A MulticastSocket is a (UDP) DatagramSocket, with additional capabilities for joining "groups" of other multicast hosts on the internet. External data representation (XDR): An agreed standard for the representation of data structures and primitive values Marshalling: is a process of assembling a collection of data items in a form suitable for transmission Unmarshalling: is a process of disassembling and recovery of original data items. Remote Object Reference is an identifier for a remote object that is valid throughout a distributed system Multicast: this is an operation that sends a single message from one process to each of the members of a group of processes, usually in such a way that the membership of the group is transparent to the sender 2/6/2017 49© Copyright Material GLOSSARY
  50. 50. IP multicast: allows the sender to transmit a single IP packet to a set of computers that form a multicast group 2/6/2017 50© Copyright Material GLOSSARY
  51. 51. 2/6/2017 51© Copyright Material