Day56 Learning Log¶
Objective¶
Today focused on Linux POSIX shared memory IPC.
The goal was to gradually build a complete event-driven IPC architecture using:
- shared memory
- process-shared mutex
- eventfd
- epoll
- UNIX domain socket
- SCM_RIGHTS fd passing
What I Built¶
Initial Shared Memory Demo¶
Started from the smallest POSIX shared memory flow:
Created a simple shared structure:
and verified parent/child shared memory behavior after fork.
Independent Process Shared Memory¶
Refactored the architecture into:
without fork inheritance.
The writer became the shared memory owner.
The reader independently attached to the existing shared memory object.
This helped clarify:
- shared object lifetime
- mmap attachment model
- process isolation vs shared pages
Process-shared pthread Mutex¶
Added:
inside shared memory.
Learned:
- mutex must be initialized after mmap()
- mutex must reside inside shared memory
- only owner process should initialize the mutex
- reader processes only use existing mutex state
Also discovered that:
Using:
caused segmentation fault / glibc abort.
Shared Memory Race Protection¶
Protected shared structure access using:
Verified correct synchronization between writer and reader.
AF_UNIX Control Channel¶
Built a UNIX domain socket control channel.
Architecture:
Implemented reusable helper layer:
SCM_RIGHTS File Descriptor Passing¶
Reused Day55 fd passing implementation:
Successfully transferred:
from writer to reader.
Also debugged an important issue:
The fd passing operation must use the accepted client socket.
eventfd Notification Architecture¶
Integrated:
into shared memory IPC.
Writer:
Reader:
This converted the IPC architecture from polling into event-driven design.
Shared Activation State¶
Added shared state flag:
Used as graceful shutdown coordination between writer and reader.
Shutdown flow:
Important Debugging Issues¶
Mutex Corruption¶
Encountered glibc abort:
Cause:
inside stdin event handler.
Reader mmap Protection¶
Encountered segmentation fault when locking mutex.
Cause:
Fix:
FD Passing Failure¶
Encountered:
Cause:
Key Concepts Learned¶
Shared Memory¶
- independent process attachment
- shared page mapping
- shared object lifetime
pthread Mutex¶
- process-shared synchronization
- userspace synchronization object
- futex-backed design
eventfd¶
- kernel event counter
- epoll integration
- lightweight notification primitive
UNIX Socket¶
- control path IPC
- fd transfer
- resource coordination
Final Architecture¶
Result¶
Successfully built:
using:
- POSIX shared memory
- process-shared mutex
- AF_UNIX socket
- SCM_RIGHTS
- eventfd
- epoll