Swapping is the procedure of copying out the entire process(its address space) from main memory to some sort of secondary storage(typically hard disk or tape storage). Usually this is done as a result of CPU scheduling.
Operating System typically maintains a read-to-run queue of processes. The queue comprises of information about processes that are ready for the execution in memory. However, the images of these processes can either be in memory or on disk (in which case it needs to be loaded into memory off the disk). If the process that is to be scheduled next is on disk, and there is no free space in main memory, a process that is currently resident in memory is swapped back to disk. The process that is to be executed next can then be loaded into memory and allocated space for its data segment, text segment etc.
The most important thing to note here is that the complete process is swapped in/out. There is no granularity over here as to what part of process’s address space is swapped back to disk. The complete process image in its entirety is swapped back to disk. This should be very clear when we are trying to understand the differences between paging and swapping.
Paging on the other hand is a memory allocation technique that allows the physical address space of the process to be non-contiguous. In other words, a process can be allocated memory wherever it is available and the unit of allocation is the size of page or frame (usually 4KB , but it is system dependent). Wherever a free frame is available in main memory, a physical page belonging to the process can be loaded into it and the page table of process will take care of mapping the logical address (logical page numbers) to correct physical frames in memory (may or may not be contiguous).
If you are trying to understand the differences or establish some sort of relation between paging and swapping, you would probably want to understand demand paging. Demand Paging is a technique to implement virtual memory. Here, the process is again allocated memory in terms of pages, but the entire process(all of its physical pages) need not exist in the memory for the execution to begin.
As and when the memory references are made for the pages currently not resident in memory, the operating system loads the desired page into memory from the disk and updates the page table of the process. While doing so, it may decide to replace some page which is already in memory. This will create space for the new page that is waiting to be loaded in.
The above paragraph is something you are looking for. In swapping, we understood that a process (in memory) is swapped with another process (on disk). In demand paging, a page (in memory and part of process’ address space) is swapped/replaced with another page (on disk, but belongs to the process).
As mentioned in Operating System Concepts (Silberschatz et al.), use of the term swapping is technically incorrect when discussing paging or demand paging. Swapping refers to the manipulation of entire process, where as in paging OS sees the process as a sequence of pages. Hence, I would like to re-frame my above statement —
“A page (in memory and part of process’ address space) is replaced (not swapped) with another page (on disk, but belongs to the process)”
Leave a Reply