Using the Operating System via context switching processes, we can shift from one unrelated task to the other. The context switching process helps us perform various tasks on one CPU. We can simultaneously work on a text editor, draft an email over an email service provider, and code for a programming assignment all on one CPU with concurrency.
Each of the different programs we run on our machines and the routines administered in the background for the necessary working of our machines is registered as different processes.
Each process can be categorized into different states as per its status.
New: A process is still in the creation phase.
Ready: A process that is in line for execution by a processor.
Running: A process that is currently being executed.
Waiting: A process that is waiting for a signal or user input.
Terminated: A process that has been entirely executed.
The CPU, at any given point in time, administers the execution of a program in the running state.
The running state of a program that is under processing by the CPU is as shown below:
Program instructions: The execution steps of the program.
Heap: Dynamically available memory storage for process variables.
Stack: Stores static variables and other process-related information.
Registers: Used to store process-related values for faster computation.
Note: It is imperative to understand that all of the process-related information that is in use by the CPU needs to be kept track of and updated to resume execution after a program is halted correctly. The process-related information is stored in the Process Control Block.
The operating system schedules the process aptly as per the process's duration, usage, and user interactivity. The switch from the execution of one process to the other while capturing all relevant updates and execution information from the CPU state of the process is enabled by context switching.
The operating system would instruct context switching in examples of some scenarios as described below:
If a process exceeds its allotted time slot, the operating system revokes its execution and signals the following process in the queue to be pushed into the running phase. The clock interrupt consequently triggers a context switch.
A process in execution might require a value from secondary memory to be accessed, which is time-consuming, and the equivalent amount of time can be utilized in running another process. This would be an efficient approach that is merely enabled by the context switching capabilities of the operating system.
On the whole, it can be consolidated that context switching allows for better process management to maintain the interactivity of the applications while also simultaneously proceeding with essential background tasks.
Free Resources