---
title: "Semaphore operations with pseudocode"
subject: "Operating System"
chapter: 3
key: "semester_6/operating_system:semaphore-operations-pseudocode"
source: https://www.bctengineer.com/q/semester_6/operating_system/semaphore-operations-pseudocode
site: BCT Engineering
---

# Semaphore operations with pseudocode

Operating System · Chapter 3: Process Communication and Synchronization

- Asked in: 2073 Bhadra, 5 marks
- Video: [Semaphores in Operating System || Process Synchronization](https://www.youtube.com/watch?v=5y14WXehSU4) — Sudhakar Atchala
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_6/operating_system/semaphore-operations-pseudocode
- Whole chapter: https://www.bctengineer.com/md/semester_6/operating_system/chapter-3

## Question

Define Semaphore and explain the major operations in semaphore including pseudocode.

## Short answer

A semaphore is a synchronization variable with an integer value and a queue of blocked processes, accessed only by atomic down and up operations. down decrements the value and blocks the caller if it becomes negative; up increments and wakes a blocked process if the value is <= 0. It is used for mutual exclusion and synchronization.

## Answer

A **semaphore** is a synchronization variable (an integer with an associated queue of blocked processes), introduced by E. W. Dijkstra, which can be accessed only through two atomic operations. It is used to protect critical sections and to synchronize processes without busy waiting.

#### Major operations

1. **Initialisation**: the semaphore is given a non-negative value (1 for a mutex, N for N resources).
2. **down (P, wait)**: tries to decrement the value; if the value is 0, the process is blocked.
3. **up (V, signal)**: increments the value or wakes up a blocked process.

#### Pseudocode

```c
typedef struct {
    int value;                 /* count */
    struct process *list;      /* queue of blocked processes */
} semaphore;

void down(semaphore *S) {      /* P operation */
    S->value = S->value - 1;
    if (S->value < 0) {
        add this process to S->list;
        block();               /* sleep */
    }
}

void up(semaphore *S) {        /* V operation */
    S->value = S->value + 1;
    if (S->value <= 0) {
        remove a process P from S->list;
        wakeup(P);
    }
}
```

(The simple version is: `down(S): while (S <= 0) wait; S--;` and `up(S): S++;`. The blocking version above avoids busy waiting.) Both procedures execute as indivisible actions, with interrupts disabled or with a TSL lock, so two processes cannot change S simultaneously.

#### Use for mutual exclusion

```c
semaphore mutex = 1;
down(&mutex);
   /* critical section */
up(&mutex);
```
A **binary semaphore** takes only the values 0 and 1; a **counting semaphore** takes any non-negative value. Semaphores are also used for synchronization (producer-consumer, dining philosophers, readers-writers).

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What is a semaphore?
   A. A hardware instruction
   B. An integer with a queue of blocked processes, accessed only via atomic down and up
   C. A shared file
   D. A scheduling queue
   Answer: B. Introduced by Dijkstra, it is changed only through atomic down (P) and up (V). (Key point: Semaphore definition)

2. In the blocking pseudocode, when does up(S) wake a process?
   A. When value <= 0 after increment, meaning someone is waiting
   B. Always
   C. When value > 5
   D. When the list is empty
   Answer: A. A value still <= 0 after increment means processes are blocked, so one is removed and woken. (Key point: Up operation)

3. How does a binary semaphore differ from a counting semaphore?
   A. Binary takes only 0 and 1; counting takes any non-negative value
   B. Binary has no queue
   C. Counting is used only for mutex
   D. They are the same
   Answer: A. A binary semaphore (mutex) is limited to 0 and 1; a counting one counts N resources. (Key point: Binary vs counting)

## Related in Operating System

- [TSL; semaphore operations as class](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Atsl-semaphore-class) — Operating System, chapter 3
- [pipe(); semaphore in distributed systems](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Apipe-semaphore-distributed) — Operating System, chapter 3
