Introduction-
Demand paging is sometimes called “Lazy Loading” or it is basically a memory management scheme implemented in OS(Operating System) including Linux, to optimize the use of physical memory. It is a technique that allows the operating system to load pages into memory only when they are demanded by programs. This approach helps in efficient memory utilization and enhances overall system performance.
Basics of Paging-
Paging is a memory management scheme that eliminates the need for contiguous allocation of physical memory. In a paged memory system, the virtual address space of a process is divided into fixed-size blocks known as pages. Similarly, physical memory is divided into blocks of the same size called frames.
The operating system maintains a page table that maps virtual pages to physical frames. This mapping allows the system to translate virtual addresses into physical addresses, enabling efficient memory access.
Understanding Demand Paging-
Demand paging takes the idea of paging a step further by loading pages into memory only when they are needed. In a demand-paged system, not all pages of a process are loaded into physical memory during its startup. Instead, pages are brought into memory only when a program references data or instructions located on those pages.
This on-demand loading of pages provides several advantages:
- Reduced Initial Loading Time: Since only essential pages are loaded initially, the operating system can start a program more quickly.
- Efficient Memory Utilization: Demand paging allows the system to use physical memory more efficiently. Only the portions of a program that are actively in use are loaded into memory.
- Optimized I/O Operations: By loading pages only when they are needed, demand paging reduces unnecessary I/O operations, leading to better I/O performance.
How Demand Paging Works in Linux-
In Linux, demand paging is implemented as part of the virtual memory subsystem. When a program is started, the kernel loads only a small portion of its code and data into physical memory. As the program executes, additional pages are brought into memory on demand.
Here’s a simplified overview of the demand paging process in Linux:
- Page Fault Handling: When a program accesses a page that is not currently in physical memory, a page fault occurs. The Linux kernel responds to this page fault by identifying the missing page and initiating a page-in operation.
- Page-in Operation: The kernel retrieves the required page from secondary storage (usually the swap space on disk) and loads it into an available physical frame.
- Updating Page Tables: The page table is updated to reflect the mapping between the virtual page and the newly allocated physical frame.
- Resuming Execution: With the required page now in memory, the program can resume its execution seamlessly.
Challenges and Considerations-
While demand paging offers several advantages, it is not without challenges. One critical consideration is the potential for page thrashing — a situation where the system spends more time swapping pages in and out of memory than executing actual instructions. This can lead to a significant degradation in performance.
To mitigate the risk of thrashing, Linux employs various strategies, including the use of algorithms to determine which pages to keep in memory (page replacement algorithms) and mechanisms to prioritize critical pages.
Conclusion-
Demand paging is a crucial aspect of modern operating systems like Linux, contributing to efficient memory utilization and improve the system performance. By loading pages into memory only when they are needed, demand paging strikes a balance between minimizing startup time and optimizing overall memory usage. Understanding how demand paging works provides insights into the intricate mechanisms that make operating systems efficient and responsive to the dynamic demands of running applications.
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
Here is the step by step procedure of writing and compiling basic Hello World Linux kernel module given below:
STEP-1: Creating the Kernel module
Creage a new C file named ‘hello.c; and add the following code-
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE(“GPL”);
MODULE_AUTHOR(“Your Name”);
MODULE_DESCRIPTION(“A simple Linux kernel module”);
static int __init hello_init(void) {
printk(KERN_INFO “Hello, World!\n”);
return 0;
}
static void __exit hello_exit(void) {
printk(KERN_INFO “Goodbye, World!\n”);
}
module_init(hello_init);
module_exit(hello_exit);
STEP-2: Creating the Makefile
Create a file named ‘Makefile’ with the following content:
obj-m += hello.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
Load the module into the kernel using the insmod
command:
sudo insmod hello.ko
dmesg | tail
You should see “Hello, World!” in the kernel log output.
sudo rmmod hello
If you want to run this in QEMU, you need to set up a kernel and a minimal root filesystem. Here is a basic example:
# Download a Linux kernel and a minimal root filesystem
wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.15.10.tar.xz
tar -xf linux-5.15.10.tar.xz
git clone https://github.com/ivandavidov/minimal
cd minimal
./debootstrap.sh
cd ..
# Build QEMU command
qemu-system-x86_64 -kernel linux-5.15.10/arch/x86/boot/bzImage -initrd minimal/rootfs.cpio -nographic -append “console=ttyS0”
# Copy your kernel module into the virtual machine
scp hello.ko root@localhost:/root
Inside the QEMU terminal:
# Load the module
insmod /root/hello.ko
# Check kernel log
dmesg | tail