This is an example repository to demonstrate how to use inter-process mutexes allocated on a shared memory segment and mapped to each address space, to achieve inter process communication.
The binary generated from this C++ program, operates as follows:
- Recursively spawns a hierarchy child processes.
- Each child process spawns 1 to 3 children.
- Without any communication, the growth is unbounded.
- A shared segment of memory is allocated to count the number of total processes.
- Additionally, a pthread mutex with the
PTHREAD_PROCESS_SHARED
attribute is also allocated into shared memory. - Before spawning new child processes, each process maps the shared segment into it's address space, consults this mutex, and spawns children only if the counter does not exceed a predefined ceiling.
Run make
to create the binary and make clean
to clean build artifacts. Alternatively,
use Docker to run the application as follows:
$ docker build -t ipcmtx .
$ docker run -it ipcmtx /usr/src/app/binary
- Careful to tweak the program. The unbounded process growth can literally bring down the system to a halt.
- Note the system value of maximum memory that can be segmented into shared space. If this value is exceeded, the
shmget
syscall fails withNo space left on device
. - A few versions of Clang/GCC were used. Thorough portable compilation is not guaranteed.
MIT