A peek at Inter-Process Communication in Unix / C — Notions
You might already have heard or read about IPC in C / Unix. I understand if you haven’t entirely grasped how it works exactly. I actually discovered it just a few weeks ago in the context of a school project, and I must admit it takes some time to digest. But now, it’s time for me to sit a second and talk about what I’ve learned and what is important to know about this notion.
But.. What does “IPC” mean?
IPC stands for Inter Process Communication. Basically, the components attached to this notion are designed to allow two or more processes to send, receive or share data with each other. This way, it enables the possibility to execute and link various tasks.
“the possibility to execute and link various tasks.”? I didn’t get the point.
When your operating system launches any process, it allows it to have both its own memory-space and resources (symbols, functions, variables, etc..).
At this point, a process may want to communicate or share some of its own data to another process. This represents the main goal of IPCs.
Cool! But… What are the tools used for this communication?
There is a lot of them! Some are useful in most situations, other only in a few. But all have a real utility.
Here is a non-exhaustive list:
- Half-duplex UNIX pipes
- FIFOs (named pipes)
- Full-duplex pipes (STREAMS pipes)
- Networking sockets (Berkeley style)
- SYSV style message queues
- SYSV style semaphore sets
- SYSV style shared memory segments
Wow, that’s a lot of new terms, can I have any help to understand these concepts?
Well.. I’ll try to stay as concise as possible but I can’t promise anything. Is it OK for you?
Perfect, go ahead!
The first notion is called “Pipe”, simply put, a pipe is a method of linking the standard output of one process to the standard input of another.
OK, so it established a tunnel between two processes! But… Why three different kind of pipe?
- Half-duplex UNIX pipes provide a method of one-way communications (hence the term half-duplex) between processes.
- FIFOs (named pipes) have the same properties than the one before, except that they exist physically as device special file and can be inherited by process from different ancestry.
- Full-duplex pipes have exactly the same behavior as the Half-Duplex, except that they allow bidirectional communication between processes.
It seems that there is a pipe for almost every usage! And what about “sockets”?
Although there is four kinds of sockets (stream, datagram, raw and sequenced packet), and almost the same (if not more) number of application domain; they all have the same basis: Allowing communication between two different processes on the same or different machines. To be more precise, it’s a way to talk to other computers using standard input and output. Thereby, it can be linked to a network connection, a text file, a terminal or something else.
“The utility of sockets? Allowing communication between two different processes on the same or different machines!”
Wow, it seems powerful! And what for “SYSV style” IPCs?
As its name suggests, “SYSV style” IPCs has been introduced with the System V from AT&T. It includes three new forms of IPC facilities (message queues, semaphores, and shared memory); each of them being identified by a unique IPC identifier.
~ Message queues
Message queues can be described as an internal linked list within the kernel’s addressing space. Messages can be sent to the queue in order, and retrieved from it in several different ways. Moreover, message queues introduce the concept of channels, allowing the process to receive only a certain part of the messages traffic.
Semaphores can be considered as counters used to control access to shared resources by multiple processes. They are most often used as a locking mechanism to prevent processes from accessing a particular resource while another process is performing operations on it.
~ Shared memory
Shared memory can best be described as the mapping of an area (segment) of memory that will be mapped and shared by more than one process.
Due to the fact that the information is directly mapped from a memory segment, and into the addressing space of the calling process; the shared memory is the fastest form of IPC in term of read & write capacity.
Uh... It was a lot of information on this last point! But as far as I am involved in this concept, I can say that the SYSV IPCs are really impressive by their versatility!
Yes, that’s totally true! Moreover, they’re a good starting point for whoever want to start learning about Unix IPCs.
OK, OK, that’s fine. Now, do you have any link to an example of the application of these concepts?
Yup, absolutely! You can indeed find the second part of this article at this address (click me) where you’ll learn how to turn the SYSV IPCs into tools to build a real game!
You’re welcome, thank you for taking the time to read my first article!
If you’re interested in more lecture about IPCs in Unix / C, I strongly advise you these books, mans, and articles:
And a special mention for this one, which is — in my opinion — one of the best reference about IPCs: http://tldp.org/LDP/lpg/node7.html