Linux Inter Process Communication examples
Linux Inter Process Communication (IPC)
A collection of examples that demostrate different IPC mechanisms in Linux.
All the examples rely on running two processes (A and B). Surprisingly some of these communication mechanisms
do not require A and B to run at the same time.
A makefile is available for every example.
You will be running 2 processes so it may require you to run process A and process B in two different TTY.
Generally, I should be simple as make
, then ./a.out
(on terminal 1) and ./b.out
(on terminal 2).
Occasionally, a sigle executable is available, in which case the original process will fork.
To remove all the builds make clean
Signals are one of the primitive type of IPC. They should be handle with care, because they are able to interrupt
the process at any time. Therefore readings (other than sig_atomic_t) may have inconsistent data.
signal
is considered obsolete than I used sigaction
(POSIX)
An half-duplex communication channel (unidirectional). Pipes are very easy to use and the pipe
system call
generate the two file descriptors needed for the communication. These descriptors cannot be accessed by other processes,
then you can only fork the process and inherit the file descriptors.
To use pipe we usually need to follow this steps:
These pipes have names and are represented in the filesystem by special files (FIFO). The advantage of using named pipe
is quite obvious, processes do not need to belong to the same hierarchy.
Named pipes are files and they may persist after process termination. It is required that a reader process and a writer process
open the pipe otherwise read/write operations can be blocking or returning error.
mkfifo - create a named pipe
The kernel allows processes to share an address space.
It is up to the processes attaching this shared location to synchronize to avoid conflict.
A kernel managed queue that stores labeled messages.
Messages can be retrieve from the oldest to newest and filter by label.
Semaphore set can hold multiple semaphores that are used to as synchornizing or signaling mechanism.
Operation on semaphore are atomic and consist of descrementing or incrementing the semaphore value.
Semaphore can be used as mutex:
A process descrement the semaphore before entering a critical section and restore its value when exiting.
Semaphore can be use as counter:
A process may set a semaphore an wait for other processes to consume the counter.
File mapping is very similar to shared memory, it can be used by processes to map a file into their memory,
any changes are visible to other processes (MAP_SHARED). An advisory locking mechanism is available for files
through fcntl. Advisory means that process need to cooperate and request the locks before operating on the shared file.
Sockets can be used for IPC. Linux provides domain sockets for Stream and Datagram.
Although these socket are local to the same system, the underlying implementation is abstracted by the Socket API, then
they can be used as any other socket of the corresponding type.
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to http://unlicense.org