Inter-Process Communication

There are two ways to communicate from one process or another.

  • You either let them share memory

  • or you let them pass message to each other

Both ways require synchronization via mutexes. In general, IPC is a mechanism provided and supported by operating system. It should be a feature come right out of the box.

The common approaches are

  • Message passing by sockets, pipes, message queues

  • Share memory by providing a memory mapped file that can be shared among two or more processes.

However, I personally believe that is very little incentive to share memory between processes. It is neither scalable or reliable.

Message-based IPC

Processes send and receive messages

  • OS creates and maintains a channel to allow such passing to happen.

  • OS provides interface to processes like a port!

  • Kernel provides the logic to establish communication and perform each IPC operation

Pipes

The simplest form of IPC is a pipe. It is characterized by two endpoints. There is no concept of messages in pipe, instead it is just a stream of bytes being pushed from producer to consumer. The more complex form of pipe is a message queue. It requires structure for each packet of bytes.

Sockets

Another common form of message based communication is socket. The socket API supports send and receive operations that allow processes to send message buffers in and out of the in-kernel communication buffer. The API also performs any necessary kernel-level processing like for TCP/IP.

If we are talking about machine to machine communication, then the buffer will exist on the network device. The kernel is responsible to talk to network device to perform any message processing.

Shared Memory IPC

Processes will read and write to a shared memory region. OS will establish a shared buffer between the processes.

  1. Physical pages are mapped into virtual address space.

  2. Then virtual addresses from P1 and P2 wil map to the same physical space.

POSIX Shared Memory API

It uses a file to perform communication. These are not the regular files that we can access from a filesystem. Instead they are files that exist only within the so called tmpfs file system. It is intended to look and feel like a file system. In essence, they are just bunch of states that are present in the physical and violatile memory.

Each shared memory segment or region is referenced by a file descriptor.

  • shm_open() opens a file and returns a file descriptor.

  • mmap() and unmap() map virtual addresses to physical addresses.

  • shm_close() removes the file descriptor from the address space of the process.

  • shm_unlink() will destroy the shared memory.

Last updated