Module IV Memory Organization Paging Paging method breaks
Module IV Memory Organization
Paging • Paging method – breaks physical memory into fixed-sized blocks called frames and – breaks logical memory into blocks of same size called pages • Paging avoids external fragmentation • Disadv: Internal Fragmentation • When a process is to be executed, its pages are loaded into available memory frames from their source.
Paging Hardware • Every address by CPU is divided into two parts: page number (p) & page offset (d). • Page number is an index to page table • Page table contains base address of the page • This base address is combined with page offset to get physical address
Paging Hardware
Paging Model
Demand Paging • To load an executable program from disk into memory • Option 1: – load entire program at execution time. – But for a program with options, it is inefficient • Option 2: – load pages only as they are needed and is known as demand paging
Demand Paging • A pager swaps a page into memory when a page is needed • Pager find whether the page is in memory or disk by valid–invalid bit. • When this bit is set to “valid, ” the page is legal and is in memory. • If it is set to “invalid, ” the page either is – not valid ( not in logical address space of process) or – is valid but is in the disk.
Demand Paging • Page-table entry is brought into memory for each process • Page-table entry for a page that is not currently in memory is either – marked invalid or – contains the address of the page on disk. • Access to a page that is not in memory causes page fault invoking a trap to OS.
Demand Paging • Paging allows multiprogramming • It lead to over-allocation of memory • When no free frames are available, a page replacement algorithm is applied
Page Fault Service Routine 1. Find the location of the desired page on the disk. 2. Find a free frame: a. If there is a free frame, use it. b. If there is no free frame, use a page-replacement algorithm to select a victim frame. c. Write victim frame to disk & update page & frame table. 3. Read the desired page into the newly freed frame; change the page and frame tables. 4. Restart the user process.
Basic Page Replacement
Page Replacement Algorithms • Many algorithms are available • One with the lowest page-fault rate is considered as best. • They are: – FIFO Page Replacement – Optimal Page Replacement – LRU Page Replacement. – LFU Page Replacement
FIFO Page Replacement • It associates each page with the time it was brought into memory. • To replace a page, the oldest one is chosen. • Implementation – create a FIFO queue to hold all pages. – replace the page at the head of the queue – Insert the page at the tail of the queue.
FIFO Page Replacement • Example • Consider the reference string : 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1 • for a memory with three frames.
7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0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1 Page Frame Hit -1 -1 -1 7 7 -1 -1 0 7 0 1 7 2 Page Frame Hit 3 0 2 3 1 -1 0 1 3 0 0 1 0 2 0 1 0 0 0 1 2 1 0 2 0 1 1 1 0 1 2 1 3 2 3 1 0 7 7 1 2 0 0 2 3 0 0 0 7 0 2 0 4 4 3 0 0 2 4 2 0 0 3 4 2 3 0 0 0 2 3 0
7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1 Page Frame Hit -1 -1 -1 7 7 -1 -1 0 7 0 1 7 2 Page Frame Hit 3 0 2 3 1 -1 0 1 3 0 0 1 0 2 0 1 0 0 0 1 2 1 0 2 0 1 1 1 0 1 2 1 3 2 3 1 0 7 7 1 2 0 0 2 3 0 0 0 7 0 2 0 4 4 3 0 0 1 7 0 1 0 2 4 2 0 0 3 4 2 3 0 0 0 2 3 0
7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1 Page Frame Hit -1 -1 -1 7 7 -1 -1 0 7 0 1 7 2 Page Frame Hit 3 0 2 3 1 -1 0 1 3 0 0 1 0 2 0 1 0 0 0 1 2 1 0 2 0 1 1 1 0 1 2 1 3 2 3 1 0 7 7 1 2 0 0 2 3 0 0 0 7 0 2 0 4 4 3 0 0 1 7 0 1 0 2 4 2 0 0 3 4 2 3 0 0 0 2 3 0 Hit Ratio = 5/20 No. of Page Faults = 15
FIFO Page Replacement • It is easy to understand program. • Its performance is not always good • If initial page contain an initialization module, it works good • If it contain a heavily used variable in constant use, the performance degrades.
- Slides: 36