edited by
42,969 views
114 114 votes

Which of the following actions is/are typically not performed by the operating system when switching context from process $A$ to process $B$?

  1. Saving current register values and restoring saved register values for process $B$.
  2. Changing address translation tables.
  3. Swapping out the memory image of process $A$ to the disk.
  4. Invalidating the translation look-aside buffer.

6 Answers

Best answer
151 151 votes

Processes are generally swapped out from memory to Disk (secondary memory) when they are suspended. So. Processes are not swapped during context switching.

TLB : Whenever any page table entry is referred for the first time it is temporarily saved in TLB. Every element of this memory has a tag. And whenever anything is searched it is compared against TLB and we can get that entry/data with less memory access.

And Invalidation of TLB means resetting TLB which is necessary because a TLB entry may belong to any page table of any process thus resetting ensures that the entry corresponds to the process that we are searching for.

Hence, option (C) is correct.

edited by
43 43 votes
option C) because swapping out of the memory image of a process to disk is not done on every context switch as it would cause a huge over head but can solve problems like trashing.
37 37 votes

A. When a current process is pre empted its activation record is saved and the data of next process is restored by the OS.

B. Every process have its own page table.

C. Swapping means taking data of a process that we don't need as of now from a higher level memory to lower level memory. It is only done to make space for a new process. If there are space available in main memory there is no need to do swapping. So it is only done by the OS only when it has to.

D. TLB holds records to page table of current process, when a new process comes it must be updated because the entries in TLB may be pointing to some other page table.

So C is the answer.

9 9 votes
  • Saving current register values and restoring saved register values for process A

    • This is a fundamental part of a context switch. The CPU registers must be saved to ensure that process A can resume correctly, and the registers for process B must be restored.
    • This action is performed.
  • Changing address translation tables

    • In a multiprogramming environment, each process may have its own virtual memory address space, requiring the operating system to update the page tables or address translation tables during a context switch.
    • This action is performed.
  • Swapping out the memory image of process A to the disk

    • Swapping a process's memory image to disk is typically not part of a standard context switch because it is a time-consuming operation. Swapping occurs only under specific conditions, such as memory pressure.
    • This action is typically not performed.
  • Invalidating the translation look-aside buffer (TLB)

    • The TLB, a cache of virtual-to-physical address translations, must be invalidated when switching to a process with a different address space to prevent incorrect translations.
    • This action is performed.
 
A very in detail explanation for OPTION D:

What is the TLB?

The Translation Lookaside Buffer (TLB) is a small, fast cache in the CPU that stores recent translations of virtual memory addresses to physical memory addresses. It helps speed up memory access by avoiding repeated lookups in the page table, which resides in slower main memory.


Why invalidate the TLB?

Each process has its own virtual address space, meaning the mappings from virtual addresses to physical addresses (stored in the page tables) are unique for each process. When switching from Process A to Process B, the TLB may still hold address translations for Process A. If these stale entries are used, they could lead to incorrect memory access.

To ensure correctness:

  • The operating system invalidates the TLB (clears it) during a context switch.
  • This forces the CPU to reload the TLB with address translations specific to the new process (Process B) from its page table.

 


How is TLB invalidation done?

The invalidation can happen:

  1. By flushing the entire TLB.
  2. By selectively invalidating entries, depending on the hardware
4 4 votes

How context switch actually happens?

There are two ways we can switch from process A to process B -

1. Cooperative way where the currently running process invokes a system call by it's own choice or does something illegal that generates a trap and OS takes control.

2. Non cooperative way where HW helps the OS to take control by generating a interrupt (usally a timer interrupt).

We will dive deeper into the non cooperative way as it also talks about the steps taken in the cooperative way

Steps -

Suppose a process A is running and a timer interrupt occurs. 

1. The user registers — program counter, stack pointer, and status register — of process A are then implicitly saved by the CPU onto the kernel stack of A. 

2. Then, the hardware switches to kernel mode and jumps into interrupt handler for the operating system to take over. 

3. Then the operating system calls the switch() routine to save A's current kernel registers into the PCB of A, restores kernel registers from the PCB of process B, and switches context, that is, changes kernel stack pointer to point to the kernel stack of process B. 

4. The operating system then returns from interrupt. The hardware then loads user registers from B's kernel stack, switches to user mode, and starts running process B from B's program counter.

In case of cooperative way, we just perform the steps when the OS calls the switch() routine as there is no HW interrupt. Also the OS will save the user register content in that case to the PCB or the kernel stack depending on the OS.

References - 

https://en.wikipedia.org/wiki/Context_switch

https://stackoverflow.com/questions/67955845/during-a-context-switch-does-the-os-use-pcb-or-kernel-stack-to-restore-register ( Has a beautiful diagram :) )

OS: Three easy pieces pg. no. 7-12

Hence, Option A is correct. We will save register values.

In case of address translation table (Here the assumption is that we are using Paging + segmentation),

The above excerpt points tot the fact that we need to change the base register values which in basically pointing to different address translation table. Hence B is correct.

With reference to TLB,

However, flushing TLB leads to initial TLB hit miss the next process even in case where the previous process shared some of the address space with the current running process. 

To tackle this problem we can use ASID (Address space identifier) that identifies address spaces and are stored in the page tables. 

So, pages having same ASID in previous and current processes can still be valid even after context switch. 

But, I don't think the ASID argument in valid in context of the question, because we still need to invalidate TLB entries which are not common.

Hence, Option D is also correct.

Now coming to Option C, A process is moved to the disk (swapping) when there is no enough memory in the physical main memory. It is not necessary that this should happen during context switching. It could happen when the process is in blocked state or ready state. Hence, C is false.

edited by
0 0 votes

Context switching involves saving the CPU state of the currently running process and loading the saved state of the next scheduled process so it can resume execution.

Saving the current CPU registers of process A and restoring the saved register values of process B is the primary step of a context switch, making Option A true

Changing address translation tables is necessary because each process has its own virtual address space, requiring the operating system to update the page table base register during a context switch, which validates Option B.

Invalidating the Translation Lookaside Buffer (TLB) is often required during a context switch to prevent process B from mistakenly using old cached virtual-to-physical translations belonging to process A, validating Option D.

Moving a process to disk is called swapping (or hibernation/paging out), which is handled separately by the memory manager only when the RAM is completely full, not by the CPU scheduler during a standard context switch.

so correct Option: C

Answer:
Position:
Show:

Related questions

43 43 votes
6 answers 6 answers
16.6k
16.6k views
Kathleen asked Sep 23, 2014
16,638 views
The number of binary strings of $n$ zeros and $k$ ones in which no two ones are adjacent is$^{n-1}C_k$$^nC_k$$^nC_{k+1}$None of the above
47 47 votes
4 answers 4 answers
19.3k
19.3k views
Kathleen asked Sep 23, 2014
19,279 views
A multi-user, multi-processing operating system cannot be implemented on hardware that does not supportAddress translationDMA for disk transferAt least two modes of CPU e...
25 25 votes
6 answers 6 answers
7.0k
7.0k views
go_editor asked Feb 28, 2018
6,973 views
Consider the following solution to the producer-consumer problem using a buffer of size 1. Assume that the initial value of count is 0. Also assume that the testing of co...
24 24 votes
6 answers 6 answers
8.4k
8.4k views
Kathleen asked Sep 23, 2014
8,413 views
A certain processor provides a 'test and set' instruction that is used as follows:TSET register, flagThis instruction atomically copies flag to register and sets flag to ...