Swapping in Operating System
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 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.
Free Space Management in Operating System
Free Space Management Techniques
Linked Allocation: In this technique, each file is represented by a linked list of disk blocks.
When a file is created, the operating system finds enough free space on the disk and links the
blocks of the file to form a chain. This method is simple to implement but can lead to
fragmentation and waste of space.
Contiguous Allocation: In this technique, each file is stored as a contiguous block of disk
space. When a file is created, the operating system finds a contiguous block of free space
and assigns it to the file. This method is efficient as it minimizes fragmentation but suffers
from the problem of external fragmentation.
Indexed Allocation: In this technique, a separate index block is used to store the addresses
of all the disk blocks that make up a file. When a file is created, the operating system creates
an index block and stores the addresses of all the blocks in the file. This method is efficient
in terms of storage space and minimizes fragmentation.
File Allocation Table (FAT): In this technique, the operating system uses a file allocation
table to keep track of the location of each file on the disk. When a file is created, the
operating system updates the file allocation table with the address of the disk blocks that
make up the file. This method is widely used in Microsoft Windows operating systems.
Volume Shadow Copy: This is a technology used in Microsoft Windows operating systems
to create backup copies of files or entire volumes. When a file is modified, the operating
system creates a shadow copy of the file and stores it in a separate location. This method is
useful for data recovery and protection against accidental file deletion.
Overall, free space management is a crucial function of operating systems, as it ensures that storage
devices are utilized efficiently and effectively.
The system keeps tracks of the free disk blocks for allocating space to files when they are created.
Also, to reuse the space released from deleting the files, free space management becomes crucial.
The system maintains a free space list which keeps track of the disk blocks that are not allocated to
some file or directory. The free space list can be implemented mainly as:
1. Bitmap or Bit vector
A Bitmap or Bit Vector is series or collection of bits where each bit corresponds to a disk block. The
bit can take two values: 0 and 1: 0 indicates that the block is free and 1 indicates an allocated block.
The given instance of disk blocks on the disk in Figure 1 (where green blocks are allocated) can be
represented by a bitmap of 16 bits as: 1111000111111001.
Advantages:
Simple to understand.
Finding the first free block is efficient. It requires scanning the words (a group of 8 bits) in a
bitmap for a non-zero word. (A 0-valued word has all bits 0). The first free block is then
found by scanning for the first 1 bit in the non-zero word.
Disadvantages:
For finding a free block, Operating System needs to iterate all the blocks which is time
consuming.
The efficiency of this method reduces as the disk size increases.
2. Linked List
In this approach, the free disk blocks are linked together i.e. a free block contains a pointer to the
next free block. The block number of the very first disk block is stored at a separate location on disk
and is also cached in memory.
In Figure-2, the free space list head points to Block 5 which points to Block 6, the next free block
and so on. The last free block would contain a null pointer indicating the end of free list. A
drawback of this method is the I/O required for free space list traversal.
Advantages:
The total available space is used efficiently using this method.
Dynamic allocation in Linked List is easy, thus can add the space as per the requirement
dynamically.
Disadvantages:
When the size of Linked List increases, the headache of miniating pointers is also increases.
This method is not efficient during iteration of each block of memory.