How sanfrg/tp1-sistemasoperativos works
1. Overall shape
This repo implements a small multi-process game server architecture in C. The runtime is split into three executables: master, multiple player processes, and an optional view process. The master owns the game rules and turn progression; players compute moves; the view only observes and renders state.
2. Process roles and boundaries
- master: authoritative coordinator, creates shared state, spawns/attaches to players and view, validates moves, advances the game, and decides when the match ends.
- player: autonomous worker process that reads the current board/state and proposes a move.
- view: read-only observer that renders the board plus stats/ranking from shared memory.
These roles are cleanly separated so the UI never drives game logic, and the players never mutate authoritative state directly.
3. Inter-process communication model
The game uses two IPC mechanisms with distinct purposes:
- Pipes + select() between players and the master for move submission. This lets the master listen to many players concurrently without blocking on any single one.
- Shared memory for game state dissemination to players and the view, so everyone can inspect the current board, scores, and termination flags without copying large payloads through pipes.
4. Master control loop
The master’s main loop is in src/master_loop.c and src/master_game.c. It periodically waits for input using select(), applies a configurable delay, and processes turns in a round-robin order. Moves are validated sequentially, so if two players target the same cell, the first one processed wins and the other is rejected as invalid.
5. Game-state ownership
The authoritative board, player positions, scores, blocked status, reward cells, and game_over flag live in shared memory structures defined under include/ and implemented in src/shm.c, src/shm_generic.c, and src/state_sync.c. The master is the only writer of the canonical game state; players and the view are readers, except for the players’ move requests sent over pipes.
6. Synchronization strategy
Shared-memory access is coordinated with semaphore-based synchronization in src/state_sync.c. The design follows a readers/writers pattern so that multiple readers can inspect state concurrently while the master gets exclusive write access. This is important because both the view and all players may read the same state at once, but only the master may publish updates.
7. Player behavior
src/player.c and src/player_ai.c implement the autonomous agent. The player reads the shared state, evaluates valid neighboring moves, and scores them using local food value, future mobility, nearby reward potential, distance from rivals, and dead-end penalties. This makes player behavior deterministic in structure but still partly stochastic on ties.
8. View subsystem
The rendering path is separated into src/view.c and src/view_render.c. The view does not participate in gameplay; it consumes shared state and presents a richer visualization than the master would, including board status, per-player stats, territory percentage, and final ranking. This keeps output concerns isolated from game coordination.
9. Process lifecycle and cleanup
src/master_process.c and src/master_args.c handle launching, argument parsing, signal handling, and shutdown. The repository explicitly addresses cleanup of orphaned shared memory and termination of blocked players/view processes, because a match can end normally, by timeout, or due to signals such as SIGINT/SIGTERM.
10. Important architectural constraints
- Uses POSIX/Linux primitives heavily: fork/exec, pipe2, select, shm_open, mmap, semaphores, signals, and clock_gettime(CLOCK_MONOTONIC).
- The build/run flow is containerized via Docker (run.sh, Makefile) to keep the environment reproducible.
- The match rhythm is configurable with CLI flags, especially delay and timeout, which directly affect the master loop timing.
11. What an architecture diagram should emphasize
Draw the master as the hub:
- one inbound channel from each player via pipe,
- one shared-memory region read by both players and the view,
- synchronization semaphores around that shared state,
- an optional view process attached read-only,
- and the master’s validation/game-state update loop as the only write path.
The central design idea is “authoritative master + shared read-only state + concurrent move submission.”
Main entry points: shm.c Shared memory · player.c Player main · master.c Master main · state_sync.c Sync layer
Components 17
Master
Authoritative game coordinator and lifecycle manager
- Master main · Program entry for the authoritative server process
src/master.c - CLI args · Parses delay, timeout, and match options
src/master_args.c - Process mgmt · Launches children, handles signals, and shuts down cleanly
src/master_process.c - Turn loop · Select-based round-robin loop that drives turns
src/master_loop.c - Game rules · Validates moves, advances turns, and decides game end
src/master_game.c - Master API · Master-facing interfaces used across the coordinator modules
include/master.h
Players
Autonomous move-proposing workers
- Player main · Entry point for autonomous player processes
src/player.c - Move AI · Scores candidate moves and chooses an action
src/player_ai.c - Player API · Player-facing interfaces for AI and process logic
include/player_ai.h
View
Read-only observer and renderer
- View main · Entry point for the optional observer process
src/view.c - Render view · Draws board, stats, and final ranking
src/view_render.c - View API · View-facing interfaces for rendering and state access
include/view.h
Shared state
State model, shared memory, and synchronization
- Sync layer · Semaphore coordination and reader/writer access control
src/state_sync.c - Shared memory · Creates and manages the shared state region
src/shm.c - Shm helpers · Generic shared-memory utilities and wrappers
src/shm_generic.c - Game state · Board, scores, blocked status, rewards, and game_over flag
include/estructuras.h - IPC API · Shared-memory and synchronization declarations
include/shm.h
Connections 22
- Master main → CLI args: parses · Reads runtime configuration before startup
- Master main → Process mgmt: starts · Delegates child lifecycle management
- Process mgmt → Turn loop: runs · Hands control to the turn scheduler
- Turn loop → Game rules: validates · Applies move validation and turn progression
- Turn loop → Sync layer: reads/writes · Coordinates access to shared state during turns
- Game rules → Game state: updates · Publishes authoritative board and score changes
- Process mgmt → Shared memory: creates · Sets up the shared memory region used by all processes
- Shared memory → Shm helpers: uses · Relies on common shm helpers
- Shared memory → Game state: stores · Backs the canonical game state in shared memory
- Sync layer → Game state: guards · Serializes access with reader/writer coordination
- Player main → Move AI: chooses · Delegates move selection to the scoring logic
- Player main → Sync layer: reads · Consumes shared state under synchronization
- Player main → Shared memory: attaches · Maps the shared game region
- Player main → Master main: submits move · Sends proposed moves back through pipes
- View main → Render view: renders · Turns shared state into a visual output
- View main → Sync layer: reads · Observes state without writing it
- View main → Shared memory: attaches · Maps the shared game region read-only
- Render view → Game state: displays · Uses board, stats, territory, and ranking data
- Master API → Master main: supports · Shared declarations for master modules
- Player API → Move AI: supports · Shared declarations for player decision logic
- View API → Render view: supports · Shared declarations for rendering code
- IPC API → Shared memory: supports · Shared declarations for IPC and synchronization