A process runs on the CPU until it is context switched. This happens when one of the following occurs:
If the process has used up its time slice or is preempted, it is returned to the run queue. If it cannot proceed without access to a resource such as disk I/O, it sleeps until the resource is available. Once access to that resource is available, the process is placed on the run queue before being put on the processor. ``Preemption of a process that goes to sleep waiting for I/O'' illustrates this for a process O which goes to sleep waiting for I/O.
Preemption of a process that goes to sleep waiting for I/O
A context switch occurs when the kernel transfers control of the CPU from an executing process to another that is ready to run. The kernel first saves the context of the process. The context is the set of CPU register values and other data that describes the process' state. The kernel then loads the context of the new process which then starts to execute.
When the process that was taken off the CPU next runs,
it resumes from the point at which it was taken off the CPU.
This is possible because the saved context
includes the instruction pointer.
This indicates the point in the executable code
that the CPU had reached when the context switch occurred.