Beginners Guide to InterProcess Communication

Interprocess Communication (what is it?)

Interprocess communication (or IPC for short) is the method of sending information between two different processes or threads that are either on the same machine, or even over the internet. As technology advances in the world, the amount of tasks that are delegated between different threads, processes, and even machines is growing exponentially, and the only way to communicate effectively between these tasks, and make sure that every single one of them is getting the correct information and processing it correctly. This is why IPC is so important: the only way to allow us to use much more powerful machines at the same time is to use IPC in order to communicate between them.

Methods of IPC

There are many ways of doing IPC, and in reality, they are all part of two buckets: sending information between processes/threads in a single machine, and communicating between machines. In this article, I will not go into depth into any one of these methods; I will just be going over what they are, how they work, and how to set them up (in the C programming language), all at the most basic level.

The methods I will be going over are:

  • Signals
  • Pipes
  • Named pipes (aka fifos)
  • Sockets

Signals

Signals were there since the beginning of time (or rather beginning of computers). They were originally made to send messages over physical wires, during the time of cicruits. Nowadays, however, we can use signals to send short, single value messages to programs.

How it works

Each signals is just an integer, which is sent to a process through a separate thread. A list of signals, and their integer values, is available in /usr/include/asm/signal.h.

How to use them

In this file, you will also see a struct called sighandler. This is the struct that has to be implemented in order to handle the signals that come to the process, and you can give it any action to do when it recieves any signal, in the form of a function. In order to send a signal to a process, you can use the kill command: kill -s <signal> <pid>.

Pipes

cat names.txt | grep "nikhil chatterjee". The famous misuse of grep - but thats a different story. From this example, I want to point out the |, or the pipe operator. The function of this operator is simple: it allows the standard output of the first process (in this case cat) and redirects it to the standard input of the second process (in this case grep).

Usage

The pipe is actually a service provided directly by the operating system, through the pipe(2) system call. This sytem. call provides you with two file descriptors: one read side and one write side. The most common use case is usage with the fork(2) system call, which creates an entirely new process from that point in the execution. When fork is called, the file descriptors that were provided by pipe are duplicated: the write pipe in the parent process can be read from the second process, and vice versa.

Named Pipes (fifos)

A fifo in linux is a special type of file (after all, everything in linux is a file), which you create with the mkfifo(1) command, eg. mkfifo test. If you echo a piece of text into this file, with echo "hello world" > test, you will notice something interesting: the command is not exiting, but rather the shell is blocked. At this point, if you check the size of the fifo with du -hs test, you will see that the size is 0 bytes, which should not be the case if the bytes of “hey” were written to the file. With this, we make our first observation: the data written to it is not backed by any hard drive space, but rather all of it is stored in the RAM. Now, if you read the contents from the fifo, with cat test(in another terminal, of course), you will then see hey printed to the console, and the echo command now exits successfully. Now comes our second observation: the write operation to a fifo blocks the thread until the data has been read from the other side. This is where the name fifo originates from, as the first data to be written to the fifo is the first data read from the other side. You can also see this if you write twice to the fifo: open three terminals, and echo some text from two of them and cat from the fifo in the third.

Usage

Fifos are not just used in the terminal to have some fun with echo and cat, but can be used in your code as well. First of all, you can create a fifo in the working directory with mkfifo(3). Now, you can use it just as you would use a regular file from within your code: use open(2) and close(2) to open and close, and read(2) and write(2) calls to modify the contents. Remember, these calls will be blocked until both ends of the fifo are opened.

Sockets

Sockets are probably the most commonly used form of IPC. They can be used from any place, any time, and even across the internet, as long as you have an internet connection. As long as you have an open port on your computer, you can use it to feed it data and read from it. In fact, when you open google.com, you are actually reading from the socket listening on port 443 at the IP address that google.com points to, which is (at the time of writing this article): 172.217.5.110.