Operating Systems
Unit 4: Process Synchronization & Threads
From race conditions to thread-safe code — master critical sections, semaphores, classical synchronization problems, and multithreading to build reliable concurrent systems.
⏱️ Time to Complete: 7 hrs theory + 5 hrs lab | 💰 Earning Potential: ₹8K–₹25K/month | 📝 30 MCQs (Bloom's Mapped)
💼 Jobs this unlocks: Backend Engineer (₹6–12 LPA) | Systems Programmer (₹8–15 LPA)
Opening Hook — Race Condition in Action!
🎬 The BookMyShow Bug That Sold One Ticket Twice
Picture this: Friday evening, 7 PM. Rahul in Mumbai and Sneha in Pune are both staring at the last available ticket for Jawan at PVR Phoenix. Both see "1 seat available." Both click "Pay Now" through Paytm at the exact same millisecond.
The payment gateway processes both requests. Both receive confirmation SMS: "🎫 Booking confirmed!" But there's only ONE seat. When Rahul arrives at the theatre, his ticket is invalid. Chaos. Refunds. 1-star reviews.
What went wrong? The code that checks seat availability and deducts the seat count was NOT protected. Two threads read the same value (seats = 1), both passed the check (1 > 0), both decremented (seats = 0), but the seat was sold twice. This is a race condition — and it's exactly what process synchronization prevents.
Every payment gateway (Razorpay, Paytm, PhonePe), every booking system (IRCTC, BookMyShow, MakeMyTrip), every banking app runs concurrent code. Without synchronization, your money isn't safe. This chapter teaches you how to make it safe.
Learning Outcomes — Bloom's Taxonomy Mapped
| Bloom's Level | Learning Outcome |
|---|---|
| 🔵 Remember | List the 3 requirements of the critical section problem; define semaphore, mutex, monitor, and thread |
| 🔵 Understand | Explain how Peterson's Algorithm ensures mutual exclusion; differentiate binary vs counting semaphores with Indian analogies |
| 🟢 Apply | Implement Producer-Consumer using POSIX semaphores in C; write Pthreads programs with mutex locks |
| 🟢 Analyze | Trace race conditions in concurrent code; compare user-level vs kernel-level threads; analyze deadlock scenarios in Dining Philosophers |
| 🟠 Evaluate | Evaluate which synchronization primitive is best for a given real-world system (banking, booking, etc.) |
| 🟠 Create | Design a thread-safe booking system; implement Dining Philosophers with deadlock detection |