This project implements a multithreaded traffic signal simulation system using POSIX threads in C.
Each traffic light is modeled as a thread, and the system calculates traffic flow using matrix multiplication under strict energy constraints.
Unlike standard matrix multiplication, computations are performed step-by-step due to:
- Limited shared resources (energy pool)
- Synchronization requirements
- Controlled execution order
This project is designed to demonstrate Operating Systems concepts, particularly thread synchronization and resource management.
In a smart city:
- Each traffic signal operates as an independent thread
- Signals consume energy from a shared global pool
- Energy is limited and must be managed carefully
- Threads must wait when energy is exhausted
globalEnergyis a shared global resource- Initial value: 23 units
- Each multiplication step consumes 1 energy
- If energy reaches 0, threads are blocked
- A dedicated Power Thread replenishes energy every 200 ms
-
One thread is created for each matrix cell
C[i][j] -
Each thread:
- Computes only its assigned cell
- Cannot interfere with others
-
Due to shared
tempSum, only one thread computes at a time
-
mutexis used for:globalEnergytempSum- log file access
-
condition variablesare used for:- Waiting when energy is exhausted
- Ensuring ordered execution (single active thread)
Matrix multiplication is performed as:
C[i][j] = Σ ( A[i][k] × B[k][j] )
However, each step:
- Locks mutex
- Checks energy
- Waits if energy = 0
- Decrements energy
- Unlocks mutex
- Performs multiplication
- Locks mutex
- Updates
tempSum - Logs operation
- Unlocks mutex
- Applies delay
threaded-matrix-sim/
├── src/main.c
├── input/inputA.txt
├── input/inputB.txt
├── output/simulation_log.txt
├── README.md
├── Makefile
└── .gitignore
makemake runAll operations are logged into:
output/simulation_log.txt
Example log entry:
[Thread 140735 | Hucre (1,2)]: Enerji Harcandi | K-Adimi: 2 | tempSum: 45
tempSumandglobalEnergyare global and shared- Threads cannot run computations simultaneously
- Busy-waiting is NOT allowed
- All shared variables are protected by mutex
- Condition variables are used for proper blocking
This project demonstrates:
- Thread creation and management (pthreads)
- Mutex and condition variable usage
- Race condition prevention
- Resource allocation and blocking
- Producer-consumer pattern (Power Thread)
- Controlled execution under constraints
- 📧 Email: ozdogrumerve57@gmail.com
- 🐛 Issues: Feel free to report bugs or suggest features on GitHub Issues
- 👤 Author: Merve Özdoğru