IPC: Inter-process communication in C programming

César Przybyla
6 min readApr 12, 2017

--

My journey through the second year of my I.T school had gotten me in a quite interesting project: “LemIPC

The goal was to create a arena-game using IPCs, “Inter-process communication”, each player (IA) controlled by a process.

The first player to join have to create the needed IPCs and the arena.
The team of the player is defined in a parameter of the binary.
A player is “dead” whenever two player of the same enemy team are in contact with him (the 8 squares that surrounds him).

Players must be able to talk via “Message queue”, and can access to the map using “Shared memory”.

Because the subject was free, i guess the goal was to give us a free experimentations with IPCs by creating a rather fun and simple game.

Inter-Process Communication (IPC)

An IPC is a mechanism that allow processes to communicate and share data.
There exists a lot of different IPCs that can be used very differently. Being able to choose which one can specifically solve your issue is a good skill to have.
Fortunatly, i’ll now present some IPCs and their usage:

File storage

Protect, open, write and close. As simple as that.

Let’s start with the most basic one: using a file as a shared memory where processes can store and exchange data. It can be achieved by simply writing to a file. But be careful, having multiple processes writing into one file can be a really bad idea, so setting up a protection should be a primordial thing to do. But i’ll get onto that protection thing later.

(note: Multiple-accessing for read purposes isn’t as critical as writing.)

Signal

While being simple, Signal is a specific — yet useful — C library.

It lets you create a function who will react if a signal is received. A signal is a asynchronous notification that can be send to a process.There exists a lot of them, which always have their own utilities and usage. For example, pressing CTRL + C while a program is opened causes the signal SIGINT, which interrupts the process.

It can be used to communicate in binary, for example, with one signal representing a 0, and another one being the 1. The big downside of signal is that you have to know the PID (Parent Process Identifier) of the signal-receiver process.

Sockets

A socket is a data stream that can be send either to another process located on the same computer, or via a network.

It can be used to receive or send data, and multiple processes can connect to it. Because of his convenient way of working, and the multiple ways to use it, this is one of the most commonly used IPC.

Message Queues

Message queues are a sort-of mash up between Signals and Sockets. It allows you to create a data stream of messages which can allow one or multiple processes to communicate between them. Sending and receiving can be achieved by using a simple function.

While the usage is pretty simple, i find the Message Queue usage to be somewhat limited and shows its true potential only in very specifics situations.

Shared memory

Shared memory is like telepathy for processes.

After seing all of theses IPCs, you might think that IPCs are not offering enough flexibility. Well, shared memory might be exactly what you need.

It allow you to create a memory segment which can be accessed and modified by multiple process. Setting it up is incredibly easy (and similar to Message Queues), and managing the given data segment might be the only tricky part.

You will still want to check the actions of the processes to prevent two threads accessing the same memory segment. Using semaphores is a fitting strategy, because of the simple usage and its viability if well used.

There are a few more IPCs commonly used which i won’t present, because of my lacking experience with some of well. I’d recommend checking some documentation if multi threading is something you would like to do.

Locking resources

One common thing to do with IPCs, is to create a lock to prevent two processes from writing the same memory segment.

There exists multiple ways to do it, and you can even get a little creative to achieve it.

I’ll present the most flexible one: semaphores. It allows you to create a shared value with your processes. As simple as it is, you can create a lot of ways to manage your IPCs. For example, setting the value to 0 to communicate to the other processes that you are writing and they should not interfere. Don’t forget to reset the value when you are done with the writing, and you should be good to go.

Deadlocks

P1 need the R1 which is locked by P2 which is waiting for R2, locked by P1.

As wise as it is, you might get Deadlocks. It’s a state where two or more processes are waiting for each others to release a resource. They can appear if the locking of the resources was done poorly, or because of an algorithm failure, so be sure to check every situation where two processes are locking a resource.

Our IPCs usage

As the subject stated, we used Shared Memory, Message Queue, and Semaphores to protect multiple processes writing at the same time.

The first 400 characters of the shared memory were the 20x20 arena, where the players can fight. Players are appearing randomly in the arena, and they were represented by the number of their team. (Empty squares were ‘X’)
Characters 401–410 were containing the numbers of players who were in the game. This was initially to stop players from playing and make them wait if there wasn’t enough of them to start a game, but due to a Segmentation Fault, we disabled it.

The Shared memory was successfully created, but no player was actually able to send messages.

Our AI

Because of the lack of time we got at the end of the project, our last build had a random AI.

We still had planned to create some different AIs, so different teams would play differently. The goal was to create 3 types of gameplay — Offense, Defense, Controlled random — which would of been much fun to watch if we could’ve done it.

In the end, our project was a failure to some points (we crashed during the presentation) but i think we learned a lot about IPCs.

I personally think that the free subject was a really good idea, giving us the choice of how to think the project more than ever. The only downside is that i got lost in some point because of the lack of some indications.

Now that this project is over, i hope that at some point of my second year in my IT school, we’ll get a similar subject.

Thank you for reading this,

César Przybyla

--

--