---
title: "TSL; producer-consumer using monitors"
subject: "Operating System"
chapter: 3
key: "semester_6/operating_system:pc-monitors-tsl"
source: https://www.bctengineer.com/q/semester_6/operating_system/pc-monitors-tsl
site: BCT Engineering
---

# TSL; producer-consumer using monitors

Operating System · Chapter 3: Process Communication and Synchronization

- Asked in: 2072 Asoj, 1+2+7 marks
- Video: [Operating System | Synchronization | Hardware Solution | TSL instruction set | TSL register](https://www.youtube.com/watch?v=E6RrZPzPksA) — Pepcoding
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_6/operating_system/pc-monitors-tsl
- Whole chapter: https://www.bctengineer.com/md/semester_6/operating_system/chapter-3

## Question

What is TSL instruction? Why is it used? Solve the producer-consumer problem using monitors.

## Short answer

TSL (Test and Set Lock) is an atomic instruction that reads a lock variable and writes a non-zero value, implementing mutual exclusion without disabling interrupts. The monitor solution encapsulates buffer access with automatic mutual exclusion and condition variables full and empty; producer waits on full if buffer is full, consumer waits on empty if empty.

## Answer

#### TSL instruction and its use

**TSL (Test and Set Lock)** is a hardware instruction, `TSL REGISTER, LOCK`, which **atomically** reads the memory word `LOCK` into a register and then stores a non-zero value in `LOCK`. No other CPU can access that memory word until the instruction is finished (the memory bus is locked). It is used to implement **mutual exclusion** for a critical region without disabling interrupts, even on multiprocessors:

```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, so loop
    RET                    ; return, now in the critical region

leave_region:
    MOVE LOCK, #0          ; store 0 in LOCK
    RET
```

A process calls `enter_region` before and `leave_region` after its critical region. Because the test and set are indivisible, only one process can see `LOCK = 0`; this removes the race condition of simple lock variables. It uses busy waiting (a spin lock).

#### Producer-consumer using monitors

A **monitor** is a collection of procedures, variables and data structures grouped in a special module; **only one process can be active inside a monitor at a time** (the compiler enforces mutual exclusion). Condition variables, with the operations `wait` and `signal`, are used for synchronization.

```text
monitor ProducerConsumer
    condition full, empty;
    integer count;                       { items in buffer }

    procedure insert(item: integer);
    begin
        if count = N then wait(full);    { buffer full: sleep }
        insert_item(item);
        count := count + 1;
        if count = 1 then signal(empty); { wake consumer }
    end;

    function remove: integer;
    begin
        if count = 0 then wait(empty);   { buffer empty: sleep }
        remove := remove_item;
        count := count - 1;
        if count = N - 1 then signal(full); { wake producer }
    end;
begin
    count := 0;
end monitor;

procedure producer;
begin
    while true do begin
        item := produce_item;
        ProducerConsumer.insert(item)
    end
end;

procedure consumer;
begin
    while true do begin
        item := ProducerConsumer.remove;
        consume_item(item)
    end
end;
```

Since only one process can be inside the monitor, the buffer is never accessed simultaneously, so no `mutex` is needed. `wait` blocks the caller and releases the monitor; `signal` wakes a waiting process. The programmer need not manage mutual exclusion, so errors like wrong `down` order cannot occur.

## Check yourself

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

1. What does the TSL REGISTER, LOCK instruction do?
   A. Disables all interrupts
   B. Atomically copies LOCK into a register and sets LOCK to non-zero
   C. Sleeps the process until LOCK is zero
   D. Clears LOCK after reading it
   Answer: B. TSL reads and sets the lock word indivisibly, locking the memory bus so no other CPU can access it. (Key point: TSL atomicity)

2. What advantage does TSL have over disabling interrupts?
   A. It avoids busy waiting
   B. It needs no hardware support
   C. It works on multiprocessors and gives mutual exclusion
   D. It is faster for user processes to hang
   Answer: C. TSL locks the memory word across CPUs, so it works on multiprocessors, though it uses busy waiting. (Key point: Why TSL is used)

3. What guarantees mutual exclusion inside a monitor?
   A. The programmer's mutex semaphore
   B. Only one process can be active in the monitor at a time, enforced by the compiler
   C. Disabling interrupts globally
   D. Condition variables alone
   Answer: B. The compiler enforces that only one process is active in the monitor, so no explicit mutex is needed. (Key point: Monitor property)

4. In the monitor solution, what happens when the producer calls wait(full) because count = N?
   A. It is blocked and releases the monitor so the consumer can enter
   B. It keeps the monitor and spins
   C. It overwrites the oldest item
   D. It terminates
   Answer: A. wait blocks the caller and releases the monitor, allowing the consumer to remove an item and signal. (Key point: Condition wait)

## Related in Operating System

- [Producer-consumer using semaphore](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Apc-semaphore-mutual-exclusion) — Operating System, chapter 3, asked 2×
- [Producer-consumer: semaphore and messages](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Apc-semaphore-message-passing) — Operating System, chapter 3
- [TSL instruction and Peterson's algorithm](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Atsl-peterson-algorithm) — 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
- [TSL; semaphore operations as class](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Atsl-semaphore-class) — Operating System, chapter 3
