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)

Section A

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.

🇮🇳 BookMyShow🇮🇳 Paytm🇮🇳 IRCTC🇮🇳 Razorpay🇮🇳 PhonePe🇮🇳 Zerodha
In 2015, IRCTC's Tatkal booking system crashed because 15 million users hit "Book" simultaneously. The root cause? Inadequate synchronization in the ticket allocation module. IRCTC later implemented mutex-based locking and queue-based token systems — concepts you'll learn in this very chapter. Today, IRCTC handles 25+ million bookings daily using these techniques.
Section B

Learning Outcomes — Bloom's Taxonomy Mapped

Bloom's LevelLearning Outcome
🔵 RememberList the 3 requirements of the critical section problem; define semaphore, mutex, monitor, and thread
🔵 UnderstandExplain how Peterson's Algorithm ensures mutual exclusion; differentiate binary vs counting semaphores with Indian analogies
🟢 ApplyImplement Producer-Consumer using POSIX semaphores in C; write Pthreads programs with mutex locks
🟢 AnalyzeTrace race conditions in concurrent code; compare user-level vs kernel-level threads; analyze deadlock scenarios in Dining Philosophers
🟠 EvaluateEvaluate which synchronization primitive is best for a given real-world system (banking, booking, etc.)
🟠 CreateDesign a thread-safe booking system; implement Dining Philosophers with deadlock detection