Understanding Demand Paging in Linux (2024)

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.

Understanding Demand Paging in Linux (2)

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.

Understanding Demand Paging in Linux (3)

This on-demand loading of pages provides several advantages:

  1. Reduced Initial Loading Time: Since only essential pages are loaded initially, the operating system can start a program more quickly.
  2. 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.
  3. 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.

Understanding Demand Paging in Linux (4)

Here’s a simplified overview of the demand paging process in Linux:

  1. 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.
  2. 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.
  3. Updating Page Tables: The page table is updated to reflect the mapping between the virtual page and the newly allocated physical frame.
  4. 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

Understanding Demand Paging in Linux (2024)
Top Articles
How to avoid punishment for drunk driving? Drink more, apparently
SpaceX moves incorporation to Texas, as Elon Musk continues to blast Delaware
Where To Go After Howling Pit Code Vein
Tattoo Shops Lansing Il
Hannaford Weekly Flyer Manchester Nh
Chicago Neighborhoods: Lincoln Square & Ravenswood - Chicago Moms
Mrh Forum
Jeremy Corbell Twitter
Cumberland Maryland Craigslist
Craigslist Vermillion South Dakota
Chuckwagon racing 101: why it's OK to ask what a wheeler is | CBC News
Embassy Suites Wisconsin Dells
Costco in Hawthorne (14501 Hindry Ave)
Xm Tennis Channel
The Rise of Breckie Hill: How She Became a Social Media Star | Entertainment
Discover Westchester's Top Towns — And What Makes Them So Unique
People Portal Loma Linda
Samsung Galaxy S24 Ultra Negru dual-sim, 256 GB, 12 GB RAM - Telefon mobil la pret avantajos - Abonament - In rate | Digi Romania S.A.
Conan Exiles Colored Crystal
Ms Rabbit 305
Vegito Clothes Xenoverse 2
Stihl Dealer Albuquerque
Https E22 Ultipro Com Login Aspx
Discord Nuker Bot Invite
Nk 1399
They Cloned Tyrone Showtimes Near Showbiz Cinemas - Kingwood
Ihs Hockey Systems
Halsted Bus Tracker
Hoofdletters voor God in de NBV21 - Bijbelblog
6465319333
Everstart Jump Starter Manual Pdf
Babbychula
Makemkv Key April 2023
SOC 100 ONL Syllabus
Priscilla 2023 Showtimes Near Consolidated Theatres Ward With Titan Luxe
Craigslist Jobs Brownsville Tx
Cdcs Rochester
Final Jeopardy July 25 2023
Thelemagick Library - The New Comment to Liber AL vel Legis
Gold Dipping Vat Terraria
Nba Props Covers
Gfs Ordering Online
Lbl A-Z
Trivago Anaheim California
Divinity: Original Sin II - How to Use the Conjurer Class
Celsius Claims Agent
Enr 2100
Go Nutrients Intestinal Edge Reviews
House For Sale On Trulia
Food and Water Safety During Power Outages and Floods
Swissport Timecard
Latest Posts
Article information

Author: Merrill Bechtelar CPA

Last Updated:

Views: 6297

Rating: 5 / 5 (70 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Merrill Bechtelar CPA

Birthday: 1996-05-19

Address: Apt. 114 873 White Lodge, Libbyfurt, CA 93006

Phone: +5983010455207

Job: Legacy Representative

Hobby: Blacksmithing, Urban exploration, Sudoku, Slacklining, Creative writing, Community, Letterboxing

Introduction: My name is Merrill Bechtelar CPA, I am a clean, agreeable, glorious, magnificent, witty, enchanting, comfortable person who loves writing and wants to share my knowledge and understanding with you.