Open In App

Difference between Swapping and Context Switching

Last Updated : 22 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Programs are sets of instructions designed to accomplish specific tasks. Similarly, a process refers to a runtime instance of a computer program. During the execution of a program, several threads may be running in parallel. Single-threaded processes refer to the thread itself as the process.

1. Context switching :
An operating system uses this technique to switch a process between states to execute its functions through CPUs. It is a process of saving the context(state) of the old process(suspend) and loading it into the new process(resume). It occurs whenever the CPU switches between one process and another. Basically, the state of CPU’s registers and program counter at any time represent a context. Here, the saved state of the currently executing process means to copy all live registers to PCB(Process Control Block). Moreover, after that, restore the state of the process to run or execute next, which means copying live registers’ values from PCB to registers.
 

2. Swapping : 
This is the process by which a process is temporarily swapped (moved) from the main memory (RAM) to the secondary memory (Disk). The main memory is fast but has less space than secondary storage, so the inactive processes are moved to secondary memory, and the system swaps the memory from secondary to the main memory later. During swapping, most of the time is spent transferring information, and the amount of memory swapped is directly proportional to the total time. Swapping has been divided into two more concepts: Swap-in and Swap-out.

Swap-in is the process of removing a program from a hard disk and moving it back to the main memory or RAM.
Swap-out removes a program from RAM or main memory and moves or stores it to the hard disk or secondary storage.
 

Difference between Swapping and Context Switching :

Context switching

Swapping

It is a procedure for storing the state of an old process and loading it into a new process. Essentially, it is a method of replicating the entire process.
A context switch occurs when the kernel switches contexts when it transfers control of the CPU from one process to another already ready to run state. Swapping happens when the entire process is moved to the disk.
A context switch determines whether a process is in the pause mode. When it comes to Swapping, It deals with memory, how much memory is being swapped.
The context switch toggles the process from running to ready states, while the dispatcher is responsible for allocating CPU resources to processes present in the ready queue. It is an OS term that we use for referring to the exchange of data between the disk and the main memory.
Active processes do context switching.  Inactive processes do swapping.
It offers a higher degree of multi-tasking. It provides a more significant degree of multiprogramming.
It helps to get better utilization of the operating system. It helps to get better utilization of memory.

 


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads