Skip to content

ozdogrumerve/threaded-matrix-sim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🧵 Smart Traffic Signal Simulation with Multithreading (OS Project)

📌 Project Description

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.


🧠 Scenario

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

⚙️ System Design

🔋 Global Energy Model

  • globalEnergy is 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

🔁 Thread Model

  • 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


🔐 Synchronization

  • mutex is used for:

    • globalEnergy
    • tempSum
    • log file access
  • condition variables are used for:

    • Waiting when energy is exhausted
    • Ensuring ordered execution (single active thread)

📊 Computation Model

Matrix multiplication is performed as:

C[i][j] = Σ ( A[i][k] × B[k][j] )

However, each step:

  1. Locks mutex
  2. Checks energy
  3. Waits if energy = 0
  4. Decrements energy
  5. Unlocks mutex
  6. Performs multiplication
  7. Locks mutex
  8. Updates tempSum
  9. Logs operation
  10. Unlocks mutex
  11. Applies delay

📁 Project Structure

threaded-matrix-sim/
├── src/main.c
├── input/inputA.txt
├── input/inputB.txt
├── output/simulation_log.txt
├── README.md
├── Makefile
└── .gitignore

▶️ Compilation & Execution

Compile

make

Run

make run

📝 Logging System

All operations are logged into:

output/simulation_log.txt

Example log entry:

[Thread 140735 | Hucre (1,2)]: Enerji Harcandi | K-Adimi: 2 | tempSum: 45

⚠️ Important Design Constraints

  • tempSum and globalEnergy are 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

🚀 Learning Outcomes

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

📞 Support & Contact


⭐ Star this repo if you find it helpful!

Made with ❤️ by Merve Özdoğru

Operating Systems Assignment

About

Multithreaded traffic simulation using pthreads with energy-constrained scheduling and synchronization mechanisms.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors