This project has been created as part of the 42 curriculum by mimeyer
Codexion is a concurrency simulation in C where multiple coder threads compete for a limited set of USB dongles in order to compile, debug, and refactor. Each coder needs two adjacent dongles to compile, and the program stops when every coder has completed the required number of compilations or when any coder burns out by missing a compile deadline.
The project implements a custom scheduler for dongle access using a thread-safe binary heap. It supports both fifo and edf arbitration policies while enforcing dongle cooldown and precise burnout detection.
-
Build the program:
make re
-
Run the simulation with 8 mandatory arguments:
./codexion [CODERS] [BURNOUT] [COMPILE] [DEBUG] [REFACTOR] [REQUIRED] [COOLDOWN] [SCHEDULER]
-
Example:
./codexion 10 500 50 25 25 5 50 edf
-
Argument rules:
CODERS: number of coder threads and number of dongles (must be greater than 1)BURNOUT: milliseconds until a coder burns out if they do not start compilingCOMPILE: compile duration in millisecondsDEBUG: debug duration in millisecondsREFACTOR: refactor duration in millisecondsREQUIRED: required number of compilations per coder before stoppingCOOLDOWN: dongle cooldown in milliseconds after releaseSCHEDULER: eitherfifooredf
-
The program rejects invalid inputs, including negative values, non-integer strings, or an unsupported scheduler.
- Codexion project PDF (assignment description)
pthreadmanuals- C Program to Implement Binary Heap - GeeksforGeeks
- Binary Heap - GeeksforGeeks
- Binary heap - Wikipedia
- Intro to threads - YouTube
- Intro to mutex - YouTube
- Intro to condition variables - YouTube
- Miro (design planning)
- ChatGPT / Copilot: used for minor debugging, heap implementation understanding, and README drafting only
- Coders never try to acquire dongle locks until they have been granted permission by the heap manager.
- Each coder waits on its own condition variable and is only woken when both adjacent dongles are confirmed available.
- Once woken, the coder locks both adjacent dongles and begins compiling, eliminating circular wait across threads.
- The heap queue feeds the next eligible coder according to the selected scheduler.
fifopreserves request order, andedfprioritizes the coder with the earliest compile deadline.- Every coder re-enters the heap after debugging and refactoring, ensuring all threads get repeated access to the scheduler.
- Dongle release times are tracked using an earliest-available timestamp array.
- The heap manager checks both adjacent dongles before waking a coder, ensuring cooldown has expired on both.
- This prevents a coder from grabbing a recently released dongle too early.
- A dedicated monitor thread checks each coder's last compile start time against
time_to_burnout. - If a coder misses a compile deadline, the monitor logs burnout immediately and stops the simulation.
- The log mutex ensures the burnout message is printed without interference from other threads.
- All log output passes through a single
pthread_mutex_t. - This guarantees messages like
X has taken a dongle,X is compiling,X is debugging,X is refactoring, andX burned outare never interleaved.
- Each coder has:
pthread_mutex_t lockto protect its internal statepthread_cond_t waketo wait for permission from the heap manager
- Each dongle has:
pthread_mutex_t lockto enforce exclusive access while compiling
- The heap uses:
pthread_mutex_t lockto serialize insertions, removals, and priority updates
- The log uses:
pthread_mutex_t lockto serialize output and prevent mixed lines
How they coordinate:
- Coders insert themselves into the shared heap when they are ready to compile.
- The heap manager thread sorts requests and checks whether both neighboring dongles are cooled down.
- When a coder is selected, the manager broadcasts on that coder's condition variable.
- The coder then locks both dongles and proceeds to compile.
- The monitor thread polls coder state safely under each coder mutex and prints burnout or completion events.