Title: Interprocess CommunicationPC
1Interprocess CommunicationPC
2Interprocess 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
3Building 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 and Receive operations
- Synchronous and asynchronous communication
- Message destinations
2
4External 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
5External 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
6Marshaling
- Take a collection of data items and assemble them
into a suitable form for transmission. - Marshaling flattens 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
There 2001
5
7Send 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
8Blocking 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
9Non-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
10Timeouts 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
11Synchronous 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
12Buffered 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
13Message 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
14Client-Server Communication
- The OSI model generates a substantial overhead in
LAN-based distributed systems.
N1
N1
N1 Data
N1 Data
N
N N1 Data
N
N N1 Data
N-1
N-1 N N1 Data
N-1
N-1 N N1 Data
Communication Path
Communication Path
13
Transmitting Site (Adds Headers)
Receiving Site (Strips Headers)
15Something 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
16The 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) .......... (Continu
ation)
GetRequest Execute request Send Reply
Client
Request Message
Reply Message
Kernel
Kernel
15
17Advantages 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
18Request-reply primitive DoOperation
- Client uses it to invoke remote operation
PROCEDURE DoOperation (serverportId,
requestMessage, replyMessage )
send request to
- Implementation uses
- A Send operation
- followed by a Receive operation
and receive reply
17
19Request-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
20Request-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
21(No Transcript)
22Group 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
23Jini 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
24Jini Architecture
- Service provider
- Service registry
- Service requestor
25Socket 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
26The 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.
27More 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
28TCP Client Sockets in Java
- Can be used to send a data stream of bytes over
the network - snew 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
29Receiving data from a socket
- To receive data from a socket, you can receive it
by creating a BufferedReader from the sockets
input stream - input new BufferedReader
- (new inputStreamReader(my_socket.getInputStream(
))) - To read a string from the socket
- my_stringinput.readline()
30Writing Data to a Socket
- Easiest way to send text data to a socket is to
send it a PrintWriter create the PrintWriter
like this - outnew PrintWriter(smtp_socket.getOutputStream(),
true) - Now send data to the socket like this
- out.println(USER mail01)
31Send 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()
32Example 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
lthtmlgtltheadgtlttitlegt....
33Exploring 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
34Exploring 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())