In Operating Systems, what is the difference between "swapping" and "context switching"? The only difference I found in my textbook is that, in swapping there is a medium-term scheduler. Shed some light on it.
5 Answers
Swapping is the term in OS generally used when we talk about exchange between main memory and disk. For example: A process currently running on a cpu needs some more pages stored in the disk. So, the swapper will swap out the pages of the other process(waiting,terminated..) pages from main memory into the disk while swapping in the required pages.
Context switch on the other hand switches the process from running state to ready state, while allocation of cpu to a process present in ready queue is done with the help of dispatcher.
Note: This is a simple example of context switch. The more is the complexity of an OS the more work is done during context switch.
Swapping is saving the current computational state (when preempted or for some other reason) of a process from physical memory to secondary storage, normally HDD. and/or loading the current computational state of a process from HDD to physical memory.
When the OS allocates CPU from one process to another, it is required for computation to be meaningful that before the CPU switches from the currently running process to the other process the current computational state of the currently running process must be saved into a semi-permanent storage i.e. HDD so that the process when gets the CPU next time it can resume its execution from where it left. This operation requires some time say, t units. After the process state is saved, the state of the process which is to be allocate the CPU must be brought from HDD into physical memory. This also requires some time, p units. These t + p = z(say) is the context switch. Thus, context switch is acually the time require for swap-in and swap-out operation.