Saturday, May 31, 2025

classic problems in CS: producer, consumer, event driven programming, blocking vs non blocking, parallel programming, synchronous ipc bad etc...

Race conditions, deadlocks, resource hogging are the kind of problems you will have to deal with in production issue debugging.

 

1. Producer–Consumer Problem

  • What: One or more producers generate data, and one or more consumers process it.

  • Why classic: It models pipelines and buffers in OS, I/O, and multithreading.

  • Challenge: Managing a shared buffer with thread safety, avoiding deadlocks or race conditions.

  • Modern Usage: Logging systems, data ingestion pipelines, concurrent queues, asyncio tasks.


 

2. Event-Driven Programming

  • What: Program flow controlled by events like I/O, signals, or user actions.

  • Examples: GUI frameworks, network servers (e.g., Node.js), embedded systems.

  • Key Idea: Handlers (callbacks) respond to events asynchronously.

  • Pitfall: Callback hell, hard-to-debug flow.

    3. Blocking vs Non-Blocking I/O


    BlockingNon-blocking
    ThreadWaits (halts)Continues immediately
    RiskFreezes UI / threadNeeds polling or async model
    UseSimple scriptsReal-time systems, UI, servers

4. Parallel Programming

  • What: Running multiple computations simultaneously (threads or processes).

  • Goal: Speed up workloads like ML training, video processing, simulations.

  • Challenge: Thread safety, synchronization, avoiding contention.

  • In Python: GIL limits threading → use multiprocessing for true parallelism.

    5. Synchronous IPC: Problems

  • MsgReceive() in QNX or similar synchronous IPC:

    • Blocks caller until a message arrives.

    • Problem: If a thread is blocked on MsgReceive(), it can’t respond to new events → UI hangs.

    • Fix: Use async messaging, polling with timeout, or delegate to worker th

  • 6. Deadlocks, Starvation, and Race Conditions

  • Deadlock: Two threads waiting on each other → system freeze.

  • Starvation: One thread never gets scheduled.

  • Race Condition: Two threads modifying shared data without sync → nondeterministic bugs.

 

No comments: