• A process that need to be executed must be present into the memory.
  • When there is not enough physical memory available for any process or page to be allocated then the operating system must have to take decision to remove some process or page from the memory to a backing store( like disk) temporarily.
  • The different operating system uses different algorithms for taking this decision.
  • For example the Linux uses aging technique for swapping. According to which, when a page is accessed frequently, it becomes younger and the page which is accessed less it becomes old. Old pages are selected for swapping.
  • In a multi-programming system with a round-robin scheduling algorithm, when a time slice expires, memory manager starts to swap out the process that have finished and swap in another process to the memory.
  • Another swapping policy is priority based scheduling algorithm.
  • When any higher priority process arrives for execution, the memory management swaps out the lower priority process so that it can load and execute the higher priority process.

Swapping

  • After completing the execution of higher priority process, the lower priority process is swap back and continues its execution. This type of swapping is also called roll out (swap out) and roll in (swap in).
  • Normally a previously swapped out process will be swapped back into the same memory space but it depends on the address binding method.
  • If address binding method is execution time binding then a process can be swapped to a different memory space as physical address are computed during execution time.
  • Swapping requires a fast backing store like a disk. It must be large enough to accommodate copies of all memory images for all users.
  • When CPU scheduler decides to execute a process it calls dispatcher that check whether the next process in the queue is in main memory.
  • If it is not and there is not enough free memory available, the dispatcher swaps out any current process and swaps in the desired process.
  • If we want to swap a process it must be completely idle.
  • A process waiting for an I/O operation should never be swapped to free-up its memory.
  • If we want to swap out process p1 and swap in process p2, the I/O operation of process p1 might attempt to use memory that belongs to process p2.
  • Main solution of this problem is never to swap a process with pending I/O or execute an I/O operation only into operating system buffers.