Swapping in OS

Learn via video courses
Topics Covered

Overview

Swapping in OS is one of those schemes which fulfill the goal of maximum utilization of CPU and memory management by swapping in and swapping out processes from the main memory. Swap in removes the process from hard drive(secondary memory) and swap out removes the process from RAM(main memory).

What is Swapping in Operating Systems (OS)?

Let's suppose there are several processes like P1, P2, P3, and P4 that are ready to be executed inside the ready queue, and processes P1 and P2 are very memory consuming so when the processes start executing there may be a scenario where the memory will not be available for the execution of the process P3 and P4 as there is a limited amount of memory available for process execution.

Swapping in the operating system is a memory management scheme that temporarily swaps out an idle or blocked process from the main memory to secondary memory which ensures proper memory utilization and memory availability for those processes which are ready to be executed.

When that memory-consuming process goes into a termination state means its execution is over due to which the memory dedicated to their execution becomes free Then the swapped-out processes are brought back into the main memory and their execution starts.

The area of the secondary memory where swapped-out processes are stored is called swap space. The swapping method forms a temporary queue of swapped processes in the secondary memory.

In the case of high-priority processes, the process with low priority is swapped out of the main memory and stored in swap space then the process with high priority is swapped into the main memory to be executed first.

The main goals of an operating system include Maximum utilization of the CPU. This means that there should be a process execution every time, the CPU should never stay idle and there should not be any Process starvation or blocking.

Different process management and memory management schemes are designed to fulfill such goals of an operating system.

Swapping in OS is done to get access to data present in secondary memory and transfer it to the main memory so that it can be used by the application programs.

It can affect the performance of the system but it helps in running more than one process by managing the memory. Therefore swapping in os is also known as the memory compaction technique.

swapping in os

There are two important concepts in the process of swapping which are as follows:

  1. Swap In
  2. Swap Out

Refer to the 'Swap In and Swap Out in OS' section for a detailed explanation.

Swap In and Swap Out in OS

Swap In:

The method of removing a process from secondary memory (Hard Drive) and restoring it to the main memory (RAM ) for execution is known as the Swap In method.

Swap Out:

It is a method of bringing out a process from the main memory(RAM) and sending it to the secondary memory(hard drive) so that the processes with higher priority or more memory consumption will be executed known as the Swap Out method.

Note:- Swap In and Swap Out method is done by Medium Term Scheduler(MTS).

Advantages of Swapping in OS

The advantages of the swapping method are listed as follows:

  • Swapping in OS helps in achieving the goal of Maximum CPU Utilization.
  • Swapping ensures proper memory availability for every process that needs to be executed.
  • Swapping helps avoid the problem of process starvation means a process should not take much time for execution so that the next process should be executed.
  • CPU can perform various tasks simultaneously with the help of swapping so that processes do not have to wait much longer before execution.
  • Swapping ensures proper RAM(main memory) utilization.
  • Swapping creates a dedicated disk partition in the hard drive for swapped processes which is called swap space.
  • Swapping in OSis an economical process.
  • Swapping method can be applied on priority-based process scheduling where a high priority process is swapped in and a low priority process is swapped out which improves the performance.

Disadvantages of Swapping in OS

There are some limited disadvantages of the swapping method which are listed as follows:

  • If the system deals with power-cut during bulky swapping activity then the user may lose all the information which is related to the program.
  • If the swapping method uses an algorithm that is not up to the mark then the number of page faults can be increased and therefore this decreases the complete performance.
  • There may be inefficiency in a case when there is some common resource used by the processes that are participating in the swapping process.

Example:

Let's understand the concept of swapping with an example:

Suppose we have a user whose process size is 4096KB. Here the user is having a standard hard disk drive in which the swapping has a transfer rate of 4Mbps. Now we will compute how long it takes to transfer the data from the main memory which is RAM to the secondary memory which is the hard disk.

Now, taking both swap in and swap out time into account the total time taken for transferring the data from main memory to secondary memory is equal to 2000 milliseconds which is 2 Seconds.

Conclusion

  • Swapping in OS is a memory management method that temporarily swaps out idle or blocked processes from main memory to secondary memory which ensures proper memory utilization.
  • There are two important concepts of swapping:
    1. Swap In
    2. Swap Out
  • Swap In is a method used to remove the process from secondary memory and restore it to primary memory meanwhile Swap Out is a method used to remove the process from main memory and send it to secondary memory.
  • One of the main advantages of the swapping technique is that it provides proper RAM utilization and ensures memory availability for every process.
  • One of the main disadvantages of the swapping technique is that the algorithm used for swapping must be good enough otherwise it decreases the overall performance.