WikiGalaxy

Personalize

Paging in Virtual Memory

Introduction to Paging:

  • Paging is a memory management scheme that eliminates the need for contiguous allocation of physical memory.
  • It divides the virtual memory into blocks of physical memory called "pages" and "frames".
  • Paging helps in efficient and effective utilization of memory resources.

Page Table:

  • A page table is a data structure used to map virtual addresses to physical addresses.
  • Each process has its own page table, which keeps track of where its pages are stored in memory.
  • Page tables help in translating logical addresses to physical addresses.

Page Faults:

  • A page fault occurs when a program tries to access a page that is not currently mapped to physical memory.
  • Handling a page fault involves loading the required page from disk into memory.
  • Frequent page faults can lead to performance degradation, known as "thrashing".

Demand Paging:

  • Demand paging is a strategy where pages are loaded into memory only when they are needed.
  • This reduces the overall memory usage and improves system performance.
  • It works by initially loading only those pages that are necessary for execution.

Swapping:

  • Swapping is a technique where processes are temporarily moved out of main memory to a backing store (disk) to free up memory.
  • This allows for the execution of larger programs or multiple programs simultaneously.
  • Swapping can be costly in terms of time due to the slow speed of disk I/O compared to RAM.

Example: Basic Paging Mechanism

Basic Paging Mechanism:

In this example, we demonstrate a simple paging mechanism where a logical address is translated to a physical address using a page table.


public class PagingExample {
    public static void main(String[] args) {
        int[] pageTable = {5, 3, 1, 2, 4, 0}; // Page table mapping
        int logicalAddress = 3; // Logical address to translate
        int pageSize = 1024; // Assume each page is 1024 bytes

        int pageNumber = logicalAddress / pageSize;
        int offset = logicalAddress % pageSize;
        int frameNumber = pageTable[pageNumber];
        int physicalAddress = frameNumber * pageSize + offset;

        System.out.println("Logical Address: " + logicalAddress);
        System.out.println("Physical Address: " + physicalAddress);
    }
}
    

Explanation:

In this code, a logical address is divided into a page number and an offset. The page number is used to index into the page table, which provides the frame number. The physical address is calculated by combining the frame number with the offset.

Console Output:

Logical Address: 3

Physical Address: 3075

Example: Handling Page Faults

Handling Page Faults:

This example illustrates how a system might handle a page fault by loading a page from disk into memory.


import java.util.HashMap;
import java.util.Map;

public class PageFaultHandler {
    private Map pageTable = new HashMap<>();
    private int[] memory = new int[5]; // Simulated physical memory

    public void accessPage(int logicalPage) {
        if (!pageTable.containsKey(logicalPage)) {
            handlePageFault(logicalPage);
        }
        System.out.println("Accessed page: " + logicalPage);
    }

    private void handlePageFault(int logicalPage) {
        System.out.println("Page fault occurred for page: " + logicalPage);
        int frame = findFreeFrame();
        pageTable.put(logicalPage, frame);
        loadPageIntoMemory(logicalPage, frame);
    }

    private int findFreeFrame() {
        // Simulate finding a free frame
        return (int) (Math.random() * 5);
    }

    private void loadPageIntoMemory(int logicalPage, int frame) {
        memory[frame] = logicalPage; // Load page into memory
        System.out.println("Loaded page " + logicalPage + " into frame " + frame);
    }

    public static void main(String[] args) {
        PageFaultHandler handler = new PageFaultHandler();
        handler.accessPage(2);
        handler.accessPage(4);
        handler.accessPage(2); // No page fault
    }
}
    

Explanation:

The code simulates a page fault handler that checks if a page is in memory. If not, it handles the page fault by finding a free frame and loading the page into memory.

Console Output:

Page fault occurred for page: 2

Loaded page 2 into frame 3

Accessed page: 2

Page fault occurred for page: 4

Loaded page 4 into frame 1

Accessed page: 4

Accessed page: 2

Example: Demand Paging

Demand Paging:

In this example, demand paging is demonstrated where pages are loaded into memory only when accessed.


import java.util.HashMap;
import java.util.Map;

public class DemandPaging {
    private Map pageTable = new HashMap<>();
    private int[] memory = new int[5]; // Simulated physical memory

    public void accessPage(int logicalPage) {
        if (!pageTable.containsKey(logicalPage)) {
            loadPage(logicalPage);
        }
        System.out.println("Accessed page: " + logicalPage);
    }

    private void loadPage(int logicalPage) {
        System.out.println("Loading page: " + logicalPage);
        int frame = findFreeFrame();
        pageTable.put(logicalPage, frame);
        memory[frame] = logicalPage; // Load page into memory
    }

    private int findFreeFrame() {
        // Simulate finding a free frame
        return (int) (Math.random() * 5);
    }

    public static void main(String[] args) {
        DemandPaging demandPaging = new DemandPaging();
        demandPaging.accessPage(1);
        demandPaging.accessPage(3);
        demandPaging.accessPage(1); // Page already loaded
    }
}
    

Explanation:

This code simulates demand paging by loading pages into memory only when they are accessed. It checks if a page is already in memory before loading it.

Console Output:

Loading page: 1

Accessed page: 1

Loading page: 3

Accessed page: 3

Accessed page: 1

Example: Swapping Mechanism

Swapping Mechanism:

This example demonstrates a simple swapping mechanism where processes are swapped in and out of memory.


import java.util.LinkedList;
import java.util.Queue;

public class SwappingExample {
    private Queue memory = new LinkedList<>();
    private Queue disk = new LinkedList<>();

    public void swapIn(int process) {
        if (memory.size() >= 3) {
            int swappedOut = memory.poll();
            disk.add(swappedOut);
            System.out.println("Swapped out process: " + swappedOut);
        }
        memory.add(process);
        System.out.println("Swapped in process: " + process);
    }

    public void swapOut() {
        if (!memory.isEmpty()) {
            int process = memory.poll();
            disk.add(process);
            System.out.println("Swapped out process: " + process);
        }
    }

    public static void main(String[] args) {
        SwappingExample swapping = new SwappingExample();
        swapping.swapIn(1);
        swapping.swapIn(2);
        swapping.swapIn(3);
        swapping.swapIn(4); // Triggers swap out
        swapping.swapOut();
    }
}
    

Explanation:

The swapping mechanism is simulated by maintaining two queues: one for memory and one for disk. When memory is full, a process is swapped out to disk to make room for a new process.

Console Output:

Swapped in process: 1

Swapped in process: 2

Swapped in process: 3

Swapped out process: 1

Swapped in process: 4

Swapped out process: 2

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025