The Wayback Machine - https://web.archive.org/web/20240812155905/https://www.geeksforgeeks.org/swapping-in-operating-system/
Open In App

Swapping in Operating System

Last Updated : 29 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

To increase CPU utilization in multiprogramming, a memory management scheme known as swapping can be used. Swapping is the process of bringing a process into memory and then temporarily copying it to the disc after it has run for a while. The purpose of swapping in an operating system is to access data on a hard disc and move it to RAM so that application programs can use it.

What is Swapping in the Operating System?

Swapping in an operating system is a process that moves data or programs between the computer’s main memory (RAM) and a secondary storage (usually a hard disk or SSD). This helps manage the limited space in RAM and allows the system to run more programs than it could otherwise handle simultaneously.

It’s important to remember that swapping is only used when data isn’t available in RAM. Although the swapping process degrades system performance, it allows larger and multiple processes to run concurrently. Because of this, swapping is also known as memory compaction. The CPU scheduler determines which processes are swapped in and which are swapped out. Consider a multiprogramming environment that employs a priority-based scheduling algorithm. When a high-priority process enters the input queue, a low-priority process is swapped out so the high-priority process can be loaded and executed. When this process terminates, the low-priority process is swapped back into memory to continue its execution. The below figure shows the swapping process in the operating system:

Swapping in Operating System

Swapping has been subdivided into two concepts: swap-in and swap-out.

  • Swap-out is a technique for moving a process from RAM to the hard disc.
  • Swap-in is a method of transferring a program from a hard disc to main memory, or RAM.

Process of Swapping

  • When the RAM is full and a new program needs to run, the operating system selects a program or data that is currently in RAM but not actively being used.
  • The selected data is moved to the secondary storage, making space in RAM for the new program.
  • When the swapped-out program is needed again, it can be swapped back into RAM, replacing another inactive program or data if necessary.

Real Life Example of Swapping

Imagine you have a disk (RAM) that is too small to hold all your books and papers (programs). You keep the most important items on the desk and store the rest in a cabinet (secondary storage). When you need something from the cabinet, you swap it with something on your desk. This way, you can work with more items than your desk alone could hold.

Advantages

  • If there is low main memory so some processes may has to wait for much long but by using swapping process do not have to wait long for execution on CPU.
  • It utilize the main memory.
  • Using only single main memory, multiple process can be run by CPU using swap partition.
  • The concept of virtual memory start from here and it utilize it in better way.
  • This concept can be useful in priority based scheduling to optimize the swapping process.

Disadvantages

  • If there is low main memory resource and user is executing too many processes and suddenly the power of system goes off there might be a scenario where data get erase of the processes which are took parts in swapping.
  • Chances of number of page faults occur
  • Low processing performance 

Only one process occupies the user program area of memory in a single tasking operating system and remains in memory until the process is completed.
When all of the active processes in a multitasking operating system cannot coordinate in main memory, a process is swapped out of main memory so that other processes can enter it.

For more information, you can refer Difference between Paging and Swapping in OS, and the Difference between Swapping and Context Switching. 

Conclusion

Swapping in an operating system is a technique that moves data between RAM and secondary storage to manage limited memory efficiently. It allows the system to run more programs simultaneously by temporarily moving inactive data out of RAM. This process helps improve system performance and ensures that the available memory is used effectively.

Frequently Asked Questions on Swapping – FAQs

Why is swapping used in operating systems?

Swapping is used to manage limited RAM effectively, allowing the system to run more programs simultaneously and improving overall system performance.

How does swapping work?

When RAM is full, the operating system selects inactive data or programs and moves them to secondary storage. This frees up RAM for new or active programs. When the swapped-out data is needed again, it is moved back to RAM.

What is “swapping in” and “swapping out”?

“Swapping in” refers to the process of moving data from secondary storage back into RAM, while “swapping out” refers to moving data from RAM to secondary storage.



Similar Reads

Lazy Swapping in Operating System
Pre-requisites: Swapping in Operating System Lazy swapping, also known as demand paging, is a memory management technique used in operating systems to allow programs to access more memory than is physically available in the system. Increasing the degree of multi-programming of the system means increasing the number of processes loaded into the RAM
2 min read
System Protection in Operating System
Introduction: System protection in an operating system refers to the mechanisms implemented by the operating system to ensure the security and integrity of the system. System protection involves various techniques to prevent unauthorized access, misuse, or modification of the operating system and its resources. There are several ways in which an op
3 min read
User View Vs Hardware View Vs System View of Operating System
User View of Operating System: The Operating System is an interface, hides the details which must be performed and present a virtual machine to the user that makes it easier to use. Operating System provides the following services to the user. Execution of a programAccess to I/O devicesControlled access to filesError detection (Hardware failures, a
1 min read
Xv6 Operating System -adding a new system call
Prerequisite - Xv6 Operating System -add a user program In last post we got to know how to add user program in Xv6 Operating System. Now here you will see how to add new system call in Xv6 Operating System. A dding new system call to xv6: A system call is way for programs to interact with operating system. A computer program makes system call when
6 min read
Traps and System Calls in Operating System (OS)
Traps and system calls are two mechanisms used by an operating system (OS) to perform privileged operations and interact with user-level programs. Here is an overview of each mechanism: Traps: A trap is an interrupt generated by the CPU when a user-level program attempts to execute a privileged instruction or encounters an error. When a trap occurs
3 min read
Difference between System Software and Operating System
1. System Software: System software is a type of computer program that is designed to run a computer's hardware and application programs it controls a computer's internal functioning, chiefly through an operating system. It also controls peripherals devices such as monitors, printers, and storage devices. 2. Operating System: An operating system or
3 min read
Fork System Call in Operating System
In many operating systems, the fork system call is an essential operation. The fork system call allows the creation of a new process. When a process calls the fork(), it duplicates itself, resulting in two processes running at the same time. The new process that is created is called a child process. It is a copy of the parent process. The fork syst
5 min read
System Programs in Operating System
System Programming can be defined as the act of building Systems Software using System Programming Languages. According to Computer Hierarchy, Hardware comes first then is Operating System, System Programs, and finally Application Programs. Program Development and Execution can be done conveniently in System Programs. Some of the System Programs ar
3 min read
File System Implementation in Operating System
A file is a collection of related information. The file system resides on secondary storage and provides efficient and convenient access to the disk by allowing data to be stored, located, and retrieved. File system implementation in an operating system refers to how the file system manages the storage and retrieval of data on a physical storage de
11 min read
Multiple-Processor Scheduling in Operating System
In multiple-processor scheduling multiple CPU's are available and hence Load Sharing becomes possible. However multiple processor scheduling is more complex as compared to single processor scheduling. In multiple processor scheduling there are cases when the processors are identical i.e. HOMOGENEOUS, in terms of their functionality, we can use any
7 min read
three90RightbarBannerImg