Inter Process Communication (IPC)

The two basic types of inter-process communication are message passing and shared memory communication.

Shared Memory Communication

In shared memory communication a common memory segment, S, is mapped to the address spaces of process A and process B:

A and B can communicate by leaving messages for each other in S or indirectly, by updating a data structure that resides in S. For example, A may be a producer process that manufactures widgets and places them in a bounded buffer allocated in S. B may be a consumer process that removes widgets from the buffer.

This form of communication is very efficient and doesn't impose limits on the amount of information that may be exchanged (other than the size of S). It does lead to synchronization problems, however. Also, A and B must both be processes running on the same computer. If A and B are on different computers, then the shared memory segment must be a file or database that both processes can read and write.

Message Passing

There are two models of message passing: connectionless and connection-oriented. For connectionless message passing imagine a virtual post office (the network). A puts a message inside a virtual envelope; puts B's address on the envelope; then submits it to a virtual post office, which delivers the letter to B.

class VirtualPostOffice {
   Queue[] messageQueues;
   void send(String message, Address receiver, boolean isBlocking);
   String receive(boolean isBlocking);
}

In connection-oriented message passing, A calls B using a virtual telephone. Once A's "phone" is connected to B's phone, the two may exchange messages through the connection until one hangs up.

class VirtualTelephone {
   int myNumber;
   boolean isConnected;
   void connect(int number);
   void disconnect();
   void send(String msg, boolean isBlocking);
   String receive(boolean isBlocking);
}

Synchronous versus Asynchronous Communication

Assume A and B are processes that exchange messages. When A sends a message to B how will it know that B has received it. If this is important, then A must send the message and wait for an acknowledgement from B:

telephoneA.send(msg, true); // wait for ack
// resume working

If B can't proceed without the message from A, then it must wait for the message to arrive:

msg = telephoneB.receive(true); // wait for message
// process msg

This is called synchronous or reliable message passing.

If A doesn't care if B receives the message, for example, if A broadcasts spam to many receivers, then it can perform a non-blocking send:

for(int i = 0; i < N; i++) {
   postOffice.send(spam, receiver[i], false);
}

If B has other work it can do if A's message hasn't been received, it can perform a non-blocking receive:

msg = postOffice.receive(false);
if (msg == null) {
   // do something else
} else {
   // process msg
}

This is called asynchronous or unreliable message passing.

Implementation

How can these methods be implemented? At the physical level messages are just sequences of high and low voltages pulsing through wires. The trick is layers. Each layer provides send and receive functions. Each layer is more primitive than the layer above it. This design results in a stack of communication layers. Here's a typical network architecture. The dashed arrows represent virtual wires:

Each layer defines one or more protocols for exchanging messages at that level. A protocol describes the structure of a stereotypical conversation between two processes.

Examples of application layer protocols:

FTP, Telnet, HTTP

Examples of transport layer protocols:

UDP, TCP

Examples of (inter) network layer protocols:

IP

Examples of Data link layer protocols:

HDLC, SLIP, PPP

An internet

The following diagram illustrates a simple internet. Hosts A and B are computers connected to local area networks LAN-1 and LAN-2, respectively. The LANs are connected to a wide area network (WAN) by routers 1 and 2, respectively. A client program runs on Host-A. A server program perpetually runs on Host-B at port 80:

The address of the server must specify its LAN, host, and server:

LAN-2.Host-B:80

In the Internet Protocol (IP) the IP address of a host is a string of four bytes of the form:

byte0.byte1.byte2.byte3

Of course each byte is an integer from 0 to 255. Depending on the network class (A, B, or C) the first one, two, or three bytes, respectively, specify the host's network. The remaining bytes specify the host itself.

To identify the server, we append the port number to the IP address:

byte0.byte1.byte2.byte3:port

The port number is an integer between 0 and 65535 with ports 0 - 1023 reserved for well-known services. For example, port 80 is reserved for web servers.

Alternatively, the client can use a symbolic domain name to identify the host:

host.domain-3.domain-2.domain-1:80

For example:

big-blue.ibm.com:80

A domain name server (DNS) automatically translates domain names into IP addresses.