Skip to main content

Synchronization issues in RTOS

Short answer

Concurrent access to shared data causes race conditions, critical section violations, and non-reentrant problems. Solutions include mutexes/semaphores, reentrant functions, priority inheritance for priority inversion, and resource ordering to prevent deadlock.

Explain different synchronization issues (problems faced when tasks share the same variables and functions, and their solutions).

In an RTOS, several tasks run concurrently and often share global variables, buffers, functions and I/O devices. If access is not coordinated, the result depends on the order of execution, which causes the following issues.

Issues

  1. Race condition: two tasks read-modify-write a shared variable at the same time; the result depends on timing. Example: both tasks do counter = counter + 1 and one update is lost.
  2. Critical section problem: the code that accesses a shared resource must run by only one task at a time; otherwise data becomes inconsistent.
  3. Non-reentrant functions: a shared function that uses global or static data gives wrong results if a task is pre-empted inside it and another task calls it.
  4. Deadlock: tasks wait forever for resources held by each other.
  5. Priority inversion: a high-priority task is blocked by a low-priority task holding a lock, while a medium-priority task runs (Mars Pathfinder problem).
  6. Starvation: a low-priority task never gets the resource.

Solutions

  • Mutual exclusion: protect critical sections with mutex/binary semaphore, or disable interrupts for very short sections.
  • Semaphores (counting) to control access to a pool of resources and for signalling.
  • Make functions reentrant: use local variables and parameters, not globals.
  • Priority inheritance / priority ceiling to avoid priority inversion.
  • Resource ordering / request all at once to avoid deadlock.
  • Message queues, mailboxes and events for safe communication instead of shared variables.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Embedded System chapter 6 questions →