3. 3
Memory Management
• Ideally programmers want memory that is
– large
– fast
– non volatile
• Memory hierarchy
– small amount of fast, expensive memory – cache
– some medium-speed, medium price main memory
– gigabytes of slow, cheap disk storage
• Memory manager handles the memory hierarchy
7. 7
Relocation and Protection
• Cannot be sure where program will be loaded in memory
1-Relocation is way to map virtual addresses into physical
addresses.
– address locations of variables, code routines cannot be absolute
– must keep a program out of other processes’ partitions
• Use base and limit values
– address locations added to base value to map to physical
address
– address locations larger than limit value is an error
15. 15
Page Replacement Algorithms
• Page fault forces choice
– which page must be removed
– make room for incoming page
• Modified page must first be saved
– unmodified just overwritten
• Better not to choose an often used page
– will probably need to be brought back in soon
16. 16
Optimal Page Replacement Algorithm
• Replace page needed at the farthest point in future
– Optimal but unrealizable
• Estimate by …
– logging page use on previous runs of process
– although this is impractical
17. 17
Not Recently Used Page Replacement Algorithm
• Each page has Reference bit, Modified bit
– bits are set when page is referenced, modified
• Pages are classified
1. not referenced, not modified
2. not referenced, modified
3. referenced, not modified
4. referenced, modified
• NRU removes page at random
– from lowest numbered non empty class
18. 18
FIFO Page Replacement Algorithm
• Maintain a linked list of all pages
– in order they came into memory
• Page at beginning of list replaced
• Disadvantage
– page in memory the longest may be often used
19. 19
Second Chance Page Replacement Algorithm
• Operation of a second chance
– pages sorted in FIFO order
– Page list if fault occurs at time 20, A has R bit set
(numbers above pages are loading times)
21. 21
Least Recently Used (LRU)
• Assume pages used recently will used again soon
– throw out page that has been unused for longest time
• Must keep a linked list of pages
– most recently used at front, least at rear
– update this list every memory reference !!
• Alternatively keep counter in each page table entry
– choose page with lowest value counter
– periodically zero the counter
22. 22
Simulating LRU in Software (1)
LRU using a matrix – pages referenced in order
0,1,2,3,2,1,0,3,2,3
23. 23
The Working Set Page Replacement Algorithm (1)
• The working set is the set of pages used by the k
most recent memory references
• w(k,t) is the size of the working set at time, t
24. 24
The WSClock Page Replacement Algorithm
Operation of the WSClock algorithm
26. 26
Load Control
• Despite good designs, system may still thrash
• When PFF algorithm indicates
– some processes need more memory
– but no processes need less
• Solution :
Reduce number of processes competing for memory
– swap one or more to disk, divide up pages they held
– reconsider degree of multiprogramming
27. 27
Page Size (1)
Small page size
• Advantages
– less internal fragmentation
– better fit for various data structures, code sections
– less unused program in memory
• Disadvantages
– programs need many pages, larger page tables
28. 28
Page Size (2)
• Overhead due to page table and internal
fragmentation
• Where
– s = average process size in bytes
– p = page size in bytes
– e = page entry
2
s e p
overhead
p
page table space
internal
fragmentation
Optimized when
2
p se
30. 30
Cleaning Policy
• Need for a background process, paging daemon
– periodically inspects state of memory
• When too few frames are free
– selects pages to evict using a replacement algorithm
• It can use same circular list (clock)
– as regular page replacement algorithmbut with diff ptr
31. 31
Implementation Issues
Operating System Involvement with Paging
Four times when OS involved with paging
1. Process creation
determine program size
create page table
2. Process execution
MMU reset for new process
TLB flushed
3. Page fault time
determine virtual address causing fault
swap target page out, needed page in
4. Process termination time
release page table, pages
32. 32
Page Fault Handling (1)
1. Hardware traps to kernel
2. General registers saved
3. OS determines which virtual page needed
4. OS checks validity of address, seeks page frame
5. If selected frame is dirty, write it to disk
33. 33
Page Fault Handling (2)
6. OS brings schedules new page in from disk
7. Page tables updated
Faulting instruction backed up to when it began
6. Faulting process scheduled
7. Registers restored
Program continues
34. 34
Locking Pages in Memory
• Virtual memory and I/O occasionally interact
• Issues call for read from device into buffer
– while waiting for I/O, another processes starts up
– has a page fault
– buffer for the first may be chosen to be paged out
• Need to specify some pages locked
– exempted from being target pages
44. 44
MCQs
1. What is virtual memory in an operating system?
a. A type of memory used for storing graphics and video data
b. A method that allows programs to execute using a portion of a hard
disk as if it were an extension of RAM
c. A type of memory used only by operating systems
d. A type of memory used for storing temporary data
Answer: b
2. What is the purpose of page replacement algorithms?
a. To allow multiple programs to share the same memory space
b. To optimize the use of cache memory in a computer system
c. To manage memory allocation and deallocation in an operating system
d. To select which memory pages should be removed from RAM when
there is no room for incoming pages
Answer: d
45. 45
3. What is thrashing in an operating system?
a. A high rate of swapping memory pages between RAM and virtual memory due
to insufficient memory
b. A method of allocating memory that requires contiguous blocks of memory
c. A mechanism that prevents access to memory by unauthorized programs
d. A scheduler algorithm for distributing CPU time between different programs
Answer: a
4. What is the role of the Memory Management Unit (MMU) in an operating
system?
a. To manage virtual memory for the operating system
b. To allocate memory resources to different programs
c. To protect memory from unauthorized access
d. To translate virtual addresses used by programs to physical addresses in
memory
Answer: d
5. Which of the following is an example of a page size used in memory
management?
a. 128 bytes
b. 256 kilobytes
c. 4 megabytes
d. 16 gigabytes Answer: c
46. 46
1. What is the purpose of memory management in an
operating system?
Answer: The purpose of memory management in an operating
system is to allocate a portion of the computer's memory to
running programs, manage memory allocation and deallocation,
protect one program's memory from another program, optimize
the use of available memory space and prevent memory-related
errors or crashes.
2. What is a memory partition in memory management?
Answer: A memory partition is a fixed block of memory reserved
for a specific program or process. It is created by the operating
system and allocated to a running program or process.
3. What is a page table in memory management?
Answer: A page table is a data structure used in memory
management that maps virtual memory addresses used by a
program to physical memory addresses where the data is stored.
47. 47
4. What is paging in memory management?
Answer: Paging is a technique used in memory management
to split a program's virtual memory into smaller blocks called
pages, which are stored in physical memory or on disk. The
operating system uses a page table to map the program's
virtual memory to physical memory locations.
5. What is thrashing in memory management?
Answer: Thrashing in memory management occurs when the
operating system spends a lot of time swapping pages between
physical memory and disk to create space for new pages. This
can lead to a severe drop in system performance and is caused
by insufficient physical memory for the programs being run.