3
votes

I have read the description in several popular OS textbooks of what happens during a context switch. None of them have left me completely satisfied, though the one quoted below (Tanenbaum) comes most close. There are a couple questions it leaves me with. Each one is highlighted in bold and elaborated below.

Assume that user process 3 is running when a disk interrupt occurs. User process 3's program counter, program status word, and often one or more registers are pushed onto the (current) stack by the interrupt hardware. The computer then jumps to the address specified in the interrupt vector. That is all the hardware does. From here on, it is up to the software, particularly, the interrupt service procedure.

Why must the hardware save the PC and PSW, but the software can save everything else (see next quote below)?

I am guessing it is because once execution has jumped to the interrupt service procedure, the PC and PSW are lost (because they've been replaced by those for the service procedure). So the hardware must do it. Is this correct?

All interrupts start by saving the registers, sometimes in the process table entry for the current process. Then the information pushed onto the stack by the interrupt is removed and the stack pointer is set to point to a temporary stack used by the process handler.

The way this is worded (the word "removed" specifically) it makes it look like the old process' registers are saved (by kernel software) to the process table, and then the PC and PSW which were pushed to old process stack by the hardware (previous paragraph) are just discarded (again, the word "removed"). Obviously they can't be discarded since we'll need them in the future, and also it would be stupid since we made a point to put them on that stack!

I am guessing that when they say "removed" they mean "removed... and then put in the process table along with all the register and other info that the kernel already put there." So now the inactive process is ready to go again since (a) its process table is complete and (b) the temp stuff (PC/PSW) that was on top of its stack is cleaned away. Is this correct?

1

1 Answers

2
votes

Question 1- Yes, correct - PC & PSW and any other registers - depending on the architecture - are destroyed when jumping into interrupt handler routine. After the interrupt handler finishes, the stored information is used to restore the interrupted process state as nothing has happend.

Question 2 - remove from stack means to move respective stack pointer to a value which it has before the removed data were added. Respective stack pointer is decremented or incremented depending on the direction of stack growth.