Professional Documents
Culture Documents
Logical & Physical Address Space - Swapping - Memory Management Techniques
Logical & Physical Address Space - Swapping - Memory Management Techniques
Logical & Physical Address Space - Swapping - Memory Management Techniques
Physical address
– address seen by the memory unit
2
Swapping
Schematic View
3
Swapping
• A process can be swapped temporarily out of memory to a backing store, and
then brought back into memory for continued execution
• Backing Store
– Fast disk large enough to accommodate copies of all memory images for all users; must
provide direct access to these memory images
• Modified versions of swapping are found on many systems (i.e., UNIX, Linux,
and Windows)
5
Single Contiguous Memory Management
• Simplicity
• Available memory fully not utilised
• Limited Job Size (< Available Memory)
6
Partitioned Memory Management
7
8
Dynamic Storage-Allocation Problem
How to satisfy a request of size n from a list of free holes
• First-fit:
– Allocate the first hole that is big enough
• Best-fit:
– Allocate the smallest hole that is big enough; must search entire list, unless
ordered by size
– Produces the smallest leftover hole
• Worst-fit:
– Allocate the largest hole; must also search entire list
– Produces the largest leftover hole
First-fit and best-fit better than worst-fit in terms of speed and storage
utilization
9
1. Consider a swapping system in which memory consists of the following hole sizes
in memory order: 10KB, 4KB, 20KB, 18KB, 7KB, 9KB, 12KB, and 15KB. Which
hole is taken for successive segment requests of 12KB, 10KB, 9KB for first fit?
Now repeat the question for best fit, and worst fit.
8 KB 8 KB
18 KB 9 KB 18 KB 10 KB
(Job 3) (Job 2)
9 KB 8 KB
7 KB 7 KB 7 KB 7 KB
9 KB 9 KB 9 KB 9 KB
(Job3)
12 KB 12 KB 12 KB 12 KB
(Job 1)
15 KB 15 KB 15 KB 9 KB
Job 3)
6 KB
10
10
Relocation Partitioned Memory Management
11
Fragmentation
• External Fragmentation
– Total memory space exists to satisfy a request, but it is not contiguous
• Internal Fragmentation
– allocated memory may be slightly larger than requested memory; this size
difference is memory internal to a partition, but not being used
– I/O problem
• Latch job in memory while it is involved in I/O
• Do I/O only into OS buffers
12
Relocation Partitioned Memory Management
13
Paging
• Physical address space of a process can be noncontiguous.
• Process is allocated physical memory whenever the latter is
available
• Divide physical memory into fixed-sized blocks called frames
(Size is power of 2, between 512 bytes and 8,192 bytes)
14
Address Translation Scheme
Address generated by CPU is divided into:
Page number (p) – used as an index into a page table which
contains base address of each page in physical memory
Page offset (d) – combined with base address to define the
physical memory address that is sent to the memory unit
page number page offset
p d
m -n n
15
Paging Hardware
16
Paging Model of Logical and Physical Memory
17
Paging Example
18
Paging (Cont.)
• Calculating internal fragmentation
– Page size = 2,048 bytes
– Process size = 72,766 bytes
– 35 pages + 1,086 bytes
– Internal fragmentation of 2,048 - 1,086 = 962 bytes
– Worst case fragmentation = 1 frame – 1 byte
– On average fragmentation = 1 / 2 frame size
– So small frame sizes desirable?
– But each page table entry takes memory to track
– Page sizes growing over time
• Solaris supports two page sizes – 8 KB and 4 MB
• Process view and physical memory now very different
• By implementation process can only access its own memory
19
Segmentation
• Program
Collection of Segments
• Segment
Logical Unit
• Main Program
• Procedure
• Function
• Method
• Object
• Local / Global Variables
• Common Block
• Stack
• Symbol Table
• Arrays
20
User’s View of a Program
21
Logical View of Segmentation
4
1
3 2
4
22
Segmentation Architecture
• Logical address consists of a two tuple:
<segment-number, offset>,
23
Segmentation Architecture (Cont.)
• Protection
– With each entry in segment table associate:
• validation bit = 0 illegal segment
• read/write/execute privileges
• Protection bits associated with segments; code sharing
occurs at segment level
• Since segments vary in length, memory allocation is a
dynamic storage-allocation problem
• A segmentation example is shown in the following diagram
24
Segmentation Hardware
25
Example of Segmentation
26
Virtual Memory – Background
• Virtual memory
Separation of user logical memory from physical memory.
27
Virtual Memory That is Larger Than Physical Memory
28
Demand Paging
• Bring a page into memory only when it is needed
– Less I/O needed
– Less memory needed
– Faster response
– More users
• Lazy swapper
Never swaps a page into memory unless page will be needed
– Swapper that deals with pages is a pager
29
Demand Paging
30
Page Replacement
31
Page Replacement Algorithms
• Want lowest page-fault rate
• Evaluate algorithm by running it on a particular string of memory
references (reference string) and computing the number of page
faults on that string
32
Page Replacement Algorithms
33
FIFO Page Replacement Algorithm
Reference String: 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1.
34
First-In-First-Out (FIFO) Algorithm
• Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
• 3 frames (3 pages can be in memory at a time per process)
1 1 4 5
2 2 1 3 9 page faults
3 3 2 4
• 4 frames
1 1 5 4
2 2 1 5 10 page faults
3 3 2
4 4 3
35
Optimal Algorithm
• Replace page that will not be used for longest period of time
• Example:
Reference String: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
4 frames
1 4
2 6 page faults
3
4 5
36
Optimal Page Replacement
Reference String: 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1.
37
Least Recently Used (LRU) Algorithm
• Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
• Counter implementation
– Every page entry has a counter; every time page is referenced
through this entry, copy the clock into the counter
– When a page needs to be changed, look at the counters to
determine which are to change
1 1 1 1 5
2 2 2 2 2
3 5 5 4 4
4 4 3 3 3
38
LRU Page Replacement
Reference String: 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1.
• Stack implementation
Keep a stack of page numbers in a double link form:
– Page referenced:
• Move it to the top
• requires 6 pointers to be changed
39
Second Chance (Clock) Algorithms
• Reference bit
– With each page associate a bit, initially = 0
– When page is referenced bit set to 1
– Replace the one which is 0 (if one exists)
• We do not know the order, however
• Second chance
– Need reference bit
– Clock replacement
– If page to be replaced (in clock order) has reference bit = 1 then:
• set reference bit 0
• leave page in memory
• replace next page (in clock order), subject to same rules
40
Second-Chance (clock) Page-Replacement Algorithm
41
Overlay Memory Management
42
Thrashing
• If a process does not have “enough” pages, the page-fault rate is
very high. This leads to:
– low CPU utilization
– operating system thinks that it needs to increase the degree of
multiprogramming
– another process added to the system
• Thrashing
A process is busy swapping pages in and out
43
Windows
• Uses demand paging with clustering. Clustering brings in pages
surrounding the faulting page
• Processes are assigned working set minimum and working set
maximum
• Working set minimum is the minimum number of pages the
process is guaranteed to have in memory
• A process may be assigned as many pages up to its working set
maximum
• When the amount of free memory in the system falls below a
threshold, automatic working set trimming is performed to
restore the amount of free memory
• Working set trimming removes pages from processes that have
pages in excess of their working set minimum
44
Linux
• Linux address translation
• Linux uses paging to translate virtual addresses to physical addresses
• Linux does not use segmentation
Advantages
• More portable since some RISC architectures don’t support segmentation
• Hierarchical paging is flexible enough
• Intel x86 processes have segments
• Linux tries to avoid using segmentation
• Memory management is simpler when all processes use the same segment register values
• Using segment registers is not portable to other processors
• Linux uses paging 4k page size
• A three-level page table to handle 64-bit addresses
• On x86 processors
• Only a two-level page table is actually used
• Paging is supported in hardware
• TLB is provided as well
45
Assignment Questions
1. Explain briefly about free space management.
2. Explain the paging concept.
3. What is paging? Why paging is used?
4. Explain Paging technique with example.
5. What is fragmentation? Different types of fragmentation.
6. Explain internal fragmentation.
7. Explain briefly about
a. fragmentation
b. Swapping
c. Thrashing
9. Write short notes on Segmentation.
10. What is virtual memory?
46
Assigment Questions
11. Briefly explain the implementation of virtual memory.
12. Discuss the issues when a page fault occurs.
13. What are the needs for Page replacement algorithms? Explain
any two page replacement algorithms.
14. Write short notes on Page replacement algorithms and its
types.
15. Explain the following Page Replacement algorithms.
a. LRU Replacement
b. FIFO Replacement
c. Optimal Replacement
d. Second Chance Replacement
47