</>EmbeddedInterview

Explain mutex, semaphore and spinlock in very brief.

MediumOperating System Concepts

Mutex, semaphore and spinlock are synchronization mechanisms used to control concurrent access to shared resources and prevent race conditions.

1. Mutex (Mutual Exclusion)

A mutex allows only one thread to access a shared resource at a time.

If another thread holds the mutex, the requesting thread normally goes to sleep until the mutex becomes available.

mutex_lock(&lock);

shared_data++;

mutex_unlock(&lock);
Thread 1Thread 2Critical sectionSleepingCritical sectionThread 1 releases mutex
  • Has ownership: the thread that acquires it must release it.
  • Can sleep.
  • Cannot be used in interrupt context.
  • Used for protecting shared data and resources in process context.

2. Semaphore

A semaphore maintains a counter representing available resources or permits.

struct semaphore sem;

sema_init(&sem, 3);

down(&sem);

// Access resource

up(&sem);
✓Thread 1Resource acquired✓Thread 2Resource acquired✓Thread 3Resource acquiredCounter = 0Thread 4 waits
  • down() decrements the counter when a permit is available; otherwise, it waits.
  • up() releases a permit and may wake a waiting thread.
  • Binary semaphore: Counter initialized to 1.
  • Counting semaphore: Counter initialized to N.
  • Has no ownership requirement.

Used for resource management and synchronization.

3. Spinlock

A spinlock continuously checks whether the lock is available instead of putting the requesting thread to sleep.

spin_lock(&lock);

shared_data++;

spin_unlock(&lock);
CPU 0CPU 1Critical sectionSpinning (busy wait)Critical sectionCPU 0 releases lock

At the ARM hardware level, spinlocks can use atomic instructions such as:

LDAXR   // Exclusive load with acquire semantics
STXR    // Exclusive store
STLR    // Release lock

Processors supporting LSE may use other atomic instructions.

  • Does not sleep in a conventional non-RT Linux kernel.
  • Consumes CPU cycles while waiting.
  • Used for very short critical sections.
  • Can be used in interrupt context with appropriate IRQ handling.

If the same lock is accessed from process and interrupt contexts:

spin_lock_irqsave(&lock, flags);

shared_data++;

spin_unlock_irqrestore(&lock, flags);

4. Mutex vs Semaphore vs Spinlock

Feature Mutex Semaphore Spinlock
Waiting mechanism Sleep Sleep Busy wait
Ownership Yes No Yes
Concurrent holders One N permits One
CPU usage while waiting Low Low High
Interrupt context No up() only Yes, with proper IRQ handling
Critical section Longer Resource-dependent Very short
Can sleep while holding Yes Yes No
Main purpose Mutual exclusion Resource counting or signaling Atomic-context protection

Interview takeaway:

  • Mutex: Sleep until exclusive access becomes available.
  • Semaphore: Allow up to N threads to access resources.
  • Spinlock: Keep checking until the lock becomes available.

The spinlock behavior described above applies to conventional non-PREEMPT_RT Linux kernels; raw_spinlock_t preserves spinning semantics on PREEMPT_RT kernels as well.