Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSS434 IPC1 CSS434 Interprocess Communication Textbook Ch4 Professor: Munehiro Fukuda.

Similar presentations


Presentation on theme: "CSS434 IPC1 CSS434 Interprocess Communication Textbook Ch4 Professor: Munehiro Fukuda."— Presentation transcript:

1 CSS434 IPC1 CSS434 Interprocess Communication Textbook Ch4 Professor: Munehiro Fukuda

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

3 CSS434 IPC3 Middleware Layers

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

5 CSS434 IPC5 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());}} }

6 CSS434 IPC6 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

7 CSS434 IPC7 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*/}} }

8 CSS434 IPC8 Serialization Manual Operations in C++ class SubObject { public: int id; SubObject( int I ) { id = I; } void print( ) { cout << “SubObject: id=“ << id << endl; } }; class MainObject { public: int id; SubObject *subObj; 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 ) ); //manually write data pointed to by 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 ) ); //manually read data pointed to by obj2 read( f2, obj2->subObj, sizeof( SubObject ) );//manually read data pointed to by obj2 close( f2 ); obj2->print( ); obj2->subObj->print( ); } MainObj SuObj Read/write

9 CSS434 IPC9 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 ) ); //automatically write all objects traceable from obj1 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 ); // automatically read all object traceable from obj2 MainObject obj2 = (MainObject)I.readObject( ); // automatically read all object traceable from obj2 I.close( ); obj2.print( ); obj2.subObj.print( ); }

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

11 CSS434 IPC11 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.

12 CSS434 IPC12 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 }

13 CSS434 IPC13 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.

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

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

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

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

18 CSS434 IPC18 Exercises (No turn-in) 1. Consider the pros and cons of polling and interrupt in non- 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++;


Download ppt "CSS434 IPC1 CSS434 Interprocess Communication Textbook Ch4 Professor: Munehiro Fukuda."

Similar presentations


Ads by Google