---
title: "TSL; semaphore operations as class"
subject: "Operating System"
chapter: 3
key: "semester_6/operating_system:tsl-semaphore-class"
source: https://www.bctengineer.com/q/semester_6/operating_system/tsl-semaphore-class
site: BCT Engineering
---

# TSL; semaphore operations as class

Operating System · Chapter 3: Process Communication and Synchronization

- Asked in: 2074 Bhadra, 5 marks
- Video: [L-3.6: Test and Set Instruction in OS | Process Synchronization](https://www.youtube.com/watch?v=9hzoO4hBXFw) — Gate Smashers
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_6/operating_system/tsl-semaphore-class
- Whole chapter: https://www.bctengineer.com/md/semester_6/operating_system/chapter-3

## Question

What is TSL? Why is it used? Explain the major operations of semaphore with a simple implementation as a class.

## Short answer

TSL is an atomic instruction that reads a lock variable and sets it to non-zero, implementing a spin lock. A semaphore is an integer with atomic wait and signal operations; wait decrements and blocks if value becomes negative, signal increments and wakes one blocked process. Its class implementation stores an integer value and a process queue, using TSL for atomicity.

## Answer

#### TSL
**TSL (Test and Set Lock)** is an atomic hardware instruction `TSL RX, LOCK`: it copies the memory word LOCK into register RX and then sets LOCK to a non-zero value, with no interrupt or other CPU able to intervene between the two steps.

#### Why it is used
It is used to implement mutual exclusion (a lock) for a critical region on single and multiprocessor systems:
```text
enter_region:
    TSL REGISTER, LOCK   | copy LOCK to REGISTER, set LOCK = 1
    CMP REGISTER, #0     | was LOCK zero?
    JNE enter_region     | if not zero, lock was set: loop again
    RET                  | LOCK was 0, now 1: enter critical region

leave_region:
    MOVE LOCK, #0        | store 0 in LOCK
    RET                  | return to caller
```

A process enters only if it reads LOCK = 0; otherwise it spins. It is simple and works for many processes, but it uses busy waiting.

#### Semaphore operations
A **semaphore** (Dijkstra) is an integer variable accessed only through two atomic operations:
- **wait / down / P(S):** if S > 0, decrement S and continue; otherwise block the process in the semaphore's queue.
- **signal / up / V(S):** if some process is blocked on S, wake one; otherwise increment S.

A binary semaphore (0 or 1) gives mutual exclusion; a counting semaphore controls access to a pool of resources.

```cpp
class Semaphore {
    int value;                 // semaphore value
    queue<Process*> waiting;   // blocked processes
public:
    Semaphore(int v) { value = v; }
    void wait() {              // P / down  (executed atomically)
        value--;
        if (value < 0) {
            waiting.push(current_process());
            block();           // sleep
        }
    }
    void signal() {            // V / up    (executed atomically)
        value++;
        if (value <= 0) {
            Process* p = waiting.front(); waiting.pop();
            wakeup(p);
        }
    }
};
```
Atomicity is obtained by disabling interrupts or using TSL around these short operations. Usage: `Semaphore mutex(1); mutex.wait(); /* critical section */ mutex.signal();`

## Check yourself

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

1. What does the TSL RX, LOCK instruction do?
   A. Sets LOCK to zero and then reads it
   B. Disables all interrupts permanently
   C. Copies LOCK to RX and sets LOCK non-zero as one indivisible operation
   D. Copies RX to LOCK only if LOCK is 0, otherwise blocks the process
   Answer: C. The read and set happen atomically, with the bus locked against other CPUs. (Key point: TSL atomicity)

2. After executing TSL in enter_region, when does the process enter its critical region?
   A. When the register value read is 0
   B. Always, then it checks later
   C. When the register value read is 1
   D. When LOCK was already set by another process
   Answer: A. Reading 0 means the lock was free and is now 1 (owned by the caller); reading 1 means it loops. (Key point: TSL lock acquisition)

3. In the Semaphore class, what does wait() do?
   A. Spins until value is 0
   B. Increments value and wakes a process
   C. Resets value to 1
   D. Decrements value and blocks the process in the queue if value becomes negative
   Answer: D. value--; if value < 0 the process is queued and blocked. (Key point: Semaphore wait)

4. In signal(), when is a blocked process woken?
   A. Never; processes wake on their own
   B. Whenever value > 0
   C. After value++, if value <= 0 (some process is waiting)
   D. Only when value equals 1
   Answer: C. A value <= 0 after increment means at least one process is waiting in the queue. (Key point: Semaphore signal)

## Related in Operating System

- [Semaphore operations with pseudocode](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Asemaphore-operations-pseudocode) — 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
- [TSL; producer-consumer using monitors](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Apc-monitors-tsl) — Operating System, chapter 3
- [Process synchronization, Peterson, TSL](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Awhy-synchronize-peterson-tsl) — Operating System, chapter 3
- [Semaphore for mutual exclusion](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Asemaphore-enforce-mutual-exclusion) — Operating System, chapter 3
- [File system layout; interface vs implementation](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Afs-layout-interface-vs-implementation) — Operating System, chapter 5

## Related in other subjects

- [Marina class diagram implementation code](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Amarina-class-diagram-implementation-code) — Object Oriented Analysis and Design, chapter 4
- [Spread spectrum disadvantages; CDMA implementation](https://www.bctengineer.com/question.md?key=semester_7%2Fwireless_communication%3Aspread-spectrum-disadvantages-cdma) — Wireless Communication, chapter 7
- [Project attributes; implementation phase activities](https://www.bctengineer.com/question.md?key=semester_7%2Fproject_engineering%3Aproject-attributes-implementation-activities) — Project Engineering, chapter 1
- [Is a specified job a project? Implementation activities](https://www.bctengineer.com/question.md?key=semester_7%2Fproject_engineering%3Ais-job-a-project-implementation) — Project Engineering, chapter 1
