What is the difference between a semaphore and a mutex?
Answer
Both are synchronization primitives but with different purposes. Mutex (Mutual Exclusion): Binary (locked/unlocked), owned by the locking task. Only owner can unlock; enforces ownership. Used for protecting shared resources (critical sections). Supports priority inheritance to prevent priority inversion. Semaphore: Counting value (0 to N), not owned. Any task can post (increment), anyone can wait (decrement). Used for signaling between tasks and counting resources. Binary semaphore similar to mutex but without ownership. Common pattern: Mutex protects a shared buffer, semaphore signals data availability. Rule of thumb: Mutex for mutual exclusion, semaphore for signaling and counting available resources.
Master These Concepts with IIT Certification
175+ hours of industry projects. Get placed at Bosch, Tata Motors, L&T and 500+ companies.