Gregory Hildstrom Projects Publications Resume Contact About Youtube

Linux System V and POSIX IPC Examples

Introduction
System V Private IPC
Shared Memory
Semaphores
Message Queues
Makefile

Introduction

Some recent tasks at work highlighted some obvious gaps in my working knowledge of Linux inter-process communication (IPC). I spent some personal time brushing up on things I had used before and learning about some mechanisms that I had not used before. I am not discussing pipes or sockets here; I am discussing message queues, semaphores, and shared memory. The System V and POSIX specifications have different APIs for using basically the same three IPC mechanisms. I am not exploring every nuance or feature of each API and I am not comparing performance. My goal was simply to demonstrate each IPC mechanism for each API in a set of very simple programs. I show the programs side by side where appropriate, but the code is also available in
ipc_sysv_posix.zip.

The man pages are extremely helpful when diving into stuff like this. Some good starting points are 'man svipc', 'man shm_overview', 'man sem_overview', and 'man mq_overview'. It is important to clean up after yourself. IPC mechanisms tend to persist in the Linux kernel until the next reboot unless they are explicitly removed. See 'man ipcs' for information on listing IPC mechanisms.

My test system runs Fedora 20 Linux with the latest updates as of August 9, 2015. The Makefile at the end shows the compile commands.

System V Private IPC

The System V API allows for private, unnamed, in-memory IPC mechanisms to be shared between parent and identical forked child processes. The processes must be the same and the IPC mechanisms will not be passed along to different exec'd processes. System V IPC among different processes requires a file system path name and a project name to identify a shared IPC resource. So, System V IPC mechanisms can all be named or unnamed. POSIX supports named (sem_open) and unnamed (sem_init) semaphores, but message queues (mq_open) and shared memory (shm_open) must be named. Here I demonstrate private unnamed System V shared memory IPC between a parent process and a forked child process. The same key=IPC_PRIVATE approach also applies to the other System V IPC mechanisms, so I only wrote an example program for private unnamed shared memory.
[an error occurred while processing this directive]
Here is the output:
[ghildstrom@hplt ipc_sysv_posix]$ ./sysv_shared_memory_private 
parent pid is 2164 and child pid is 2165
child pid is 2165
child wrote to shared memory buffer: hello from child
parent read from shared memory buffer: hello from child

Shared Memory

listingSystem VPOSIX
parent
source
[an error occurred while processing this directive] [an error occurred while processing this directive]
child
source
[an error occurred while processing this directive] [an error occurred while processing this directive]
output
[ghildstrom@hplt ipc_sysv_posix]$ ./sysv_shared_memory_file_parent 
parent pid is 2435 and child pid is 2436
child pid is 2436
execve'd child pid is 2436
execve'd child wrote to shared memory buffer: hello from execve'd child
parent read from shared memory buffer: hello from execve'd child
[ghildstrom@hplt ipc_sysv_posix]$ ./posix_shared_memory_parent 
parent pid is 2476 and child pid is 2477
child pid is 2477
execve'd child pid is 2477
execve'd child wrote to shared memory buffer: hello from execve'd child
parent read from shared memory buffer: hello from execve'd child

Semaphores

listingSystem VPOSIX
parent
source
[an error occurred while processing this directive] [an error occurred while processing this directive]
child
source
[an error occurred while processing this directive] [an error occurred while processing this directive]
output
[ghildstrom@hplt ipc_sysv_posix]$ ./sysv_semaphore_file_parent 
parent pid is 2899 and child pid is 2900
child pid is 2900
execve'd child pid is 2900
execve'd child waiting for semaphore zero value
parent delay
parent about to decrement semaphore back to zero
execve'd child detected semaphore zero value
[ghildstrom@hplt ipc_sysv_posix]$ ./posix_semaphore_parent 
parent pid is 2925 and child pid is 2926
child pid is 2926
execve'd child pid is 2926
execve'd child waiting for semaphore non-zero value
parent delay
parent about to increment semaphore to non-zero
execve'd child detected semaphore non-zero value

Message Queues

listingSystem VPOSIX
parent
source
[an error occurred while processing this directive] [an error occurred while processing this directive]
child
source
[an error occurred while processing this directive] [an error occurred while processing this directive]
output
[ghildstrom@hplt ipc_sysv_posix]$ ./sysv_message_queue_file_parent 
parent pid is 2965 and child pid is 2966
child pid is 2966
execve'd child pid is 2966
execve'd child waiting for message
parent delay
parent about to send message
execve'd child received message type 1234
[ghildstrom@hplt ipc_sysv_posix]$ ./posix_message_queue_parent 
parent pid is 2992 and child pid is 2993
child pid is 2993
execve'd child pid is 2993
execve'd child waiting for message
parent delay
parent about to send message
execve'd child received message 1234

Makefile

[an error occurred while processing this directive]