As a task executes it utilizes the processor / microcontroller registers and accesses RAM and ROM just as any other program. These resources together (the processor registers, stack, etc.) comprise the task execution context.
A task is a sequential piece of code – it does not know when it is going to get suspended (swapped out or switched out) or resumed (swapped in or switched in) by the kernel
and does not even know when this has happened. Consider the example of a task being suspended immediately before executing an
instruction that sums the values contained within two processor registers.
While the task is suspended other tasks will execute and may modify the processor register values. Upon resumption the task
will not know that the processor registers have been altered – if it used the modified values the summation would result in an
To prevent this type of error it is essential that upon resumption a task has a context identical to that immediately prior to
its suspension. The operating system kernel is responsible for ensuring this is the case – and does so by saving the context of
a task as it is suspended. When the task is resumed its saved context is restored by the operating system kernel prior to its
execution. The process of saving the context of a task being suspended and restoring the context of a task being resumed is
called context switching.