Glossary · Automation software engineering and architecture
Context switch
Also known as: Task switch
German: Kontextwechsel
In operating systems and real-time software, a context switch is the process of saving the state of the currently running task, thread or process, such as its registers and stack pointer, and restoring the state of another one so the processor can continue executing it.
- Software engineering
In one sentence
A context switch saves the state of the running task and restores another one's, so the processor can switch between tasks.
Example
When the 1 ms motion task becomes ready, the real-time kernel performs a context switch from the lower-priority logic task, which resumes after the motion task finishes.
How it applies
- Engineering: Each context switch costs processor time and can disturb caches. Frequent switches add overhead and contribute to Execution jitter. Real-time operating systems keep switch times short and bounded.
- Design: Task priorities and cycle times determine how often switches occur. Too many tasks, or tasks with very short cycles, can consume a noticeable share of the processor.
- Documentation: For performance-critical systems, document the task model and measured timing, including worst-case context switch and interrupt latency where the platform vendor specifies them. This supports later extensions and troubleshooting of timing problems.
Preemptive vs. cooperative switching
In preemptive scheduling, the kernel can switch tasks at any time, for example when a higher-priority task becomes ready. In Cooperative scheduling, a task gives up the processor voluntarily. Preemption gives better response times for urgent tasks but requires protection of shared data.