Simulation of Address Translation in Paging Scheme

 Simulation of Address Translation in Paging Scheme


Aim

To simulate the address translation mechanism in the paging memory management scheme by converting a given virtual address into its corresponding physical address using a page table.


Problem Statement

Write a program to simulate paging-based address translation with the following inputs provided as command-line arguments:

  1. Size of the virtual address space (in megabytes)

  2. Page size (in kilobytes)

  3. Virtual address (in decimal notation)

The program should output the physical address in the format:

<frame number, offset>

Assume that:

  • The page table is implemented as an array indexed by page number

  • If the page number is not present in the page table, declare a page table miss


Description / Theory

Paging is a memory management technique in which:

  • Virtual memory is divided into fixed-size blocks called pages

  • Physical memory is divided into fixed-size blocks called frames

Each process maintains a page table that maps page numbers to frame numbers.

A virtual address consists of:

  • Page Number (p) – used as an index into the page table

  • Offset (d) – position within the page

Address Translation Formula:

Page Number = Virtual Address / Page Size Offset = Virtual Address % Page Size

The physical address is represented as:

<Frame Number, Offset>

If the page number does not exist in the page table, a page table miss occurs.


Algorithm

  1. Read the command-line arguments:

    • Virtual address space size (MB)

    • Page size (KB)

    • Virtual address

  2. Convert:

    • Virtual address space → bytes

    • Page size → bytes

  3. Compute the number of pages.

  4. Create a page table with identity mapping:

    page i → frame i
  5. Validate the virtual address.

  6. Extract:

    • Page number

    • Offset

  7. If the page number is valid:

    • Display physical address as <frame number, offset>

  8. Else:

    • Display Page Table Miss


Flowchart

Start | Read command-line inputs | Convert sizes to bytes | Validate virtual address | Calculate page number and offset | Is page number < number of pages? | | Yes No | | Print <frame,offset> Page Table Miss | End

Program (C Implementation)

#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { int vspace_mb, page_kb; unsigned int virtual_address; unsigned int virtual_space_bytes; unsigned int page_size_bytes; unsigned int num_pages; unsigned int page_number, offset; unsigned int *page_table; if (argc != 4) { printf("Usage: %s <VirtualSpace_MB> <PageSize_KB> <VirtualAddress>\n", argv[0]); return 1; } vspace_mb = atoi(argv[1]); page_kb = atoi(argv[2]); virtual_address = atoi(argv[3]); virtual_space_bytes = vspace_mb * 1024 * 1024; page_size_bytes = page_kb * 1024; if (virtual_address >= virtual_space_bytes) { printf("Invalid Virtual Address\n"); return 1; } num_pages = virtual_space_bytes / page_size_bytes; page_table = (unsigned int *)malloc(num_pages * sizeof(unsigned int)); for (unsigned int i = 0; i < num_pages; i++) page_table[i] = i; // Identity mapping page_number = virtual_address / page_size_bytes; offset = virtual_address % page_size_bytes; if (page_number >= num_pages) { printf("Page Table Miss!\n"); } else { printf("Physical Address = <%u, %u>\n", page_table[page_number], offset); } free(page_table); return 0; }

Sample Input

./paging 16 4 12345

Input Explanation:

  • Virtual Address Space = 16 MB

  • Page Size = 4 KB

  • Virtual Address = 12345


Sample Output

Physical Address = <3, 57>

Result

Thus, the given virtual address was successfully translated into its corresponding physical address using paging. The simulation demonstrates the working of a page table–based address translation mechanism.

Comments

Popular posts from this blog

Operating Systems OS Lab PCCSL407 Semester 4 KTU BTech CS 2024 Scheme - Dr Binu V P

Exploring the /proc file system

ps command