POSIX IPC • Shared Memory • Semaphores

RPG Engine

A modular multi-process dungeon crawler built on Linux with
fork & exec, POSIX signals, and semaphore synchronization

Scroll to explore

The Party

Three forked child processes, each executing their own binary via execl()

PID: ---

Barbarian

Tank • Melee DPS
HP
100
ATK
85
Ability: Read enemy HP from shared memory, write attack damage
Waiting at camp
PID: ---

Wizard

Support • Decoder
HP
70
INT
95
Ability: Decode Caesar cipher from shared memory barrier puzzle
Studying scrolls
PID: ---

Rogue

Scout • Lockpick
HP
60
DEX
90
Ability: Binary search lockpick, collect treasure through semaphore door
Sharpening picks

The Dungeon

Four stages of peril — each requiring a different IPC mechanism to overcome

The party stands at the dungeon entrance. Press Run Dungeon or Step Through to begin the adventure…
Enemy
Barbarian attacks
Barrier
Wizard decodes
Trap
Rogue picks lock
Treasure
Semaphore door

Stage 1 — Enemy Encounter

Barbarian's Turn
Barbarian
VS
Dragon
HP: 120
Shared Memory — shm_enemy
enemy_hp 120 read by Barbarian
attack_dmg 0 written by Barbarian

Stage 2 — Magic Barrier

Wizard's Turn
Caesar Cipher (shift = 3)
Encrypted
RSHQ VHVDPH
Decoded
_
Shared Memory — shm_barrier
cipher_text "RSHQ VHVDPH" read by Wizard
decoded "" written by Wizard

Stage 3 — Lock Trap

Rogue's Turn
Target: ???
Binary Search Lockpick
Low: 1 Mid: 50 High: 100
Shared Memory — shm_trap
guess written by Rogue
result read by Rogue

Stage 4 — Treasure Room

Semaphore Sync
[Closed]
[Closed]
Barbarian
sem_wait()
Not holding
Wizard
sem_wait()
Not holding
Semaphore State
sem[0]
1
sem[1]
1
Rogue waiting outside…

IPC Message Log

Real-time inter-process communication — signals, shared memory ops, and semaphore calls

00:00.000 SYSTEM Dungeon visualizer initialized. Awaiting orders…

Architecture

How the multi-process RPG engine is wired under the hood

fork() & exec()

game.c is the launcher. It calls fork() three times and uses execl() to replace each child with barbarian, wizard, or rogue binaries. The parent tracks all child PIDs.

Shared Memory

POSIX shm_open() + mmap() creates named shared memory segments. Each dungeon stage uses a dedicated struct so processes can read/write game state without pipes.

Signals

kill(pid, SIGUSR1) coordinates turn order. The game launcher signals which character should act next. Handlers registered via sigaction() wake sleeping processes.

Semaphores

POSIX named semaphores (sem_open) guard the treasure room door. Two party members call sem_wait() to hold levers open while the Rogue calls sem_post() after collecting treasure.

Game Loop

The parent process loops through dungeon stages, dispatching signals to the appropriate child. Each child reads/writes shared memory, then signals completion back to the parent.

Cleanup

On SIGINT or normal exit, the parent calls shm_unlink(), sem_unlink(), and waitpid() to reap children and free all IPC resources cleanly.