A concurrent multiplayer game using System V IPC mechanisms. Each player is an independent process coordinating through shared memory, semaphores and message queues to eliminate opponents. Last team standing wins!
Battle game where players coordinate with teammates to surround and eliminate opponents on a shared board. Core concepts: Shared Memory, Semaphores, Message Queues, Signal Handling and Process Management.
- Terminal-based display where all players show the board (mandatory) or SDL2 graphics with creator-only display (bonus)
- Up to 9 teams with intelligent AI coordination
- First process creates IPC resources and persists for cleanup
- Dynamic player joining with automatic resource management
- Message queue team communication and real-time statistics
- Board: One tile holds only one player at a time
- Movement: One square at a time (up/down/left/right, no diagonals)
- Elimination: Player dies when touched by at least 2 players from the same opposing team in adjacent tiles (horizontal/vertical/diagonal)
- Coordination: Message queues share enemy positions between teammates
- Victory: Last team standing wins
| Mechanism | Purpose |
|---|---|
| Shared Memory | Game board, player count, game status, team stats |
| Semaphores | Mutual exclusion for shared resources access (prevents race conditions) |
| Message Queues | Team communication for enemy positions |
| Signals | Graceful exit (SIGINT) and cleanup |
First Process (Creator):
- Creates all IPC resources (shared memory, semaphores, message queue)
- Persists until game end (even if eliminated as player)
- Displays game board (mandatory: all players display; bonus: only creator displays)
- Handles final cleanup
Subsequent Players:
- Attach to existing IPC resources
- Display board in mandatory version, no display in bonus version
- Detach and clean local references on exit
- Do not remove IPC resources
Prerequisites: GCC, Make, System V IPC support, SDL2/SDL2_gfx (bonus only)
# Ubuntu/Debian
sudo apt-get install libsdl2-dev libsdl2-gfx-devmake # Mandatory version (terminal)
cd bonus && make # Bonus version (SDL2 graphics)
make fclean # Clean allNote: Mandatory version has no memory leaks (no dynamic allocations). Bonus version leaks are due to SDL2 library internals.
./lemipc [team_number] # Team 1-9First process creates IPC resources and persists until game end. Subsequent processes attach to existing resources.
Tip: Before running a script, launch in a separate terminal a first process to create IPC resources. Script outputs are silenced to avoid cluttering.
./scripts/launch_N_random_players.sh 64 # 64 random team players
./scripts/launch_A_team_of_N_players.sh 1 10 # Team 1 with 10 players
./scripts/launch_X_teams_of_N_players.sh 3 8 # 3 teams, 8 players eachSPACE Pause/Resume • +/- Speed control • 1-9 Spawn player • ESC Close
├── srcs/ # Mandatory (terminal display)
├── bonus/ # Bonus (SDL2 graphics)
├── libft/ # Custom C library
├── scripts/ # Launch utilities
└── subject/ # FAQ and documentation
Adjust in lemipc.h: Board size (16x16), max teams (9), IPC keys, AI behavior, game delay, debug mode
SDL2 graphical interface with color-coded teams, grid display, pause/resume, speed control and real-time statistics overlay.
Initialization: Parse team number → Create IPC resources (if first) or attach to existing → Signal handlers → Random board position
Game Loop: Lock semaphore → Check surroundings → AI movement → Message queue communication → Update board → Display (only creator in bonus) → Release semaphore
Termination: Death/SIGINT → Detach resources → Regular players exit → Creator persists and removes IPC at game end
A buffered ft_printf implementation was developed to address severe performance bottlenecks. With large boards and many players, the standard ft_printf caused significant lag due to multiple system calls for each character output. The buffered version accumulates output in memory and flushes to stdout in a single write operation, dramatically improving refresh rates and enabling smooth real-time gameplay.
Manhattan distance target selection → Greedy pathfinding → Message queue coordination → Random moves to avoid deadlocks
Technical Documentation:
- The Linux Programming Interface (Chapters 45-48: System V IPC)
- System V vs POSIX IPC
- Taxicab Geometry (Manhattan Distance)
- ANSI Escape Code (CSI sequences)
- SDL2 Docs
- SDL2_gfx Docs
Video Tutorials:
- How to Set up Shared Memory in Your Linux and MacOS Programs. (shmget, shmat, shmdt, shmctl, ftok)
- IPC: To Share Memory Or To Send Messages
- Advanced Programming in the UNIX Environment: Week 08, Segment 2 - System V IPC
- Creating a Game Loop with C & SDL (Tutorial)
Project Demos:
- Text Mode Demo - 16x16 Board - 3 Teams (6 Players)
- Text Mode Demo - 48x48 Board - 576 Players
- Graphic Mode Demo - 200x100 Board - 2 Teams (5000 Players)
Useful Commands:
# Emergency cleanup (if program loses control)
pkill lemipc && ipcrm -a
# Monitor IPC resources in real-time
while true ; do ipcs ; sleep 1 ; doneHappy Gaming! May the best team win! 🏆