Processes and Process Context

Process Context

Each time a process is removed from access to the processor, sufficient information on its current operating state must be stored such that when it is again scheduled to run on the processor it can resume its operation from an identical position. This operational state data is known as its context and the act of removing the process's thread of execution from the processor (and replacing it with another) is known as a process switch or context switch.

The distinction is made here between the condition where a process is removed from the processor completely and is replaced by another process (a process switch) and the case where the process state is stored while execution is interrupted temporarily (a context switch). Note that a process switch performs a superset of the operations required for the context switch. The latter case may be when an external interrupt is serviced or when a system call necessitates a switch from user mode to system mode. In the case of the process switch much more information must be saved in order to later restore the process context than in the second case where the process remains resident in memory while it's thread of execution is interrupted.

The context of a process includes its address space, stack space, virtual address space, register set image (e.g. Program Counter (PC), Stack Pointer (SP), Instruction Register (IR), Program Status Word (PSW) and other general processor registers), updating profiling or accounting information, making a snapshot image of its associated kernel data structures and updating the current state of the process (waiting, ready, etc).

This state information is saved in the process's process control block which is then moved to the appropriate scheduling queue. The new process is moved to the CPU by copying the PCB info into the appropriate locations (e.g. the program counter is loaded with the address of the next instruction to execute).

Linux |  | Windows NT
Each process's context is described by a task_struct structure. The task_struct holds data such as the scheduling policy, scheduler priority, real time priority, processor allowed time counter, processor registers, file handles (files_struct), virtual memory (mm_struct). |  | The kernel maintains a view of a process or thread known as a kernel process object or kernel thread object respectively. These contain just the information the kernel needs to effectively switch between processes or threads.
When a process switch is made the scheduler saves the process's task_struct and replaces the current tasks pointer with a pointer to the new process's task_struct, restoring its memory access and register context. This may be assisted by hardware. |  | The kernel makes a context switch by pushing context information onto the current kernel mode stack, for a process it also saves the address of it's page table directory so that it's address space is maintained.