Winter Semester 2026–27 · VIT-AP · BACSE106 · Operating Systems · Dr. Krithika L.B

Session 3.4: Semaphores

Dijkstra's wait() and signal(): binary and counting semaphores, using them for mutual exclusion, ordering and resource counting, and implementing them without busy waiting.

Module 3 1.5 hours Theory CO3

Learning objectives

By the end of this session you should be able to:

  • Define a semaphore and its atomic wait() and signal() operations.
  • Distinguish binary and counting semaphores and use each correctly.
  • Use a semaphore to enforce an execution order between two processes.
  • Explain the blocking implementation and interpret negative semaphore values.
  • Show how careless semaphore use causes deadlock, starvation or priority inversion.

Lecture notes

1. Definition

Dijkstra's semaphore S is an integer variable that, apart from initialisation, is accessed only through two atomic operations: wait() (originally P, from Dutch proberen, “to test”) and signal() (V, verhogen, “to increment”).

Classical (busy-waiting) definition
wait(S) {
    while (S <= 0)
        ;          /* busy wait */
    S--;
}

signal(S) {
    S++;
}
Binary semaphore

Value 0 or 1. Behaves like a mutex lock: initialise to 1, wait before the CS, signal after.

Counting semaphore

Any non-negative value. Initialise to the number of available instances of a resource; each user does wait to take one and signal to return it.

2. Using semaphores

Mutual exclusion

mutex = 1
semaphore mutex = 1;
do {
    wait(mutex);
        /* critical section */
    signal(mutex);
        /* remainder section */
} while (true);

Ordering (synchronisation): S2 in P2 must run after S1 in P1

synch = 0
semaphore synch = 0;

/* P1 */            /* P2 */
S1;                 wait(synch);    /* blocks until P1 signals */
signal(synch);      S2;

Counting: at most N users of a resource

Counting semaphore S = 2 guarding two printers; three processes arriveStep through the interleaving one instruction at a time.

3. Implementation without busy waiting

The busy-waiting definition wastes CPU (a spinlock). Instead, each semaphore gets a waiting list. A process that must wait blocks itself (moves to the waiting state, Session 2.1); signal wakes up one waiting process (moves it to ready).

Blocking semaphore
typedef struct {
    int value;
    struct process *list;     /* processes waiting on this semaphore */
} semaphore;

wait(semaphore *S) {
    S->value--;
    if (S->value < 0) {
        add this process to S->list;
        block();              /* system call: running -> waiting */
    }
}

signal(semaphore *S) {
    S->value++;
    if (S->value <= 0) {
        remove a process P from S->list;
        wakeup(P);            /* system call: waiting -> ready   */
    }
}
Reading the value

In this implementation the value can become negative: if S = −k, then k processes are waiting. wait and signal must themselves be atomic — the kernel protects them with interrupt disabling (single CPU) or a spinlock (SMP); the busy waiting is moved from the application's long critical section to these few instructions.

4. Deadlock and starvation with semaphores

Deadlock with two semaphores S = Q = 1Step through the interleaving one instruction at a time.

Starvation (indefinite blocking) happens if the waiting list is served in LIFO order, for example. Priority inversion: a high-priority process waits for a lock held by a low-priority one, which is itself preempted by a medium-priority process — the high-priority process effectively waits for the medium one. The fix is priority inheritance: the lock holder temporarily inherits the waiting process's high priority (famously needed on the Mars Pathfinder rover in 1997).

5. Semaphores in C (POSIX)

POSIX unnamed semaphores (used in Labs 9 and 10)
#include <semaphore.h>

sem_t s;
sem_init(&s, 0, 1);     /* 0 = shared between threads of this process; initial value 1 */
sem_wait(&s);           /* wait / P   */
    /* critical section */
sem_post(&s);           /* signal / V */
sem_destroy(&s);

VideoBytes

Semaphores: wait and signal

Video coming soon

Semaphores: wait and signal

Recorded by Dr. Krithika L.B. Slot m3-semaphores

Blocking semaphores, deadlock and priority inversion

Video coming soon

Blocking semaphores, deadlock and priority inversion

Recorded by Dr. Krithika L.B. Slot m3-semaphore-impl

Interactive lessons

Work through these in order after the lecture — each one expands a part of the notes with diagrams, hover notes and a quick check.

Lab connection

Check your understanding

Answer each question in your own words first, then reveal the model answer.

Revealed 0
A counting semaphore has value −3. What does that mean?

The resource is fully in use and three processes are blocked in its waiting list.

How do you make statement B in P2 execute only after statement A in P1?

Semaphore s = 0; P1: A; signal(s). P2: wait(s); B.

Why is the blocking implementation better than busy waiting?

Waiting processes do not consume CPU cycles; they sleep until signalled.

Give an order of wait() calls that causes deadlock with two semaphores.

P0: wait(S); wait(Q). P1: wait(Q); wait(S). If each completes its first wait before the other's second, both block forever.

What is priority inheritance?

A low-priority process holding a lock needed by a high-priority process temporarily runs at the high priority so it can release the lock quickly.

Key takeaways

  • Semaphore = integer + atomic wait (P, decrement/possibly block) and signal (V, increment/possibly wake).
  • Binary semaphores give mutual exclusion; counting semaphores manage N instances; initial value 0 enforces order.
  • Blocking implementation: waiting list, block() and wakeup(); misuse leads to deadlock, starvation, priority inversion.

Where this leads

With semaphores in hand we can solve the three classic problems every OS course uses to test a synchronization tool.

Continue to 3.5 — Classical synchronization problems →