Like this article? We recommend

Like this article? We recommend

The examples for this section are written to the latest published ISO standard for the C programming language, commonly known as C99. All POSIX-compliant systems, including any that ship with GCC, should include an executable called c99, which can be used to compile them using the command 'c99 {sourcefile}'. A working knowledge of C99 is considered a pre-requisite of this tutorial, and some experience with POSIX systems is considered useful.

Part 1 of this series looked at how to create processes and how to share
memory among them. This article discusses how to deal with largely independent
processes that communicate by exchanging messages.

Message-passing processes are generally much easier to reason about than
those that rely on shared memory and locking. The messy parts of the low-level
IPC are generally hidden from the developer, and you just have to worry about
making sure that your processes are sufficiently independent.

Processes communicating in this way can also be used for security; if you
have to run a part of your program as root (for example, to bind to a
low-numbered socket, or to run programs as other users), you can isolate this
part in one process, which is easier to audit, and run the rest at a lower
privilege level.

NOTE

The examples in this article can be downloaded in the accompanying
source.zip.

The Theory

Message-passing processes typically have a run loop that involves a receive
> process > send cycle. They take a message from the queue, process it,
and then send a result. In this respect, the initial message can be thought of
as an asynchronous function call.

Some function calls are already implemented like this under the hood. When
you write some data to disk, typically the function you call will copy the data
into a kernel buffer and the kernel will asynchronously write the data to disk.
Often, it’s a good idea to do the same with your code—create an
interface to the other process that looks like a set of functions. Then you can
change the mechanism used for sending the messages, without having to modify
your code in lots of places.