Simulation of Paged Memory System

Nitin Handa, RK3701B41 B. Tech. CSE Lovely Professional University
---------------------------------------------------------------------------------------------------------------------------------------Abstract— To run a program it must be brought into memory. While the size of memory has been increasing steadily, and modern CPUs now use long address fields capable of generating very large address spaces, the amount of memory available and actually installed in most computers remains a limiting factor. Virtual memory addresses the problems of sharing limited memory through architectural and OS software mechanisms that hide the presence of physical memory and present instead an abstract view of main memory to all applications. This paper describes the paged memory system for virtual memory and describes the concept- how virtual address generated by CPU is mapped to physical address as seen by main memory.
Index Terms— MMU, Logical Address, Virtual Address, Physical Address, Paging, Page Table, Page Offset, Frame number

Logical Vs Physical address space An address generated by the CPU is commonly referred to as a logical address, whereas an address as seen by the memory unit is commonly referred to as a physical address. The compile time and load-time address binding schemes result in an environment where the logical and physical addresses are the same. However, the execution-time address binding scheme results in an environment where the logical and physical addresses differ. The set of all logical addresses generated by a program is referred to as a logical address space; the set of all physical addresses corresponding to these logical addresses is referred to as physical address space. The run-time mapping from virtual to physical address is done by the memory-management unit (MMU), which is a hardware device.



Main memory is a large array of words or bytes. Each word or byte has its own address. For a program to be executed, it must be mapped to absolute addresses and loaded into memory. Usually a program resides on a disk as a binary executable file. The program must be brought into memory and placed in a process for it to be executed. Addresses in the source program are generally symbolic (such as count). A compiler will typically bind these symbolic addresses to re locatable addresses. The linkage editor or loader will in turn bind these re locatable addresses to absolute addresses (such as 74014). Each binding is a mapping from one address space to another. Classically, the binding of instructions and data to memory addresses can be done at any step along the way:  Compile time: If it is known at compile time where the process will reside in memory, then absolute can be generated.  Load time: if it is known at compile time where the process will reside in the memory, the compiler must generate re locatable code. In this case, final binding is delayed until load time.  Execution time/Run time: If the process can be moved during execution from one memory segment to another, then binding must be delayed until runtime. Special hardware must be available for this scheme to work.

Relocation Register 14000 Logical address 346 Physical address






Dynamic relocation using relocation register

Paging System In Paged Memory System:    Physical memory is broken into fixed sized blocks called frames. Logical memory is broken into blocks of the same size called pages. The backing store is divided into fixed-sized blocks that are of the same size as the memory frames.

The user program views the memory as one single contiguous space containing only this one program. In fact,

The size of page is typically a power of 2.the user program is scattered throughout the physical memory. and Va0 the lowest order bit. which also holds other programs. For our simple example above. For this example. we need 6 bits total for our virtual address (26 = 64). of pages × page size 2m = 64 × 1024 2m = 26 × 210 So number of bits required: 2m =216 »» m=16 bit Page Table To record where each virtual page of the operating system is placed in physical memory. Each page of the process needs one frame. This base address is combined with the page offset to obtain the physical memory address that is sent to the memory unit. Thus if the process requires n pages. Page Table Consider 64 byte virtual address space with page size of 16 bytes. 4 bits in this case. Converting “21” into binary form. A page table is maintained which stores the frame no: corresponding to each page. mapped onto a physical memory of 32 frames Size of logical address space (2m) = no. Every address generated by the CPU is divided into 2 parts:  Page Number (p)  Page Offset (d) The page no: is used to index into the page table. we have a 2-bit virtual page number (VPN). the page table would thus have the following entries: . our virtual address: CPU p d d f d d p ______ ______ ______ f_____ ______ Physical memory Where Va5 is the highest-order bit of the virtual address. The page table contains the base address of each page in physical memory. the OS and hardware must combine to translate this virtual address into a meaningful physical address. The major role of the page table is to store address translations for each . there must be at least n frames available in memory. varying between 512 bytes and 16 MB per page Page Number Page Offset P m-n d n The page size is 16 bytes in a 64-byte address space. If there are n frames available in the memory. When a process generates a virtual address. and thus we can examine this virtual address and see how it breaks down into a virtual page number (VPN) and offset: For given logical address space 2m (m bits) and page size 2n. Thus. we call this the offset. and the offset within the page. they are allocated in this arriving process. thus we need to be able to select 4 pages. we get “010101”. Thus. and the top 2 bits of the address do just that. Logical address Physical address of the virtual pages of the address space. the operating system keeps a per-process data structure known as a page table. thus letting us know where in physical memory they live. we have to first split it into two components: the virtual page number (VPN). its size expressed in pages. we can further divide the virtual address as follows: Page table PAGING HARDWARE The page size is defined by the hardware. To translate this virtual address that the process generated. Because we know the page size (16 bytes). Consider virtual address 21. Consider a logical address space of 64 pages of 1024 words each. because the virtual address space of the process is 64 bytes. When a process arrives in the system to be executed. The remaining bits tell us which byte of the page we are interested in. is examined.

In the page table above the physical frame number or PFN is 7 (binary 111). &msize). getch(). pos).. scanf("%d". } printf("\n-------------Page table--------------\n"). printf("\nEnter the page table Entries\n").i++) { printf("Page %d: ". we can now index our page table and find which physical page that virtual page 1 resides within. printf("\nPage number: %d". For extracting Page Offset from logical address we take modulus with page size. main() The following is the implementation of main function: . With our virtual page number. (m+(frameno*size))).i<cnt. The Simulation Program The Simulation characteristics:        program. printf("Enter the page size (in bytes): ").i. we can translate this virtual address by replacing the VPN with the PFN and then issue the load to physical memory. pos. cnt=(msize/size). void main() { int pagetab[100]. msize.i). pos=(logadd/size). The process is described in following figure: The offset stays the same (i. int size. printf("\nVirtual Page Number\tPhysical Frame number").pagetab[i]). &size). printf("\nFrame number: %d\n".i<cnt. &pagetab[i]). printf("\nEnter the memory size (in bytes): ").i++) { printf("\n %d\t\t\t %d". for(i=0.e. the virtual address “21” is on the 5th (“0101”th) byte of virtual page “01” (or 1). developed. scanf("%d". //compute physical address and display printf("\nThe corresponding physical address is %d". because the offset just tells us which byte within the page we want. scanf("%d".i. scanf("%d". it is not translated). cnt. and is exactly where we want our load to fetch data from.j. m=0. &logadd). //enter page table entries for(i=0. It prompts user to enter page table entries. Thus. //number of pages printf("\nThe number of pages is %d". It displays all the page table entries entered by the user in table format. Finally it displays the logical address. we divide the logical address with page size. has following It has flexibility to get size of logical address space and page size from user input.cnt). For extracting Virtual Page number from logical address entered by user. logadd. } printf("\nEnter the logical address: "). It displays the corresponding page number and frame number calculated from logical address entered by the user. Using those page table entries the logical address is mapped to calculate physical address. frameno). } A Runtime Instance An output instance of simulation of some test cases is as following: Enter the memory size (in bytes): 64 Enter the page size (in bytes): 16 The number of pages is 4 Enter the page table entries Page 0: 3 Page 1: 7 Page 2: 5 Page 3: 2 -------------Page table--------------- The simulation program is developed in only one function i.Thus. Our final physical address is 1110101 (117 in decimal). then it calculates the number of pages according to that. frameno=pagetab[pos]. int frameno. m=logadd%size.e.

b. Arpaci-Dusseau and Andrea C. Operating Systems: Three Easy Pieces. Peter Baer Galvin. HELPFUL HINTS a. . MATH All calculation has been shared in the Body text. Inc. UNITS No units to share on this Publication. Operating System Page Number Physical Page Number 0 3 1 7 2 5 3 2 //Test Case 1: Enter the logical address: 21 Page number: 1 Frame number: 7 The 117 corresponding physical address is Test Case 2: for the same page table Enter the logical address: 46 Page number: 2 Frame number: 5 The corresponding physical address is 94 II. Figures and Tables All figures and tables have already been shown in the Body Text. John Wiley & Sons. Equations All the algorithmic calculations have already been used in the Body Text. Avi Silberschatz. IV.comp. REFERENCES 1.wikipedia. Arpaci-Dusseau http://en. 3. 4.htm http://williams. Remzi H.ncat. III.

Sign up to vote on this title
UsefulNot useful