codexion
C project · featured · pthreads, mutexes, scheduling
Multithreaded twist on the Dining Philosophers: coders compete for shared dongles, with FIFO/EDF scheduling.
The problem
Dining philosophers with coders: each coder is a thread that needs two shared USB dongles to compile, and burns out if it goes too long without compiling. A monitor must log a burnout within 10 ms.
What I built
- Dongles are taken atomically, both or none, under one mutex, so hold-and-wait (and so deadlock) can never happen.
- A hand-rolled binary heap orders waiting coders: by arrival for FIFO, or by deadline for EDF, so the coder closest to burning out goes first.
- Waiters use pthread_cond_timedwait with a fresh deadline each time round, so a dongle that comes off cooldown mid-wait is always noticed.
- A monitor thread that sleeps until the next possible burnout instead of polling, and a separate print mutex so log lines never interleave.
The hard part
Missed wake-ups. A released dongle stays unavailable for a cooldown, so a broadcast on release can arrive before the dongle is usable again, and a coder that simply waits for the next signal sleeps forever. The fix was a timed wait with a deadline recomputed every loop, plus re-checking the condition on every wake-up instead of trusting the signal.
What it looks like
$ ./codexion 5 800 200 100 100 3 10 edf 1478 3 has taken a dongle 1478 3 is compiling 1683 3 is debugging 1788 3 is refactoring
Built with
- C
- POSIX threads
- mutexes + condition variables
- binary heap
- Norminette
More projects
- metro · Lisbon metro sim in pixel art: watch the four lines run, step into any station, board a train and ride it.
- call_me_maybe · Turns a sentence into a JSON function call with a 0.6B model using constrained decoding — parseable by construction, not by luck.
- fly-in · Drone fleet routing: Dijkstra pathfinding, turn-based scheduling and a pygame replay.
- a-maze-ing · Maze generator with multiple algorithms, pathfinding solvers and terminal visualization.
- push_swap · Highly optimized sorting with two stacks and a tiny set of allowed operations.
- ft_printf · printf rebuilt from scratch: chars, strings, pointers, decimals, unsigned and hex.
- get_next_line · Reads a file descriptor one line at a time with careful dynamic memory handling.
- Libft · My own C standard library — the foundation every later 42 project is built on.