This is also known as context switching.
Context switches are usually computationally intensive, and much of the design of operating systems is to optimize the use of context switches. Switching from one process to another requires a certain amount of time for doing the administration – saving and loading registers and memory maps, updating various tables and lists, etc. What is actually involved in a context switch varies between these senses and between processors and operating systems.
For example, in the Linux kernel, context switching involves switching registers, stack pointer, and program counter. In extreme cases, such as switching between goroutines in Go, a context switch is equivalent to a coroutine yield, which is more expensive than a subroutine call.
1. In a switch, the state of process currently executing must be saved somehow, so that when it is rescheduled, this state can be restored.
2. The process state includes all the registers that the process may be using, especially the program counter, plus any other operating system specific data that may be necessary. This is usually stored in a data structure called a process control block (PCB) or switchframe.
3. A handle to the PCB is added to a queue of processes that are ready to run, often called the ready queue.
4. Since the operating system has effectively suspended the execution of one process, it can then switch context by choosing a process from the ready queue and restoring its PCB.
5. In doing so, the program counter from the PCB is loaded, and thus execution can continue in the chosen process. Process and thread priority can influence which process is chosen from the ready queue (i.e., it may be a priority queue).