Lecture 6 Interprocess Communication
Lecture 6 Interprocess Communication
INTERPROCESS
COMMUNICATION
Prof. Cheruiyot W.K, PhD
TTU
Topics
INTRODUCTION
The API for the INTERNET PROTOCOLS
EXTERNAL DATA REPRESENTATION
CLIENT-SERVER COMMUNICATION
GROUP COMMUNICATION
2
INTERPROCESS COMMUNICATION
Interprocess Communications
- Exchange of data between two or more separate,
independent processes/threads.
- Operating systems provide facilities/resources for inter-
process communications (IPC), such as message
queues, semaphores, and shared memory.
- Distributed computing systems make use of these
facilities/resources to provide application programming
interface (API) which allows IPC to be programmed at a
higher level of abstraction. (e.g., send and receive)
- Distributed computing requires information to be
exchanged among independent processes.
3
INTERPROCESS COMMUNICATION
P roc es s 1 P roc es s 2
data
5
INTERPROCESS COMMUNICATION
W e b s e rv e r
S1 S2 S3 S4
o pe ra ti o n s :
H TTP S 1 : a cce pt co n n e cti o n
a pro ce s s S 2 : r e c e i ve ( r e q u e s t )
re qu e s t
S 3 : s e n d (re s po n s e )
a n o pe ra ti o n Ss43 : d i s c o n n e c t
H TTP
C 1 : m a k e co n n e cti o n
re s po n s e
C 2 : s e n d (re qu e s t)
da ta fl o w C 3 : r e c e i ve ( r e s p o n s e )
C1 C2 C3 C4
C 4 : di s co n n e ct
W e b bro ws e r
Event Synchronization
Interprocess communication may require
that the two processes synchronize their
operations: one side sends, then the other
receives until all data has been sent and
received.
Ideally, the send operation starts before
the receive operation commences.
In practice, the synchronization requires
system support.
7
INTERPROCESS COMMUNICATION
8
INTERPROCESS COMMUNICATION
Introduction
The java API for interprocess
communication in the internet provides
both datagram and stream
communication.
The two communication patterns that are
most commonly used in distributed
programs:
Client-Server communication
The request and reply messages provide
the basis for remote method invocation
(RMI) or remote procedure call (RPC).
9
INTERPROCESS COMMUNICATION
Introduction
Group communication
The same message is sent to several
processes.
10
INTERPROCESS COMMUNICATION
Introduction
This chapter is concerned with
middleware.
Introduction
Remote Method Invocation (RMI)
It allows an object to invoke a method in
an object in a remote process.
E.g. CORBA and Java RMI
Remote Procedure Call (RPC)
It allows a client to call a procedure in a
remote server.
12
INTERPROCESS COMMUNICATION
Introduction
The application program interface
(API) to User Datagram Protocol (UDP) provides a message
passing abstraction.
Message passing is the simplest form of interprocess
communication.
API enables a sending process to transmit a single
message to a receiving process.
The independent packets containing theses messages
are called datagrams.
In the Java and UNIX APIs, the sender specifies the
destination using a socket.
Introduction
Socket is an indirect reference to a
particular port used by the destination
process at a destination computer.
The application program interface (API) to
TCP provides the abstraction of a two-way
stream between pairs of processes.
Introduction
Request-reply protocols are designed to
support client-server communication in the
form of either RMI or RPC.
15
INTERPROCESS COMMUNICATION
Introduction
Group multicast is a form of interprocess
communication in which one process in a
group of processes transmits the same
message to all members of the group.
16
INTERPROCESS COMMUNICATION
17
SYSTEM
INTERPROCESS MODEL
COMMUNICATION
18
INTERPROCESS COMMUNICATION
19
INTERPROCESS COMMUNICATION
20
INTERPROCESS COMMUNICATION
21
INTERPROCESS COMMUNICATION
22
INTERPROCESS COMMUNICATION
Socket Creation in C
int s = socket(domain, type, protocol);
s: socket descriptor, an integer (like a file-handle)
domain: integer, communication domain
e.g., PF_INET (IPv4 protocol) – typically used
type: communication type
SOCK_STREAM: reliable, 2-way, connection-based service
SOCK_DGRAM: unreliable, connectionless,
other values: need root permission, rarely used, or obsolete
protocol: specifies protocol (see file /etc/protocols for a list of
options) - usually set to 0
NOTE: socket call does not specify where data will be coming
from, nor where it will be going to; it just creates the interface.
24
INTERPROCESS COMMUNICATION
Sockets
Internet IPC mechanism of Unix and other
operating systems (BSD Unix, Solaris,
Linux, Windows NT, Macintosh OS)
Processes in the above OS can send and
receive messages via a socket.
Sockets need to be bound to a port
number and an internet address in order to
send and receive messages.
Each socket has a transport protocol (TCP
or UDP).
25
INTERPROCESS COMMUNICATION
Sockets
Messages sent to some internet address
and port number can only be received by a
process using a socket that is bound to
this address and port number.
Processes cannot share ports (exception:
TCP multicast).
26
INTERPROCESS COMMUNICATION
Sockets
Both forms of communication, UDP and
TCP, use the socket abstraction, which
provides and endpoint for communication
between processes.
27
INTERPROCESS COMMUNICATION
Sockets
29
INTERPROCESS COMMUNICATION
30
INTERPROCESS COMMUNICATION
31
INTERPROCESS COMMUNICATION
32
INTERPROCESS COMMUNICATION
33
INTERPROCESS COMMUNICATION
array of bytes containing message | length of message| Internet address | port number|
34
INTERPROCESS COMMUNICATION
35
INTERPROCESS COMMUNICATION
36
INTERPROCESS COMMUNICATION
Figure 3. UDP client sends a message to the server and gets a reply
37
INTERPROCESS COMMUNICATION
38
INTERPROCESS COMMUNICATION
Figure 4. UDP server repeatedly receives a request and sends it back to the client
39
INTERPROCESS COMMUNICATION
40
INTERPROCESS COMMUNICATION
41
INTERPROCESS COMMUNICATION
42
INTERPROCESS COMMUNICATION
44
INTERPROCESS COMMUNICATION
Figure 6. TCP server makes a connection for each client and then echoes the client’s request
45
INTERPROCESS COMMUNICATION
46
INTERPROCESS COMMUNICATION
47
INTERPROCESS COMMUNICATION
48
INTERPROCESS COMMUNICATION
50
INTERPROCESS COMMUNICATION
51
INTERPROCESS COMMUNICATION
52
INTERPROCESS COMMUNICATION
54
INTERPROCESS COMMUNICATION
55
INTERPROCESS COMMUNICATION
57
INTERPROCESS COMMUNICATION
59
INTERPROCESS COMMUNICATION
61
INTERPROCESS COMMUNICATION
Client-Server Communication
The client-server communication is designed to
support the roles and message exchanges in
typical client-server interactions.
In the normal case, request-reply communication
is synchronous because the client process
blocks until the reply arrives from the server.
Asynchronous request-reply communication is
an alternative that is useful where clients can
afford to retrieve replies later.
62
INTERPROCESS COMMUNICATION
Client-Server Communication
Often built over UDP datagrams
Client-server protocol consists of
request/response pairs, hence no
acknowledgements at transport layer are
necessary
Avoidance of connection establishment
overhead
No need for flow control due to small amounts
of data are transferred
63
INTERPROCESS COMMUNICATION
Client-Server Communication
The request-reply protocol was based on a trio
of communication primitives: doOperation,
getRequest, and sendReply shown in Figure 12.
64
INTERPROCESS COMMUNICATION
Client-Server Communication
The request-reply protocol is shown in Figure 12.
Client-Server Communication
The designed request-reply protocol matches requests to
replies.
If UDP datagrams are used, the delivery guarantees
must be provided by the request-reply protocol, which
may use the server reply message as an
acknowledgement of the client request message.
Figure 13 outlines the three communication primitives.
66
INTERPROCESS COMMUNICATION
Client-Server Communication
67
INTERPROCESS COMMUNICATION
Client-Server Communication
The information to be transmitted in a request
message or a reply message is shown in Figure 14.
68
INTERPROCESS COMMUNICATION
Client-Server Communication
In a protocol message
The first field indicates whether the message is a
request or a reply message.
The second field request id contains a message
identifier.
The third field is a remote object reference .
The forth field is an identifier for the method to be
invoked.
69
INTERPROCESS COMMUNICATION
Client-Server Communication
Message identifier
A message identifier consists of two parts:
A requestId, which is taken from an
increasing sequence of integers by the
sending process
An identifier for the sender process, for
example its port and Internet address.
70
INTERPROCESS COMMUNICATION
Client-Server Communication
Failure model of the request-reply protocol
If the three primitive doOperation,
getRequest, and sendReply are
implemented over UDP datagram, they
have the same communication failures.
Omission failure
Messages are not guaranteed to be
delivered in sender order.
71
INTERPROCESS COMMUNICATION
Client-Server Communication
RPC exchange protocols
Three protocols are used for implementing
various types of RPC.
The request (R) protocol.
The request-reply (RR) protocol.
The request-reply-acknowledge (RRA)
protocol.
(Figure 15)
72
INTERPROCESS COMMUNICATION
Client-Server Communication
73
INTERPROCESS COMMUNICATION
Client-Server Communication
In the R protocol, a single request message is
sent by the client to the server.
The R protocol may be used when there is no
value to be returned from the remote method.
The RR protocol is useful for most client-server
exchanges because it is based on request-reply
protocol.
RRA protocol is based on the exchange of three
messages: request-reply-acknowledge reply.
74
INTERPROCESS COMMUNICATION
Client-Server Communication
HTTP: an example of a request-reply
protocol
HTTP is a request-reply protocol for the
exchange of network resources between
web clients and web servers.
75
INTERPROCESS COMMUNICATION
Client-Server Communication
HTTP protocol steps are:
Connection establishment between client
and server at the default server port or at a
port specified in the URL
client sends a request
server sends a reply
connection closure
76
INTERPROCESS COMMUNICATION
Client-Server Communication
HTTP 1.1 uses persistent connections.
Persistent connections are connections that
remains open over a series of request-reply
exchanges between client and server.
Resources can have Multipurpose Internet
Mail Extensions (MIME)-like structures in
arguments and results.
77
INTERPROCESS COMMUNICATION
Client-Server Communication
A Mime type specifies a type and a
subtype, for example:
text/plain
text/html
image/gif
image/jpeg
78
INTERPROCESS COMMUNICATION
Client-Server Communication
HTTP methods
GET
Requests the resource, identified by URL as
argument.
If the URL refers to data, then the web server replies
by returning the data
If the URL refers to a program, then the web server
runs the program and returns the output to the client.
79
INTERPROCESS COMMUNICATION
Client-Server Communication
HEAD
This method is similar to GET, but only
meta data on resource is returned (like date
of last modification, type, and size)
80
INTERPROCESS COMMUNICATION
Client-Server Communication
POST
Specifies the URL of a resource (for
instance, a server program) that can deal
with the data supplied with the request.
This method is designed to deal with:
• Providing a block of data to a data-handling
process
• Posting a message to a bulletin board, mailing
list or news group.
• Extending a dataset with an append operation
81
INTERPROCESS COMMUNICATION
Client-Server Communication
PUT
Supplied data to be stored in the given URL
as its identifier.
DELETE
The server deletes an identified resource by
the given URL on the server.
OPTIONS
A server supplies the client with a list of
methods.
It allows to be applied to the given URL
82
INTERPROCESS COMMUNICATION
Client-Server Communication
TRACE
The server sends back the request
message
83
INTERPROCESS COMMUNICATION
Client-Server Communication
A reply message specifies
The protocol version
A status code
Reason
Some headers
An optional message body
84
INTERPROCESS COMMUNICATION
Group Communication
The pairwise exchange of messages is not
the best model for communication from
one process to a group of other
processes.
A multicast operation is more appropriate.
Multicast operation is an operation that
sends a single message from one process
to each of the members of a group of
processes.
85
INTERPROCESS COMMUNICATION
Group Communication
The simplest way of multicasting, provides
no guarantees about message delivery or
ordering.
Multicasting has the following
characteristics:
Fault tolerance based on replicated
services
A replicated service consists of a group of
servers.
86
INTERPROCESS COMMUNICATION
Group Communication
Client requests are multicast to all the
members of the group, each of which
performs an identical operation.
Finding the discovery servers in
spontaneous networking
Multicast messages can be used by servers
and clients to locate available discovery
services in order to register their interfaces
or to look up the interfaces of other services
in the distributed system.
87
INTERPROCESS COMMUNICATION
Group Communication
Better performance through replicated
data
Data are replicated to increase the
performance of a service.
Propagation of event notifications
Multicast to a group may be used to notify
processes when something happens.
88
INTERPROCESS COMMUNICATION
Group Communication
IP multicast
IP multicast is built on top of the Internet
protocol, IP.
IP multicast allows the sender to transmit a
single IP packet to a multicast group.
A multicast group is specified by class D IP
address for which first 4 bits are 1110 in
IPv4.
89
INTERPROCESS COMMUNICATION
Group Communication
The membership of a multicast group is
dynamic.
A computer belongs to a multicast group if
one or more processes have sockets that
belong to the multicast group.
90
INTERPROCESS COMMUNICATION
Group Communication
The following details are specific to IPv4:
Multicast IP routers
• IP packets can be multicast both on local
network and on the wider Internet.
• Local multicast uses local network such as
Ethernet.
• To limit the distance of propagation of a
multicast datagram, the sender can specify the
number of routers it is allowed to pass- called
the time to live, or TTL for short.
91
INTERPROCESS COMMUNICATION
Group Communication
Multicast address allocation
• Multicast addressing may be permanent or
temporary.
• Permanent groups exist even when there are no
members.
• Multicast addressing by temporary groups must
be created before use and cease to exit when all
members have left.
• The session directory (sd) program can be used
to start or join a multicast session.
• session directory provides a tool with an
interactive interface that allows users to browse
advertised multicast sessions and to advertise
their own session, specifying the time and
duration.
92
INTERPROCESS COMMUNICATION
Group Communication
Java API to IP multicast
The Java API provides a datagram interface
to IP multicast through the class
MulticastSocket, which is a subset of
DatagramSocket with the additional
capability of being able to join multicast
groups.
The class MulticastSocket provides two
alternative constructors , allowing socket to
be creative to use either a specified local
port, or any free local port.
(Figure 18)
93
INTERPROCESS COMMUNICATION
Group Communication
import java.net.*;
import java.io.*;
public class MulticastPeer{
public static void main(String args[]){
// args give message contents and destination multicast group (e.g. "228.5.6.7")
MulticastSocket s =null;
try {
InetAddress group = InetAddress.getByName(args[1]);
s = new MulticastSocket(6789);
s.joinGroup(group);
byte [] m = args[0].getBytes();
DatagramPacket messageOut = new DatagramPacket(m, m.length, group, 6789);
s.send(messageOut);
byte[] buffer = new byte[1000];
for(int i=0; i< 3;i++) { // get messages from others in group
DatagramPacket messageIn = new DatagramPacket(buffer, buffer.length);
s.receive(messageIn);
System.out.println("Received:" + new String(messageIn.getData()));
}
s.leaveGroup(group);
}catch (SocketException e){System.out.println("Socket: " + e.getMessage());
}catch (IOException e){System.out.println("IO: " + e.getMessage());
}finally {if(s != null) s.close();}
}
Figure 18. Multicast peer joins a group and sends and receives datagrams
94
INTERPROCESS COMMUNICATION
Group Communication
A process can join a multicast group with a given
multicast address by invoking the joinGroup
method of its multicast socket.
A process can leave a specified group by invoking
the leaveGroup method of its multicast socket.
The Java API allows the TTL to be set for a
multicast socket by means of the setTimeToLive
method. The default is 1, allowing the multicast to
propagate only on the local network.
95