

You are working on embedded firmware for a battery-powered device running a small RTOS. Multiple tasks share peripherals such as I2C, SPI, and a ring buffer used by an interrupt handler and a worker task, and a recent bug caused intermittent stalls and priority inversion under load. The codebase currently mixes mutexes, binary semaphores, and counting semaphores without a clear rule for when each should be used.
How would you compare a mutex and a semaphore in this embedded context, and when would you choose each? Explain the tradeoffs around ownership, ISR interaction, priority inversion, deadlock risk, and how you would structure synchronization for shared peripherals and task signaling.
You are working on embedded firmware for a battery-powered device running a small RTOS. Multiple tasks share peripherals such as I2C, SPI, and a ring buffer used by an interrupt handler and a worker task, and a recent bug caused intermittent stalls and priority inversion under load. The codebase currently mixes mutexes, binary semaphores, and counting semaphores without a clear rule for when each should be used.
How would you compare a mutex and a semaphore in this embedded context, and when would you choose each? Explain the tradeoffs around ownership, ISR interaction, priority inversion, deadlock risk, and how you would structure synchronization for shared peripherals and task signaling.