Processes and context switching
Understand what the operating system schedules and which state must be preserved.
What you’ll learn
- Distinguish a program and a process
- Explain a context switch conceptually
- Separate concurrency from parallel execution
A program becomes an execution
A program is code and associated data stored in a form that can be executed. A process is a running instance with execution state and operating-system-managed resources. Starting the same executable twice can create two separate processes, each with its own view of memory and its own progress through the code. This distinction explains why a source file is not itself a schedulable running activity and why two launches can behave independently even when they began from identical instructions.
The CPU needs a current context
To continue an execution, the system needs information such as the instruction position, register values, and other architecture-dependent state. The operating system also maintains information about the process, its memory mappings, and its resources. The exact layout depends on the platform, so treat a simple process-control diagram as a conceptual aid rather than a universal binary structure. What matters first is that pausing and resuming work requires preserving enough state to continue correctly.
Switch between runnable work
A context switch changes which execution context is running on a processor. The kernel saves relevant state for the outgoing work and restores state for the incoming work. A scheduler chooses among work that is ready to run according to its policy. Work waiting for an event such as I/O completion is not necessarily eligible to use the CPU. This is why a process can exist without currently executing an instruction and why waiting and running should be represented as different states.
Concurrency is not always parallelism
Several tasks can make progress over time on one CPU through interleaving. Actual simultaneous execution requires suitable hardware resources and runnable work. From a program's perspective, the order of interleavings can still matter even on a single processor. Threads within a process share some resources while keeping their own execution state; processes usually provide a stronger isolation boundary. The comparison should account for shared state, communication needs, and overhead rather than declaring one mechanism universally faster.
Observe before concluding
Use an operating-system process viewer or a read-only command to observe a program that alternates computation and waiting. CPU utilization, elapsed time, and process state answer different questions. A low CPU percentage does not necessarily mean the program is efficient; it may be blocked on a slow dependency. Likewise, a high context-switch count needs workload context before it can be called a defect. Begin with a hypothesis and compare observations with what the program is expected to do.
Try it yourself
Explain why two processes created from the same executable can have different variable values while running.
Show a possible solution
Each process has its own execution state and virtual address-space view. They can execute different paths or receive different inputs, and ordinary private memory updates in one do not directly overwrite the other process’s private state.
Check your understanding
Try 14 questions across operating systems. Read each explanation and revisit anything that needs another pass.
Practise this subjectGo to the source
Use these primary references to check details and explore the topic further.
Saved in this browser. Signed-in progress syncs to your account.