SlideShare a Scribd company logo
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Chapter 9: Virtual Memory
9.2 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Chapter 9: Virtual Memory
 Background
 Demand Paging
 Copy-on-Write
 Page Replacement
 Allocation of Frames
 Thrashing
 Memory-Mapped Files
 Allocating Kernel Memory
 Other Considerations
 Operating-System Examples
9.3 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Objectives
 To describe the benefits of a virtual memory system
 To explain the concepts of demand paging, page-replacement
algorithms, and allocation of page frames
 To discuss the principle of the working-set model
 To examine the relationship between shared memory and
memory-mapped files
 To explore how kernel memory is managed
9.4 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Background
 Code needs to be in memory to execute, but entire program rarely
used
 Error code, unusual routines, large data structures
 Entire program code not needed at same time
 Consider ability to execute partially-loaded program
 Program no longer constrained by limits of physical memory
 Each program takes less memory while running -> more
programs run at the same time
 Increased CPU utilization and throughput with no increase
in response time or turnaround time
 Less I/O needed to load or swap programs into memory ->
each user program runs faster
9.5 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Background (Cont.)
 Virtual memory – separation of user logical memory from
physical memory
 Only part of the program needs to be in memory for execution
 Logical address space can therefore be much larger than physical
address space
 Allows address spaces to be shared by several processes
 Allows for more efficient process creation
 More programs running concurrently
 Less I/O needed to load or swap processes
9.6 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Background (Cont.)
 Virtual address space – logical view of how process is
stored in memory
 Usually start at address 0, contiguous addresses until end of
space
 Meanwhile, physical memory organized in page frames
 MMU must map logical to physical
 Virtual memory can be implemented via:
 Demand paging
 Demand segmentation
9.7 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Virtual Memory That is Larger Than Physical Memory
9.8 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Virtual-address Space
 Usually design logical address space for
stack to start at Max logical address and
grow “down” while heap grows “up”
 Maximizes address space use
 Unused address space between
the two is hole
 No physical memory needed
until heap or stack grows to a
given new page
 Enables sparse address spaces with
holes left for growth, dynamically linked
libraries, etc
 System libraries shared via mapping into
virtual address space
9.9 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Shared Library Using Virtual Memory
9.10 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Demand Paging
 Could bring entire process into memory
at load time
 Or bring a page into memory only
when it is needed
 Less I/O needed, no unnecessary
I/O
 Less memory needed
 Faster response
 More users
 Similar to paging system with swapping
(diagram on right)
 Page is needed  reference to it
 invalid reference  abort
 not-in-memory  bring to memory
 Lazy swapper – never swaps a page
into memory unless page will be needed
 Swapper that deals with pages is a
pager
9.11 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Basic Concepts
 With a process is to be swapped in, pager guesses which pages will
be used before the process is swapped out again
 Instead of swapping in a whole process, the pager brings only those
pages into memory
 How to determine that set of pages?
 Need new MMU functionality to implement demand paging
 The valid–invalid bit scheme can be used for this purpose
 when this bit is set to “valid,” the associated page is both legal
and in memory
 If the bit is set to “invalid,” the page either is not valid (that is, not
in the logical address space of the process) or is valid but is
currently on the disk.
9.12 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Basic Concepts (Cont.,)
 If pages needed are already memory resident
 No difference from non demand-paging
 If page needed and not memory resident
 Access to a page marked invalid causes a page fault
 The paging hardware will notice that the invalid bit is set, causing
a trap to the operating system
9.13 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Basic Concepts (Cont.,)
 The procedure for handling this page fault :
 Check an internal table (usually kept with the process control block) for this process to determine
whether the reference was a valid or an invalid memory access.
 If the reference was invalid, we terminate the process. If it was valid but we have not yet brought in
that page, we now page it in.
 Find a free frame (by taking one from the free-frame list, for example).
 Schedule a disk operation to read the desired page into the newly allocated frame.
 When the disk read is complete, we modify the internal table kept with the process and the page
table to indicate that the page is now in memory.
 We restart the instruction that was interrupted by the trap. The process can now access the page
as though it had always been in memory.
9.14 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Valid-Invalid Bit
 With each page table entry a valid–invalid bit is associated
(v  in-memory – memory resident, i  not-in-memory)
 Initially valid–invalid bit is set to i on all entries
 Example of a page table snapshot:
9.15 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Page Table When Some Pages Are Not in Main Memory
9.16 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Steps in Handling a Page Fault
9.17 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Aspects of Demand Paging
 Extreme case – start process with no pages in memory
 OS sets instruction pointer to first instruction of process, non-
memory-resident -> page fault
 And for every other process pages on first access
 Pure demand paging
 Actually, a given instruction could access multiple pages (one page for
the instruction and many for data) -> may cause multiple page faults
 Consider fetch and decode of instruction which adds 2 numbers
from memory and stores result back to memory
 Hardware support needed for demand paging
 Page table with valid / invalid bit
 Secondary memory (swap device with swap space)
 Instruction restart (next slide)
9.18 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Aspects of Demand Paging (Cont.,)
 Instruction Restart
 A crucial requirement for demand paging is the ability to restart any
instruction after a page fault
 Because we save the state (registers, condition code, instruction
counter) of the interrupted process when the page fault occurs
 we must be able to restart the process in exactly the same place and
state, except that the desired page is now in memory and is accessible
 A page fault may occur at any memory reference.
 If the page fault occurs on the instruction fetch, we can restart by
fetching the instruction again
 If a page fault occurs while we are fetching an operand, we must
fetch and decode the instruction again and then fetch the operand.
9.19 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Aspects of Demand Paging (Cont.,)
 As a worst-case example, consider a three-address instruction such as ADD the content of A to B,
placing the result in C. These are the steps to execute this instruction:
 Fetch and decode the instruction (ADD).
 Fetch A.
 Fetch B.
 Add A and B.
 Store the sum in C
 If we fault when we try to store in C (because C is in a page not currently in memory), we will have to
get the desired page, bring it in, correct the page table, and restart the instruction
 The restart will require fetching the instruction again, decoding it again, fetching the two operands
again, and then adding again.
9.20 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Performance of Demand Paging
 Stages in Demand Paging (worse case)
1. Trap to the operating system
2. Save the user registers and process state
3. Determine that the interrupt was a page fault
4. Check that the page reference was legal and determine the location of the page on the disk
5. Issue a read from the disk to a free frame:
1. Wait in a queue for this device until the read request is serviced
2. Wait for the device seek and/or latency time
3. Begin the transfer of the page to a free frame
6. While waiting, allocate the CPU to some other user
7. Receive an interrupt from the disk I/O subsystem (I/O completed)
8. Save the registers and process state for the other user
9. Determine that the interrupt was from the disk
10. Correct the page table and other tables to show page is now in memory
11. Wait for the CPU to be allocated to this process again
12. Restore the user registers, process state, and new page table, and then resume the
interrupted instruction
9.21 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Performance of Demand Paging (Cont.)
 Three major activities in Demand Paging
 Service the interrupt – careful coding means just several hundred
instructions needed
 Read the page – lots of time
 Restart the process – again just a small amount of time
 Page Fault Rate 0  p  1
 if p = 0 no page faults
 if p = 1, every reference is a fault
 Effective Access Time (EAT)
EAT = (1 – p) x memory access
+ p (page fault overhead
+ swap page out
+ swap page in )
9.22 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Demand Paging Example
 Memory access time = 200 nanoseconds (1s=109ns)
 Average page-fault service time = 8 milliseconds (1s=103ms)
 EAT = (1 – p) x 200 + p (8 milliseconds)
= (1 – p ) x 200 + p x 8,000,000
= 200 + p x 7,999,800
 If no page fault (p=0), then EAT = 200 ns
 If one access out of 1,000 causes a page fault (p=0.001), then
 EAT = 8.2 microseconds (8200ns).(1s=106ms)
 This is a slowdown by a factor of 40!!
 If want performance degradation < 10 percent
 220 > 200 + 7,999,800 x p
20 > 7,999,800 x p
 p < .0000025
 < one page fault in every 400,000 memory accesses

More Related Content

PPT
ch9 memory Management and virtual memory.ppt
PPT
Operating system virtual memory by silberscatz
PPT
ch9VirtualMemory in the operating system.ppt
PDF
بيي
PPT
Operating System chapter 9 (Virtual Memory)
PPT
Memory Management
PPT
Chapter 11 from Operating system by silberschartz
ch9 memory Management and virtual memory.ppt
Operating system virtual memory by silberscatz
ch9VirtualMemory in the operating system.ppt
بيي
Operating System chapter 9 (Virtual Memory)
Memory Management
Chapter 11 from Operating system by silberschartz

Similar to advanced operating systems chapter 9 virtual memory (20)

PDF
W-ch09.pdfklkdldkdkidodonslnsic;;;;;;xkkid
PPT
LecturePPT_Unit_3b_AY2021-22_TechngVrsn.ppt
PPT
ch9 Virtual Memory Manipal UNiversitgy Jaipurt
PPT
Chap09_Virtual Memory File_System_Fundamentals savitchAbsJavaPPT Java Program...
PPT
Virtual memory
PPTX
Chapter 10 Operating Systems silberschatz
PPT
Ch8 of OS
PPT
PPT
PPT
unit 4 virtual memory.ppt
PPT
9.Virtual Memory
PPT
ch8 Memory management and Virtual Memory.ppt
PPT
Unit4-ch8.ppt operating system bankers algorithm
PPT
The Council of Architecture (COA) has been constituted by the Government of I...
PPTX
Main memory operating system
PPT
memory management.ppt
PPT
ch8 (2).ppt
PPTX
OS Memory Management.pptx
W-ch09.pdfklkdldkdkidodonslnsic;;;;;;xkkid
LecturePPT_Unit_3b_AY2021-22_TechngVrsn.ppt
ch9 Virtual Memory Manipal UNiversitgy Jaipurt
Chap09_Virtual Memory File_System_Fundamentals savitchAbsJavaPPT Java Program...
Virtual memory
Chapter 10 Operating Systems silberschatz
Ch8 of OS
unit 4 virtual memory.ppt
9.Virtual Memory
ch8 Memory management and Virtual Memory.ppt
Unit4-ch8.ppt operating system bankers algorithm
The Council of Architecture (COA) has been constituted by the Government of I...
Main memory operating system
memory management.ppt
ch8 (2).ppt
OS Memory Management.pptx
Ad

Recently uploaded (20)

PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
01-Introduction-to-Information-Management.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Pre independence Education in Inndia.pdf
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
GDM (1) (1).pptx small presentation for students
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Pharma ospi slides which help in ospi learning
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
master seminar digital applications in india
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Final Presentation General Medicine 03-08-2024.pptx
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
01-Introduction-to-Information-Management.pdf
Supply Chain Operations Speaking Notes -ICLT Program
Pre independence Education in Inndia.pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Microbial disease of the cardiovascular and lymphatic systems
VCE English Exam - Section C Student Revision Booklet
Module 4: Burden of Disease Tutorial Slides S2 2025
GDM (1) (1).pptx small presentation for students
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Pharma ospi slides which help in ospi learning
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPH.pptx obstetrics and gynecology in nursing
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Abdominal Access Techniques with Prof. Dr. R K Mishra
master seminar digital applications in india
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Ad

advanced operating systems chapter 9 virtual memory

  • 1. Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition Chapter 9: Virtual Memory
  • 2. 9.2 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition Chapter 9: Virtual Memory  Background  Demand Paging  Copy-on-Write  Page Replacement  Allocation of Frames  Thrashing  Memory-Mapped Files  Allocating Kernel Memory  Other Considerations  Operating-System Examples
  • 3. 9.3 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition Objectives  To describe the benefits of a virtual memory system  To explain the concepts of demand paging, page-replacement algorithms, and allocation of page frames  To discuss the principle of the working-set model  To examine the relationship between shared memory and memory-mapped files  To explore how kernel memory is managed
  • 4. 9.4 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition Background  Code needs to be in memory to execute, but entire program rarely used  Error code, unusual routines, large data structures  Entire program code not needed at same time  Consider ability to execute partially-loaded program  Program no longer constrained by limits of physical memory  Each program takes less memory while running -> more programs run at the same time  Increased CPU utilization and throughput with no increase in response time or turnaround time  Less I/O needed to load or swap programs into memory -> each user program runs faster
  • 5. 9.5 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition Background (Cont.)  Virtual memory – separation of user logical memory from physical memory  Only part of the program needs to be in memory for execution  Logical address space can therefore be much larger than physical address space  Allows address spaces to be shared by several processes  Allows for more efficient process creation  More programs running concurrently  Less I/O needed to load or swap processes
  • 6. 9.6 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition Background (Cont.)  Virtual address space – logical view of how process is stored in memory  Usually start at address 0, contiguous addresses until end of space  Meanwhile, physical memory organized in page frames  MMU must map logical to physical  Virtual memory can be implemented via:  Demand paging  Demand segmentation
  • 7. 9.7 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition Virtual Memory That is Larger Than Physical Memory
  • 8. 9.8 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition Virtual-address Space  Usually design logical address space for stack to start at Max logical address and grow “down” while heap grows “up”  Maximizes address space use  Unused address space between the two is hole  No physical memory needed until heap or stack grows to a given new page  Enables sparse address spaces with holes left for growth, dynamically linked libraries, etc  System libraries shared via mapping into virtual address space
  • 9. 9.9 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition Shared Library Using Virtual Memory
  • 10. 9.10 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition Demand Paging  Could bring entire process into memory at load time  Or bring a page into memory only when it is needed  Less I/O needed, no unnecessary I/O  Less memory needed  Faster response  More users  Similar to paging system with swapping (diagram on right)  Page is needed  reference to it  invalid reference  abort  not-in-memory  bring to memory  Lazy swapper – never swaps a page into memory unless page will be needed  Swapper that deals with pages is a pager
  • 11. 9.11 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition Basic Concepts  With a process is to be swapped in, pager guesses which pages will be used before the process is swapped out again  Instead of swapping in a whole process, the pager brings only those pages into memory  How to determine that set of pages?  Need new MMU functionality to implement demand paging  The valid–invalid bit scheme can be used for this purpose  when this bit is set to “valid,” the associated page is both legal and in memory  If the bit is set to “invalid,” the page either is not valid (that is, not in the logical address space of the process) or is valid but is currently on the disk.
  • 12. 9.12 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition Basic Concepts (Cont.,)  If pages needed are already memory resident  No difference from non demand-paging  If page needed and not memory resident  Access to a page marked invalid causes a page fault  The paging hardware will notice that the invalid bit is set, causing a trap to the operating system
  • 13. 9.13 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition Basic Concepts (Cont.,)  The procedure for handling this page fault :  Check an internal table (usually kept with the process control block) for this process to determine whether the reference was a valid or an invalid memory access.  If the reference was invalid, we terminate the process. If it was valid but we have not yet brought in that page, we now page it in.  Find a free frame (by taking one from the free-frame list, for example).  Schedule a disk operation to read the desired page into the newly allocated frame.  When the disk read is complete, we modify the internal table kept with the process and the page table to indicate that the page is now in memory.  We restart the instruction that was interrupted by the trap. The process can now access the page as though it had always been in memory.
  • 14. 9.14 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition Valid-Invalid Bit  With each page table entry a valid–invalid bit is associated (v  in-memory – memory resident, i  not-in-memory)  Initially valid–invalid bit is set to i on all entries  Example of a page table snapshot:
  • 15. 9.15 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition Page Table When Some Pages Are Not in Main Memory
  • 16. 9.16 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition Steps in Handling a Page Fault
  • 17. 9.17 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition Aspects of Demand Paging  Extreme case – start process with no pages in memory  OS sets instruction pointer to first instruction of process, non- memory-resident -> page fault  And for every other process pages on first access  Pure demand paging  Actually, a given instruction could access multiple pages (one page for the instruction and many for data) -> may cause multiple page faults  Consider fetch and decode of instruction which adds 2 numbers from memory and stores result back to memory  Hardware support needed for demand paging  Page table with valid / invalid bit  Secondary memory (swap device with swap space)  Instruction restart (next slide)
  • 18. 9.18 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition Aspects of Demand Paging (Cont.,)  Instruction Restart  A crucial requirement for demand paging is the ability to restart any instruction after a page fault  Because we save the state (registers, condition code, instruction counter) of the interrupted process when the page fault occurs  we must be able to restart the process in exactly the same place and state, except that the desired page is now in memory and is accessible  A page fault may occur at any memory reference.  If the page fault occurs on the instruction fetch, we can restart by fetching the instruction again  If a page fault occurs while we are fetching an operand, we must fetch and decode the instruction again and then fetch the operand.
  • 19. 9.19 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition Aspects of Demand Paging (Cont.,)  As a worst-case example, consider a three-address instruction such as ADD the content of A to B, placing the result in C. These are the steps to execute this instruction:  Fetch and decode the instruction (ADD).  Fetch A.  Fetch B.  Add A and B.  Store the sum in C  If we fault when we try to store in C (because C is in a page not currently in memory), we will have to get the desired page, bring it in, correct the page table, and restart the instruction  The restart will require fetching the instruction again, decoding it again, fetching the two operands again, and then adding again.
  • 20. 9.20 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition Performance of Demand Paging  Stages in Demand Paging (worse case) 1. Trap to the operating system 2. Save the user registers and process state 3. Determine that the interrupt was a page fault 4. Check that the page reference was legal and determine the location of the page on the disk 5. Issue a read from the disk to a free frame: 1. Wait in a queue for this device until the read request is serviced 2. Wait for the device seek and/or latency time 3. Begin the transfer of the page to a free frame 6. While waiting, allocate the CPU to some other user 7. Receive an interrupt from the disk I/O subsystem (I/O completed) 8. Save the registers and process state for the other user 9. Determine that the interrupt was from the disk 10. Correct the page table and other tables to show page is now in memory 11. Wait for the CPU to be allocated to this process again 12. Restore the user registers, process state, and new page table, and then resume the interrupted instruction
  • 21. 9.21 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition Performance of Demand Paging (Cont.)  Three major activities in Demand Paging  Service the interrupt – careful coding means just several hundred instructions needed  Read the page – lots of time  Restart the process – again just a small amount of time  Page Fault Rate 0  p  1  if p = 0 no page faults  if p = 1, every reference is a fault  Effective Access Time (EAT) EAT = (1 – p) x memory access + p (page fault overhead + swap page out + swap page in )
  • 22. 9.22 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition Demand Paging Example  Memory access time = 200 nanoseconds (1s=109ns)  Average page-fault service time = 8 milliseconds (1s=103ms)  EAT = (1 – p) x 200 + p (8 milliseconds) = (1 – p ) x 200 + p x 8,000,000 = 200 + p x 7,999,800  If no page fault (p=0), then EAT = 200 ns  If one access out of 1,000 causes a page fault (p=0.001), then  EAT = 8.2 microseconds (8200ns).(1s=106ms)  This is a slowdown by a factor of 40!!  If want performance degradation < 10 percent  220 > 200 + 7,999,800 x p 20 > 7,999,800 x p  p < .0000025  < one page fault in every 400,000 memory accesses