Skip to content

Day56 - POSIX Shared Memory IPC

Objective

This lab demonstrates Linux POSIX shared memory IPC architecture.

Topics covered:

  • shm_open()
  • ftruncate()
  • mmap()
  • independent shared memory processes
  • process-shared pthread mutex
  • eventfd notification
  • AF_UNIX control socket
  • SCM_RIGHTS fd passing
  • epoll-driven IPC

Directory Structure

d56-posix-shm/
├── fd_passing.h
├── epoll_helper.h
├── unix_socket_helper.h
├── shm_common.h
├── shm_writer.c
├── shm_reader.c
└── Makefile

Build

make

or:

gcc -Wall -Wextra -pthread -o shm_writer shm_writer.c
gcc -Wall -Wextra -pthread -o shm_reader shm_reader.c

Shared Memory Structure

struct shm_data {
    pthread_mutex_t lock;
    bool activation;
    int counter;
    char message[SHM_MSG_SIZE];
};

Writer Responsibilities

The writer process owns:

  • shared memory lifecycle
  • mutex initialization
  • eventfd creation
  • control socket server

Writer flow:

shm_open()
ftruncate()
mmap()
pthread_mutex_init()
eventfd()
UNIX socket listen
accept reader
send eventfd
event loop

Reader Responsibilities

The reader process:

  • connects to writer
  • receives eventfd
  • attaches shared memory
  • waits for event notification

Reader flow:

connect()
recvmsg(SCM_RIGHTS)
shm_open()
mmap()
epoll_wait()

Step 1 - Start Writer

Terminal A:

./shm_writer

Expected output:

[INFO] shm_open success
[INFO] shared memory mapped
[INFO] socket created
[INFO] socket listen success

Step 2 - Start Reader

Terminal B:

./shm_reader

Expected output:

[INFO] connected to writer
[INFO] received eventfd
[INFO] epoll wait start

Step 3 - Shared Memory Update

Writer command:

-w hello shared memory

Expected reader output:

[INFO] shm_data:
counter=2
message=hello shared memory

Step 4 - Multiple Updates

Writer:

-w first
-w second
-w third

Expected:

  • reader wakes on each eventfd notification
  • counter increments correctly
  • message updates correctly

Step 5 - Shared Memory Display

Writer:

-s

Expected:

counter=...
message=...

Step 6 - Graceful Shutdown

Writer:

-t

Writer behavior:

activation = false
eventfd notify
cleanup

Reader behavior:

wake up
detect activation == false
exit cleanly

Step 7 - Verify Cleanup

Verify shared memory removal:

ls -l /dev/shm

Verify socket removal:

ls -l /tmp/day56_shm_ctrl.sock

Objects should no longer exist after shutdown.


Key Learning Points

Shared Memory

  • shared pages across processes
  • explicit synchronization required

pthread Mutex

  • process-shared synchronization
  • mutex object stored in shared memory

eventfd

  • lightweight event notification
  • epoll integration

UNIX Domain Socket

  • control channel IPC
  • fd passing

Important Notes

Reader mmap Permission

Even reader-only processes require:

PROT_READ | PROT_WRITE

because pthread mutex modifies internal state.


eventfd Semantics

eventfd is:

notification primitive

not:

message protocol

Shared memory stores the actual state/data.


eventfd Notification Order

Correct order:

lock shm
update data
unlock shm
write(eventfd)

Suggested Experiments

  • remove mutex protection
  • add multiple readers
  • convert writer to full epoll loop
  • use SOCK_SEQPACKET instead of SOCK_STREAM
  • switch to abstract UNIX socket
  • add shared ring buffer