04-Interprocess Communication

Java sockets Object serialization Blocking/non-blocking communication Buffering Error handling --Idenpotency --Exactly-one semantics
展开查看详情

1.CSS434 Interprocess Communication Textbook Ch4 Professor: Munehiro Fukuda CSS434 IPC 1

2.Outline  Java sockets  Object serialization  Blocking/non-blocking communication  Buffering  Error handling  Idenpotency  Exactly-one semantics CSS434 IPC 2

3. Middleware Layers Applications, services RMI and RPC request-reply protocol Middleware This layers chapter marshalling and external data representation UDP and TCP CSS434 IPC 3

4. Sockets and Ports agreed port socket any port socket message client server other ports Internet address = 138.37.94.248 Internet address = 138.37.88.249 CSS434 IPC 4

5. Java 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 serverPort = 7896; s = new Socket(args[1], serverPort); DataInputStream in = new DataInputStream( s.getInputStream()); DataOutputStream out = new DataOutputStream( s.getOutputStream()); out.writeUTF(args[0]); // UTF is a string encoding see Sn 4.3 String data = in.readUTF(); System.out.println("Received: "+ data) ; }catch (UnknownHostException e){ System.out.println("Sock:"+e.getMessage()); }catch (EOFException e){System.out.println("EOF:"+e.getMessage()); }catch (IOException e){System.out.println("IO:"+e.getMessage());} }finally {if(s!=null) try {s.close();}catch (IOException e) {System.out.println("close:"+e.getMessage());}} } CSS434 IPC 5 }

6. Java TCP Server import java.net.*; import java.io.*; public class TCPServer { public static void main (String args[]) { try{ int serverPort = 7896; ServerSocket listenSocket = new ServerSocket(serverPort); while(true) { Socket clientSocket = listenSocket.accept(); Connection c = new Connection(clientSocket); } } catch(IOException e) {System.out.println("Listen :"+e.getMessage());} } } // this figure continues on the next slide CSS434 IPC 6

7. Java TCP Server (Cont’d) class Connection extends Thread { DataInputStream in; DataOutputStream out; Socket clientSocket; public Connection (Socket aClientSocket) { try { clientSocket = aClientSocket; in = new DataInputStream( clientSocket.getInputStream()); out =new DataOutputStream( clientSocket.getOutputStream()); this.start(); } catch(IOException e) {System.out.println("Connection:"+e.getMessage());} } public void run(){ try { // an echo server String data = in.readUTF(); out.writeUTF(data); } catch(EOFException e) {System.out.println("EOF:"+e.getMessage()); } catch(IOException e) {System.out.println("IO:"+e.getMessage());} } finally{ try {clientSocket.close();}catch (IOException e){/*close failed*/}} } } CSS434 IPC 7

8. Serialization Manual Operations in C++ class SubObject { public: int id; SubObject( int I ) { id = I; } void print( ) { cout << “SubObject: id=“ << id << endl; } MainObj }; class MainObject { public: SuObj int id; SubObject *subObj; Read/write MainObject( int I, SubObject *s ) { id = I; subObj = j; } void print( ) { cout << “MainObject: id=“ << id << endl; } }; Int main( int argc, char** argv ) { int f1 = create( “sample.dat”, 0744 ); MainObject *obj1 = new MainObject( 1, new SubObject( 2 ) ); write( f1, obj1, sizeof( *obj1 ) ); write( f1, obj1->subObj, sizeof( *obj1->subObj ) ); //manually write data pointed to by obj1 close( f1 ); int f2 = open( “sample.dat”, O_RDONLY ); MainObject *obj2 = new MainObject( 0, new SubObject( 0 ) ); read( f2, obj2, sizeof( MainObject ) ); read( f2, obj2->subObj, sizeof( SubObject ) ); //manually read data pointed to by obj2 close( f2 ); obj2->print( ); obj2->subObj->print( ); } CSS434 IPC 8

9. Serialization Automatic Operations in Java public class SubObject implements Serializable { private int id; public SubObject( int I ) { id = I; } public void print( ) { System.out.println( “SubObject: id =“ + id ); } } public class MainObject implements Serializable { public int id; public SubObject subObj; public MainObject( int I, SubObject s ) { id = I; subObj = s; } public void print( ) { System.out.println( “MainObject: id =“ + id ); } } Public class Test { public static void main( String args[] ) throws IOException, ClassNotFoundException { FileOutputStream f1 = new FileOutputStream( “sample.dat” ); ObjectOutputStream o = new ObjectOutputStream( f1 ); MainObject obj1 = new MainObject( 1, new SubObject( 2 ) ); o.writeObject( obj1 ); //automatically write all objects traceable from obj1 o.flush( ); o.close( ); FileInputStream f2 = new FileInputStream( “sample.dat” ); ObjectInputStream i = new ObjectInputStream( f2 ); MainObject obj2 = (MainObject)I.readObject( ); // automatically read all object traceable from obj2 I.close( ); obj2.print( ); obj2.subObj.print( ); } } CSS434 IPC 9

10. Serialization Serializable or Not Serializable  Serializable  Classes implementing the Serializable interface  Primitive data types such as int and double and th eir arrays  Not Serializable  Image and Thread classes  Static variables  Variables quantified with Transient  Zero or null initialized upon a de-serialization CSS434 IPC 10

11.Blocking/Non-Blocking Communication  Blocking communication  TCP, UDP, and other communication packages  Client: blocked only when the destination buffer is full  Server: blocked if no message has arrived from the client.  Rendezvous  Client: blocked for a server to receive a message  Server: blocked if no message has arrived from the client.  Non-blocking communication  Server does not want to be blocked when  It may receive a message from a different client.  It has another jobs to be done such as computation or message transmission.  Some synchronization is necessary later. CSS434 IPC 11

12. Non-Blocking Communication C/C++ Example  Polling  Periodically check if a socket is ready to read data:  Example: sd = socket( AF_INET, SOCKET_STREAM, 0); set_fl(sd, O_NONBLOCK); // set the socket as non-blocking struct pollfd pfd; pfd.fd = sd; poll( &pfd, 1, timeout ) // poll the socket status  Interrupt  Notified from the system when a socket is ready to read data;  Example: sd = socket(AF_INET, SOCKET_STREAM, 0); signal(SIGIO, sigio_func); // set a future interrupt to call sigio_func( ) fcntl(sd, F_SETOWN, getpid( )); // ask OS to deliver this fd interrupt to me fcntl(sd, F_SETFL, FASYNC); // set this fd asynchronous int sigio_func( ) { // invoked upon an interrupt } CSS434 IPC 12

13. Non-Blocking Communication Java Example  Polling  Periodically check if a socket is ready to read data:  Example 1: socket = new Socket( server, port ); rawIn = socket.getInputStream( ); in = new DataInputStream( rawIn ); if ( rawIn.available( ) > 0 ) { String str = in.readUTF( ); System.out.println( str ); }  Example 2: ServerSocket server = new ServerSocket( port ); server.setSoTimeout( 500 ); //will be blocked for 500ms upon accept Socket client = null; try { client = server.accept( ); } catch ( SocketTimeoutException e ) { }  Interrupt  Difficult to implement asynchronous/interruptible communication in Java  Reason 1: There are no methods corresponding to fctl( ).  Reason 2: JVM itself receives external interrupts. CSS434 IPC 13

14. Buffering  No Buffering  A message remains on the sender until the receiver issues receive( ). message  Rendezvous  Performance drawback  Single Message Buffer  The sender can send at most one message even if the receiver has not issued receive( ). message  Stop-and-wait protocol  A message can be kept read in advance.  What if the sender has multiple messages?  Finite-Bound Buffer message  Unsuccessful communication - Go- message Back-N Technique message  Flow-controlled communication - sliding window in TCP message  Socket: capable of changing its buffer size with setsockopt( ) CSS434 IPC 14

15. Failure Handling client server client server  Loss of request request request message ack request 2 timeout  Loss of response response response timeout message response 2  Unsuccessful execution of request request request ack ack  Do we really need response timeout request 2 acknowledgment messages? CSS434 IPC 15

16. Idempotency client server client server request  A pair of request and response is enough to request Timeout handle faults request 2  Idempotency assumed: response Timeout  At-least one semantics request 3  Last-one semantics Timeout response request 4 reesponse 2 CSS434 IPC 16

17. Exactly-One Semantics  What if errors Withdraw $100 Withdraw995 $100 in the banking system $1000-$100 = $900 $1000-$100  New semantics = $900 for Trans995 required: Not received Not received  Exactly-one Withdraw $100 Withdraw995 $100 semantics Trans995 completed $900-$100 No subtraction  Server must =$800! keep track of $100 received $100 received the request sequence CSS434 IPC 17

18. Exercises (No turn-in) 1. Consider the pros and cons of polling and interrupt in no n-blocking communication. 2. Consider an example inducing an accidental system hang -up (named a deadlock) in no-buffering communication. 3. Which of the following operations are idempotent? 1. cin >> data; 2. ifstream infile(“input.txt”); infile.seek( ); 3. cout << data; 4. int a = 1, b = 2, c; c = a + b; 5. int c = 1; c++; CSS434 IPC 18