IAE Academy

GATE 2023 CS Q.22: solution and answer

GATE 2023 · CSQ.22 · 1 mark · MSQ

Which one or more of the following need to be saved on a context switch from one thread (T1) of a process to another thread (T2) of the same process?

  1. (A)

    Page table base register

  2. (B)

    Stack pointer

  3. (C)

    Program counter

  4. (D)

    General purpose registers

See the question in the paper

Answer and solution

ANSWER

The stack pointer, the program counter and the general purpose registers must be saved (B, C, D). The page table base register need not be saved.

Official answer: B, C, DSource: Official answer key, GATE 2023 CS (IIT Kanpur)

  1. Threads of one process share one address space, and so share one page table. Only each thread's own execution state has to be saved on a thread-to-thread switch.

  2. Each thread has its own execution state: the program counter (where T1 resumes), the stack pointer (each thread has its own stack), and the general purpose registers (T1's live values). All three are saved when T1 is switched out.

  3. The page table base register points to the page table of the address space. T1 and T2 belong to the same process, so this value is the same for both and does not change on the switch. It need not be saved. This is the difference from a process switch, where the address space changes.

  4. So the items to save are the stack pointer, the program counter and the general purpose registers: options B, C and D.

KEY IDEA

A thread switch within one process saves only per-thread state: program counter, stack pointer and general purpose registers. The address space, and so the page table base register, is shared and unchanged.

WATCH OUT FOR

Treating a thread switch like a process switch and choosing the page table base register. That register changes only when the address space changes, which does not happen between threads of the same process.

From the GATE 2023 CS paper. Every question of this paper · Download the full paper · All 9 Processes & threads questions, with notes

More Processes & threads questions