380 likes | 529 Views
Interprocess CommunicationPC. DCS. Interprocess Communication (IPC). Communication and synchronization between co-operating processes. Building Blocks Client-Server Communication Client-server communication using sockets in Java Group Communication Sockets in Java. Building blocks.
E N D
Interprocess Communication (IPC) • Communication and synchronization between co-operating processes. • Building Blocks • Client-Server Communication • Client-server communication using sockets in Java • Group Communication • Sockets in Java
Building blocks • Data representation and messages • External data representation • Mapping data structures and data items to messages: • Marshaling and unmarshalling • Flattening data structures before transmission and rebuilding them on arrival • Send andReceive operations • Synchronous and asynchronous communication • Message destinations 2
External data representation • Two Methods: • Convert data to an agreed (standard) external format and convert to local form on receipt. • Transmit data values in their native form together with an architecture identifier- so the recipients convert the data if necessary. 3
External data representation-The SUN XDR standard • The entire XDR message consists of a sequence of 4-byte objects: • example:‘Hi’, ‘There’, 2001 2 sequence length ‘Hi’ ‘There’ Integer 2001 “Hi--” Must also define which end of each object is the most significant 5 “ Ther” “e---” 2001 4
Marshaling • Take a collection of data items and assemble them into a suitable form for transmission. • Marshalingflattens structured data items into a sequence of data items and translates them to an external data representation • Unmarshaling translates from external data representation to local one and unflattens the data items The marshalled message of last example: 2 Hi 5 There2001 5
Send and Receive Operations • Message passing is supported by send and receive operations: B A Receive (Port-id, &message) Send (Port-id, & message) Process A blocks until the message has been sent Process B blocks until message arrives 6
Blocking Send or Receive • The sending process blocks while the message is being sent. Similarly a receive primitive blocks the caller until the message has been received. Client Blocked Client Running Client Running Return from Kernel, Process Released Trap to kernel process blocked Message being sent TIME A blocking send 7
Non-blocking Send or Receive • With non-blocking send (or receive) the control is returned to the caller immediately after the message is sent (or received): • i.e continue computing in parallel with message transmission Client Running Client Blocked Client Running Return Trap message copied to kernel buffer Message being sent TIME 8 A non-blocking send
Timeouts in blocking send and receive • A blocking send or receive could wait forever! • A timeout can be associated with a port so that the caller can terminate with an error status. 9
Synchronous and Asynchronous communication • Synchronous: • The sending process and the receiving process both synchronize at every message. • e.g.: occam • Asynchronous: • Send operation is non-blocking but the receive operation can be blocking • e.g. BSD 4.x UNIX, Mach, Chorus 10
Buffered primitives • A queue (like a mail box) is associated with each message destination. • Sending processes add messages to the queue. • Receiving processes remove messages from the queue. Queue(Mail Box) Send Receive (block) if Queue is empty! block if Queue is full! 11
Message destinations • Depending on operating systems and distributed systems environment: Message Destinations OS & Distributed Location system environment independent Process V yes Ports Mach, Chorus & Amoeba Yes Sockets BSD 4.x UNIX No Group of processes Chorous Yes Objects Clouds, Emerald Yes 12
Client-Server Communication • The OSI model generates a substantial overhead in LAN-based distributed systems. N+1 N+1 N+1 Data N+1 Data N N N+1 Data N N N+1 Data N-1 N-1 N N+1 Data N-1 N-1 N N+1 Data Communication Path Communication Path 13 Transmitting Site (Adds Headers) Receiving Site (Strips Headers)
Something more is needed.... • Use the client-server model which is based on request-reply protocol. • The client-server model structures the operating system as a group of co-operating processes called servers. • The servers offer services to clients. • The client-server machines run all on the same microkernel as user processes. 14
The request-reply protocol • The client sends a message to the server asking for some service. • The server does the work and returns the data/error code back to client. Server DoOperation .......... (Wait) .......... (Continuation) GetRequest Execute request Send Reply Client Request Message Reply Message Kernel Kernel 15
Advantages of request-reply protocol • No routing is required • Simplicity and efficiency: • layer 5 defines a set of legal requests and reply primitives. No routing or session management. • The stack is shorter and works efficiently 7 6 5 4 3 2 1 Request/reply • DoOperation() • GetRequest() • SendReply() Data Link Physical request-reply primitives 16
Request-reply primitive: DoOperation • Client uses it to invoke remote operation: PROCEDURE DoOperation (serverportId, requestMessage, replyMessage ) send request to • Implementation uses: • ASendoperation • followed by a Receiveoperation and receive reply 17
Request-reply primitives: GetRequest and SendReply • Server uses GetRequest to acquire service requests and • uses SendReply to send the reply to the client. • When the reply message is received by the client the original DoOperation is unblocked. PROCEDURE GetRequest (serverportId, requestMessage) PROCEDURE SendReply (serverportId, replyMessage ) 18
Request-Reply message structure • The information to be transmitted in a request or reply message looks like: Message Type (Request, Reply) Generated by DoOperation for each request. The server copies it into the corresponding reply message RequestId ProcedureId (a server procedure) Arguments (*flattened list) 19
Group Communication • Communication between one process and a group of processes is not best done in a pair-wise exchange • Instead a multicast operation is more appropriate where group membership is transparent to the sender • A very useful infrastructure for distributed systems offering: • Fault tolerance based on replicated objects • Easy to locate Discovery Services for spontaneous networking (Mobile + other devices) • Propagation of event notification • Example: Jini system (see next slide) uses multicast to inform interested clients when a new lookup service advertises its existence
Jini System • A service-oriented architecture (SOA): • SOA takes the existing software components on the network and lets them to be published, invoked and discovered by each other. • Web services will be regarded as an SOA implementations
Jini Architecture • Service provider • Service registry • Service requestor
Socket class in Java • A socket is a network access point that can be used by an application to send or receive data • TCP connections are created using the Socket class. • When you create the client socket you must supply a remote address and port number
The Socket class in Java • The socket on creation in Java connects to a remote machine on a given port to a given host. • The local address and port numbers are assigned automatically • Once a connection has been created we can transfer streams between remote applications.
More on Sockets • Each socket connection can be defined by a protocol, port number and a host address • So • TCP,80,128.1.20.10 • is different to • UDP,80,128,1,20,10
TCP Client Sockets in Java • Can be used to send a data stream of bytes over the network s=new Socket(stmail.staffs.ac.uk,110) • This creates a socket connecting to the server stmail.staffs.ac.uk • On port 110 (POP3, Post Office Protocol) • This socket could be then used to download from the mail server stmail.staffs.ac.uk
Receiving data from a socket • To receive data from a socket, you can receive it by creating a BufferedReader from the socket’s input stream; input= new BufferedReader (new inputStreamReader(my_socket.getInputStream())); • To read a string from the socket: my_string=input.readline();
Writing Data to a Socket • Easiest way to send text data to a socket is to send it a PrintWriter; create the PrintWriter like this: out=new PrintWriter(smtp_socket.getOutputStream(),true); • Now send data to the socket like this out.println(“USER mail01”);
Send and Receive • To send/receive objects create your IO stream as • ObjectOutputStream • ObjectInputStream • To send/receive arrays of bytes create your IO stream as • DataOutputStream • DataInputStream • Closing (important conserves resources !!) • my_sock.close()
Example: Getting Web Pages with a client socket(source code handout: Grab-page) • Using a socket to download pages from the Web Web Server host GrabPage Get /file.html HTTP/1.0 HTTP http://host/file.html Internet <html><head><title>....
Exploring the code • The program sits in a loop waiting for the user to type in URLs • For each URL it creates a new GrabPage object. • This object attempts to connect to the Web server, and download the specified page; • If successful, it displays the page contents on the screen
Exploring the code .... The class GrabPage • downloads and displays the Web-page • It first parses the URL (dissect()) • then connects to the server(connect()) • Issues an HTTP request and displays the response (grab())