Download IPC

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CSS434 Interprocess Communication
Textbook Ch4
Professor: Munehiro Fukuda
CSS434 IPC
1
Outline





Java sockets
Object serialization
Blocking/non-blocking communication
Buffering
Error handling


Idenpotency
Exactly-one semantics
CSS434 IPC
2
Middleware Layers
Applic ations, services
RMI and RPC
This
c hapter
reques t-reply protocol
Middlew are
lay ers
marshalling and ex ternal data representation
UDP and TCP
CSS434 IPC
3
Sockets and Ports
socket
any port
agreed port
socket
message
client
server
other ports
Internet address = 138.37.94.248
Internet address = 138.37.88.249
CSS434 IPC
4
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
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
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
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; }
};
MainObj
SuObj
Read/write
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
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
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
CSS434 IPC
10
Blocking/Non-Blocking
Communication

Blocking communication

TCP, UDP, and other communication packages



Rendezvous



Client: blocked only when the destination buffer is full
Server: blocked if no message has arrived from the client.
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
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
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
Buffering

message

message

message
message
message
message
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( )
CSS434 IPC
14
Failure Handling
client
server
request
ack
server
client

request
timeout
request 2
response
response

timeout

response 2
request
request
ack
response

ack
timeout
request 2
CSS434 IPC
Loss of request
message
Loss of response
message
Unsuccessful
execution of request
Do we really need
acknowledgment
messages?
15
Idempotency
client
server
request
server
request
client

Timeout
request 2
response

Timeout
A pair of request and
response is enough to
handle faults
Idempotency assumed:

request 3

Timeout
At-least one semantics
Last-one semantics
response
request 4
reesponse 2
CSS434 IPC
16
Exactly-One Semantics

Withdraw $100
Withdraw995 $100
$1000-$100
= $900
Not received
Withdraw $100
$100 received
$1000-$100

= $900 for Trans995
Not received
Withdraw995 $100
Trans995 completed
No subtraction
$900-$100
=$800!
$100 received
CSS434 IPC
What if errors in
the banking
system
New semantics
required:
 Exactly-one
semantics
 Server must
keep track of
the request
sequence
17
Exercises (No turn-in)
1.
2.
3.
Consider the pros and cons of polling and interrupt in nonblocking communication.
Consider an example inducing an accidental system hangup (named a deadlock) in no-buffering communication.
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
Related documents