Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

OS Unit 5-Memory

Download as pdf or txt
Download as pdf or txt
You are on page 1of 60

Memory Management

Roadmap
• Basic requirements of Memory
Management
• Memory Partitioning
• Basic blocks of memory management
– Paging
– Segmentation
The need for memory
management
• Memory is cheap today, and getting
cheaper
– But applications are demanding more and
more memory, there is never enough!
• Memory Management, involves swapping
blocks of data from secondary storage.
• Memory I/O is slow compared to a CPU
– The OS must cleverly time the swapping to
maximise the CPU’s efficiency
Memory Management

Memory needs to be allocated to ensure a


reasonable supply of ready processes to
consume available processor time
Memory Management
Functions
• Keeping track of status of each memory
location
• Determining allocation policy for memory
• Update memory allocation information
• Deallocation technique & policy
Memory Management
Requirements
• Relocation
• Protection
• Sharing
• Logical organisation
• Physical organisation
Requirements: Relocation
• The programmer does not know where the
program will be placed in memory when it
is executed,
– it may be swapped to disk and return to main
memory at a different location (relocated)
• Memory references must be translated to
the actual physical memory address
Memory Management
Terms
Table 7.1 Memory Management Terms

Term Description
Frame Fixed-length block of main
memory.
Page Fixed-length block of data in
secondary memory (e.g. on disk).
Segment Variable-length block of data that
resides in secondary memory.
Addressing
Requirements: Protection
• Processes should not be able to reference
memory locations in another process
without permission
• Impossible to check absolute addresses at
compile time
• Must be checked at run time
Requirements: Sharing
• Allow several processes to access the
same portion of memory
• Better to allow each process access to the
same copy of the program rather than
have their own separate copy
Requirements: Logical
Organization
• Memory is organized linearly (usually)
• Programs are written in modules
– Modules can be written and compiled
independently
• Different degrees of protection given to
modules (read-only, execute-only)
• Share modules among processes
• Segmentation helps here
Requirements: Physical
Organization
• Cannot leave the programmer with the
responsibility to manage memory
• Memory available for a program plus its
data may be insufficient
– Overlaying allows various modules to be
assigned the same region of memory but is
time consuming to program
• Programmer does not know how much
space will be available
Partitioning
• An early method of managing memory
– Pre-virtual memory
– Not used much now
• But, it will clarify the later discussion of
virtual memory if we look first at
partitioning
– Virtual Memory has evolved from the
partitioning methods
Types of Partitioning
• Fixed Partitioning
• Dynamic Partitioning
• Simple Paging
• Simple Segmentation
• Virtual Memory Paging
• Virtual Memory Segmentation
Fixed Partitioning
• Equal-size partitions (see fig 7.3a)
– Any process whose size is less than
or equal to the partition size can be
loaded into an available partition
• The operating system can swap a
process out of a partition
– If none are in a ready or running
state
Fixed Partitioning Problems
• A program may not fit in a partition.
– The programmer must design the program
with overlays
• Main memory use is inefficient.
– Any program, no matter how small, occupies
an entire partition.
– This is results in internal fragmentation.
Solution – Unequal Size
Partitions
• Lessens both problems
– but doesn’t solve completely
• In Fig 7.3b,
– Programs up to 16M can be
accommodated without overlay
– Smaller programs can be placed in
smaller partitions, reducing internal
fragmentation
Dynamic Partitioning
• Partitions are of variable length and
number
• Process is allocated exactly as much
memory as required
Dynamic Partitioning
Example
OS (8M) • External Fragmentation
• Memory external to all
P2
P1
(14M)
(20M)
processes is fragmented
Empty (6M) • Can resolve using
Empty
P4(8M)
P2
(56M)
compaction
(14M)
Empty (6M)
– OS moves processes so
P3 that they are contiguous
(18M)
– Time consuming and
wastes CPU time
Empty (4M)

Refer to Figure 7.4


Dynamic Partitioning
• Operating system must decide which free
block to allocate to a process
• Best-fit algorithm
– Chooses the block that is closest in size to the
request
– Worst performer overall
– Since smallest block is found for process, the
smallest amount of fragmentation is left
– Memory compaction must be done more often
Dynamic Partitioning
• First-fit algorithm
– Scans memory form the beginning and
chooses the first available block that is large
enough
– Fastest
– May have many process loaded in the front
end of memory that must be searched over
when trying to find a free block
Dynamic Partitioning
• Next-fit
– Scans memory from the location of the last
placement
– More often allocate a block of memory at the
end of memory where the largest block is
found
– The largest block of memory is broken up into
smaller blocks
– Compaction is required to obtain a large block
at the end of memory
Dynamic Partitioning
• Worst-fit
– Allocates the largest space
– It searches the entire list
Allocation
Given six memory partitions of 300 KB, 600 KB, 350 KB, 200 KB, 750
KB, and 125 KB (in order), how would the first-fit, best-fit, and worst-fit
algorithms place processes of size 115 KB, 500 KB, 358 KB, 200 KB,
and 375 KB (in order)?

300KB 600KB 350KB 200KB 750KB 125KB


Given six memory partitions of 300 KB, 600 KB, 350 KB, 200 KB, 750 KB, and 125 KB (in order), how
would the first-fit, best-fit, and worst-fit algorithms place processes of size 115 KB, 500 KB, 358 KB,
200 KB, and 375 KB (in order)?

300KB 600KB 350KB 200KB 750KB 125KB


Paging
• Physical address space of a process can be noncontiguous;
process is allocated physical memory whenever the latter is
available
– Avoids external fragmentation
– Avoids problem of varying sized memory chunks
• Divide physical memory into fixed-sized blocks called frames
– Size is power of 2, between 512 bytes and 16 Mbytes
• Divide logical memory into blocks of same size called pages
• Keep track of all free frames
• To run a program of size N pages, need to find N free frames and
load program
• Set up a page table to translate logical to physical addresses
• Backing store likewise split into pages
• Still have Internal fragmentation
Paging
• Partition memory into small equal fixed-
size chunks and divide each process into
the same size chunks
• The chunks of a process are called pages
• The chunks of memory are called frames
Paging
• Operating system maintains a page table
for each process
– Contains the frame location for each page in
the process
– Memory address consist of a page number
and offset within the page
Processes and Frames

A.0
A.1
A.2
A.3
D.0
B.0
D.1
B.1
D.2
B.2
C.0
C.1
C.2
C.3
D.3
D.4
Page Table
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

– For given logical address space 2m and page size 2n


Paging Hardware
Paging
Logical Addresses
Example of Address Translation
Consider a logical address space of 64 pages of 1024 words each,
mapped onto a physical memory of 32 frames.
A) How many bits are there in logical address?
B) How many bits are there in physical address?

Solution :
Given : 64 pages = 2^6 ; 1024 words = 2^10 ; 32 frames = 2^5

Logical address = page no. + offset = 6 bits + 10 bits = 16 bits


Physical Address = frame no. + offset = 5 bits + 10 bits = 15 bits
Paging Model of Logical and Physical Memory
Paging Hardware With TLB
Segmentation
• A program can be subdivided into
segments
– Segments may vary in length
– There is a maximum segment length
• Addressing consist of two parts
– a segment number and
– an offset
• Segmentation is similar to dynamic
partitioning
Segmentation
Segmentation
Segmentation
Virtual Memory
• 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
• 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
Virtual Memory That is Larger Than Physical Memory
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
Basic Concepts
• With swapping, pager guesses which pages will be
used before swapping out again
• Instead, pager brings in only those pages into
memory
• How to determine that set of pages?
– Need new MMU functionality to implement demand paging
• If pages needed are already memory resident
– No difference from non demand-paging
• If page needed and not memory resident
– Need to detect and load the page into memory from
storage
• Without changing program behavior
• Without programmer needing to change code
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:

• During MMU address translation, if valid–invalid bit in page


table entry is i  page fault
Page Table When Some Pages Are Not in Main Memory
Page Fault
• If there is a reference to a page, first reference to
that page will trap to operating system:
page fault
1. Operating system looks at another table to
decide:
– Invalid reference  abort
– Just not in memory
2. Find free frame
3. Swap page into frame via scheduled disk
operation
4. Reset tables to indicate page now in memory
Set validation bit = v
5. Restart the instruction that caused the page fault
Steps in Handling a Page Fault
Paging
• Partition memory into small equal fixed-
size chunks and divide each process into
the same size chunks
• The chunks of a process are called pages
• The chunks of memory are called frames
Page and Frame Replacement Algorithms

• Frame-allocation algorithm determines


– How many frames to give each process
– Which frames to replace
• Page-replacement algorithm
– Want lowest page-fault rate on both first access and re-access
• Evaluate algorithm by running it on a particular string of
memory references (reference string) and computing the
number of page faults on that string
– String is just page numbers, not full addresses
– Repeated access to the same page does not cause a page fault
– Results depend on number of frames available
• In all our examples, the reference string of referenced page
numbers is
7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1
First-In-First-Out (FIFO)
Algorithm
• Reference string: 7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1
• 3 frames (3 pages can be in memory at a time per process)

15 page faults
• Can vary by reference string: consider 1,2,3,4,1,2,5,1,2,3,4,5
– Adding more frames can cause more page faults!
• Belady’s Anomaly
• How to track ages of pages?
– Just use a FIFO queue

– Page faults / Page Miss = 15 Miss Ratio = 15/20


– Page hit = 5 Hit Ratio = 5/20
First-In-First-Out (FIFO)
Algorithm
• Reference string: 7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1
• 3 frames (3 pages can be in memory at a time per process)

7 7 7 7 3 3 3 3 2 2
0 0 0 0 4 4 4 4 7
1 1 1 1 0 0 0 0
2 h 2 h 2 h h 2 h h h h 1 1 h h 1h
h
Least Recently Used (LRU)
Algorithm
• Use past knowledge rather than future
• Replace page that has not been used in the most amount of
time
• Associate time of last use with each page

• 12 faults – better than FIFO but worse than OPT


• Generally good algorithm and frequently used
Optimal Algorithm
• Replace page that will not be used for longest period of
time
– 9 is optimal for the example
• How do you know this?
– Can’t read the future
• Used for measuring how well your algorithm performs
• Reference String : 0,1,2,3,0,1,2,3,0,1,2,3,4,5,6,7
• Calculate Page faults (Page hits & Page miss)
for LRU, Optimal and FIFO for frame size 3 and
4.
• Reference String : 0,1,2,3,0,1,2,3,0,1,2,3,4,5,6,7
• Calculate Page faults (Page hits & Page miss) for LRU,
Optimal and FIFO for frame size 3 and 4.
Algorithm Frame size 3 Frame size 4
(Total Pages
Page Hits Page Miss Page Hits Page Miss
= 16)
FIFO 0 16 8 8
LRU 0 16 8 8
Optimal 6 10 8 8

You might also like