---
title: "TSL instruction and Peterson's algorithm"
subject: "Operating System"
chapter: 3
key: "semester_6/operating_system:tsl-peterson-algorithm"
source: https://www.bctengineer.com/q/semester_6/operating_system/tsl-peterson-algorithm
site: BCT Engineering
---

# TSL instruction and Peterson's algorithm

Operating System · Chapter 3: Process Communication and Synchronization

- Asked in: year not given
- Video: [Operating system- Peterson's solution](https://www.youtube.com/watch?v=p3nQHRbZ1LY) — Learning got easy
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_6/operating_system/tsl-peterson-algorithm
- Whole chapter: https://www.bctengineer.com/md/semester_6/operating_system/chapter-3

## Question

What is TSL instruction? Explain its use. Explain Peterson's Algorithm in detail.

## Short answer

TSL is an atomic read-and-set hardware instruction that implements a spin lock. Peterson's algorithm uses turn and interested flags: each process sets interested[process]=TRUE, turn=process, then waits if the other is interested and turn equals itself. It guarantees mutual exclusion, progress, and bounded waiting for two processes without special hardware.

## Answer

#### TSL instruction
**TSL (Test and Set Lock)** is a hardware instruction `TSL RX, LOCK`. In one indivisible step it reads the word LOCK into register RX and stores a non-zero value in LOCK. No interrupt or other CPU can intervene, because the memory bus is locked during the instruction.

#### Use of TSL
It implements mutual exclusion for a critical region:
```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
```
LOCK = 0 means free. A process calls `enter_region` before and `leave_region` after its critical section. Only the process that reads 0 enters; the others spin. It works for any number of processes, but uses busy waiting (a spin lock).

#### Peterson's algorithm
A software solution for mutual exclusion between two processes (0 and 1) with shared variables `turn` and `interested[2]`.

```c
#define N 2                  /* two processes: 0 and 1 */
int turn;                    /* whose turn is it? */
int interested[N];           /* all FALSE at start */

void enter_region(int process) {
    int other = 1 - process;
    interested[process] = TRUE;   /* I want to enter */
    turn = process;               /* but let the other go first if it wants */
    while (turn == process && interested[other] == TRUE)
        ;                         /* busy wait */
}
void leave_region(int process) {
    interested[process] = FALSE;  /* I have left the critical region */
}
```

Steps:
1. Process sets `interested[me] = TRUE` and `turn = me`.
2. It loops while the other is interested and `turn` is still its own value.
3. If the other is not interested, it enters at once. If both try together, the later write to `turn` loses: that process waits, and the other enters.
4. On leaving, the process clears its flag, which releases the waiting one.

Properties: mutual exclusion holds (both cannot pass the loop), progress is guaranteed, and each process waits at most one turn (bounded waiting). Limitations: busy waiting and only two processes; on modern CPUs with reordering it needs memory barriers.

## Check yourself

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

1. Why can no other CPU interfere during a TSL instruction?
   A. TSL runs only on single CPUs
   B. Interrupts are queued for later
   C. LOCK is stored in a register
   D. The memory bus is locked during the instruction
   Answer: D. Locking the bus makes the read and set indivisible across CPUs. (Key point: TSL atomic)

2. How does a process release the lock in the TSL solution?
   A. Stores 1 in LOCK
   B. Stores 0 in LOCK
   C. Executes TSL again
   D. Clears the register only
   Answer: B. MOVE LOCK, #0 marks the region free. (Key point: leave_region in TSL)

3. Which shared variables does Peterson's algorithm use?
   A. count and buffer
   B. turn and interested[2]
   C. semaphores mutex and db
   D. only a lock word
   Answer: B. turn says whose turn it is; interested[] flags each process's desire. (Key point: Peterson variables)

4. What are limitations of Peterson's algorithm?
   A. It allows starvation without bound
   B. Busy waiting and only two processes
   C. It needs special hardware
   D. It violates mutual exclusion
   Answer: B. It is a spin lock for two processes; memory barriers are needed on reordering CPUs. (Key point: Peterson limitations)

## Related in Operating System

- [Process synchronization, Peterson, TSL](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Awhy-synchronize-peterson-tsl) — Operating System, chapter 3
- [Race condition example; Peterson's algorithm](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Arace-condition-example-peterson) — 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
