• No results found

Memory Management and Virtual Memory

N/A
N/A
Protected

Academic year: 2020

Share "Memory Management and Virtual Memory"

Copied!
76
0
0

Loading.... (view fulltext now)

Full text

(1)

Memory Management

• Ideally programmers want memory that is

– large

– fast

– non volatile

(2)

Memory Management

• Memory hierarchy

– Cache

• Few MBs, small amount of fast, expensive memory, volatile

– Main Memory

• Few GBs, some medium-speed, medium price, volatile

– Disk Storage

• Terabytes of slow, cheap, non-volatile

– Removable Storage

• Few GBs, non-volatile, slow, cheap, DVDs, USB sticks etc.

(3)

Memory Management

• Memory manager

– The part of operating system that manages the

memory hierarchy is called the Memory Manager.

• Efficiently manage memory

– Keep track of which parts of memory are in use

– Allocate memory to processes when they need it

– De-allocate it when they are done

• In this chapter, we will be concerned with the Main Memory Management.

– Cache is normally handled by the hardware

(4)

Basic Memory Management

No Memory Abstraction

• The simplest memory abstraction is

– No Abstraction at all

– Every program simply see the physical memory

• Example

– MOV REGISTER1,1000

– Moves contents of physical memory at location 1000 to Register1.

(5)

Basic Memory Management

No Memory Abstraction

• Three simple ways of organizing memory an operating system with one user process

– a) mainframes and minicomputers

– b) handheld and embedded

(6)

Basic Memory Management

No Memory Abstraction

• In this model, one only program can exist at a time in the main memory

• One solution is to use threads as threads share same space of the process with other threads. • But usually programs are unrelated, so threads

(7)

Basic Memory Management

Multiple Programs Without Memory Abstraction

• Another solution:

– Save entire contents of the program in the disk file

– Then bring in and run the next program.

– So at a time, there is only one program in the main memory and no conflicts.

• Disadvantage: Swapping is slow.

(8)

Basic Memory Management

Multiple Programs Without Memory Abstraction

• Another solution without swapping:

– IBM 360

– Memory divided into 2-kb blocks

– Each with 4-bit protection key stored in special registers inside CPU

– PSW(program status word) also contained a 4-bit key.

• So 1-MB memory needs

– 512 blocks (1024/2 =512) of these 4-bit registers

– These registers will take total 256 bytes of key storage (4*512 = 2048/8 = 256bytes)

(9)

• Only OS could change the protection keys

• So as a result, user processes cannot interfere with one another and with OS as well.

• Big drawback with this problem?

Basic Memory Management

(10)

Basic Memory Management

Multiple Programs Without Memory Abstraction

illustration of the relocation problem.

(11)

• Solution?

• Stop-gap solution from IBM 360

– Static relocation

• Modify program on the fly as it loaded in the main memory

• Even this solution is not very general and slow

– MOV REGISTER1,28

– Must not be relocated

– Loader needs to know which is an address and which is a constant

Basic Memory Management

(12)

Basic Memory Management

Multiple Programs Without Memory Abstraction

• Two things needs to be addressed:

– Multiple programs should run at a time in main memory

(13)

Why Need Abstraction

• exposing physical memory to processes has several major draw- backs

– First, if user programs can address every byte of memory, they can easily trash the operating system

(14)

Solution: Address Space

• New Memory abstraction

– Address Space

• creates a kind of abstract memory for programs to live in

is the set of addresses that a process can use to address memory.

(15)

Address Space

• Address Space Analogy Examples:

– Telephone numbers

• 7-digits

– 0,000,000 to 9,999,999 (some reserved numbers)

– IP addresses

• 32-digits

– 0 to 232 – 1 (some reserved numbers)

(16)

Base and Limit Registers

• When base and limit registers are used, programs are stored in consecutive memory locations in physical memory, wherever there is room and without

relocation during loading.

(17)

Base and Limit Registers

• When a process is run, the base register is loaded with the

physical address where its program begins in memory and the

limit register is loaded with the length of the program • first program is run are 0 and 16,384

• second program is run are 16,384 and 16,384 • Third would be:

– Base: 32,768

(18)

Base and Limit Registers

• Every time a process references memory,

– either to fetch an instruction or read or write a data word,

– the CPU hardware automatically adds the base value to the address

generated by the process before sending the address out on the memory bus

– Also it checks if the address offered is equal to or greater than the value in the limit register

(19)
(20)

Base and Limit Registers

• Disadvantage?

• On every memory reference

– Comparison (fast)

(21)

Swapping

• Usually there are more processes then the actual RAM.

• Example, after booting the system

– Some background processes starts running, consuming RAM

– User starts some foreground processes consumes more

• Consequently, keeping all processes in memory all the time requires a huge amount of memory and cannot be done if there is insufficient

(22)

Swapping

• Solution?

• 1) Swapping

• 2) Virtual Memory

(23)

Swapping

• 1) Swapping

• consists of bringing in each process in its

entirety, running it for a while, then putting it back on the disk.

(24)

Swapping

Memory allocation changes as – processes come into memory

– leave memory

(25)

Swapping

• Swapping creates multiple holes in memory

• Solution?

• combine them all into one big one

• by moving all the processes downward as far as possible

• This technique is known as memory compaction • Drawbacks?

• Requires a lot of CPU time

(26)

Swapping

• Swapping creates multiple holes in memory

• Any other Solution?

• Fixed Size processes – that never changes – Allocation becomes simple

• But in reality that is not the case.

• It grows Dynamically

(27)

Swapping

• As it grows Dynamically

• 1) If a hole is adjacent to the process, it can be allocated and the process allowed to grow into the hole

• 2) If the process is adjacent to another process, the growing process will either have to be moved to a hole in memory large enough for it, or one or more processes will have to be swapped out to create a large enough hole

(28)

Swapping

it is usually good idea to allocate extra memory for growth

(29)

Swapping

it is usually good idea to allocate extra memory for growth

• (a) Allocating space for a growing data segment upward. • (b) Allocating space for a growing stack downward and

a growing data segment upward.

• The memory between them can be used for either segment. If it runs out,

– the process will either have to be moved to a hole with sufficient space,

– swapped out of memory until a large enough hole can be created,

(30)

Swapping

• Solution?

• 1) Swapping

• 2) Virtual Memory

(31)

Swapping

• 1) Swapping

• consists of bringing in each process in its

entirety, running it for a while, then putting it back on the disk.

(32)

Swapping

Memory allocation changes as – processes come into memory

– leave memory

(33)

Swapping

• Swapping creates multiple holes in memory

• Solution?

• combine them all into one big one

• by moving all the processes downward as far as possible

• This technique is known as memory compaction • Drawbacks?

• Requires a lot of CPU time

(34)

Swapping

• Swapping creates multiple holes in memory

• Any other Solution?

• Fixed Size processes – that never changes – Allocation becomes simple

• But in reality that is not the case.

• It grows Dynamically

(35)

Swapping

• As it grows Dynamically

• 1) If a hole is adjacent to the process, it can be allocated and the process allowed to grow into the hole

• 2) If the process is adjacent to another process, the growing process will either have to be moved to a hole in memory large enough for it, or one or more processes will have to be swapped out to create a large enough hole

(36)

Swapping

it is usually good idea to allocate extra memory for growth

(37)

Swapping

it is usually good idea to allocate extra memory for growth

• (a) Allocating space for a growing data segment upward. • (b) Allocating space for a growing stack downward and

a growing data segment upward.

• The memory between them can be used for either segment. If it runs out,

– the process will either have to be moved to a hole with sufficient space,

– swapped out of memory until a large enough hole can be created,

(38)

Managing Free Memory

• Dynamic memory needs free memory management

– 1) Bitmaps

– 2) Free Lists

– BITMAPS

• Memory is divided into allocation units

– Can be in few words or in KBs

• A bit corresponds to each allocation unit in the bitmap

– 0 for the free unit

(39)

1) Memory Management with Bit Maps

a) Part of memory with 5 processes, 3 holes

– tick marks show allocation units

– shaded regions are free

b) Corresponding bit map

(40)

1) Memory Management with Bit Maps

• Smaller the allocation unit, the larger the bitmap

– 4 bytes or 32 bits of memory will require only 1 bit of the map

– A memory of 32n bits will use n map bits, so the bitmap will take up only 1/33th of the memory

• Drawback?

– To bring a k unit process into memory, the memory manager must search the bitmap to find a run of k consecutive 0 bits in the map

(41)

2) Memory Management with Linked Lists

• keeping track of memory to maintain a linked list of allocated and free memory segments

• c) shows memory management with linked lists

P shows allocated memory by processes and H shows holes

– After P or H, first box shows start index, second shows length and third

(42)

2) Memory Management with Linked Lists

Four neighbor combinations for the terminating process X

• a) updating the list requires replacing a P by an H

(43)

Allocating Memory Algorithms in Free Memory

• When the processes and holes are kept on a list sorted by address, several algorithms can be used to allocate memory for a created process (or an existing process being swapped in from disk). • Algorithms:

First Fit (FF)

• memory manager scans along the list of segments until it finds a hole that is big enough

• Fast and simple algorithm

Next Fit (NF)

(44)

Algorithms:

Best Fit (BF)

• Searches the entire list, from beginning to end, and takes the smallest hole that is adequate and matches the requirement

• Slower than First Fit or Next Fit

• Leaves more wasted memory than first fit or next fit because it fill ups tiny, useless holes and first fit generates larger holes on average.

Worst Fit (WF)

• always take the largest available hole, so that the new hole will be big enough to be useful.

• All four algorithms can be speeded up by maintaining separate lists for processes and holes

Quick Fit (QF)

• which maintains separate lists for some of the more common sizes requested

(45)

Example

• Example:

(46)

• All algorithms have one big same drawback?

– when a process terminates or is swapped out, finding its neighbors to see if a merge is

possible is time expensive

– If merging is not done, memory will quickly fragment into a large number of small holes

(47)

Virtual Memory

• Program is split up into chunks called Pages

• Each page is a contiguous range of addresses. • These pages are mapped onto physical

memory.

• Not all pages have to be in physical memory to run the program.

• OS keeps parts of the program which are

(48)

Virtual Memory

• virtual address space is divided into fixed-size units called pages

• corresponding units in the physical memory are called page frames

• MMU (memory management unit) translates

(49)

Virtual Memory

Paging (1)

(50)

Paging (2)

The relation between virtual addresses and physical

memory addres-ses given by

page table.

(51)

Paging(3)

• a Present/absent bit keeps track of which pages are physically present in memory • Example:

• MOV REG,32780

• which is byte 12 within virtual page 8 (starting at 32768)?

• The MMU notices that the page is unmapped (indicated by a cross in the figure)

(52)

Page Tables (1)

the virtual address is split into a virtual page number (4-bit

high-order bits) and an offset (12-bit

(53)

Page Tables (3)

(54)

Page Table Entry

• Present/Absent

– 1 for present

– 0 for absent and page fault occurs

• Protection Bit

– Simple form having 1 bit for indication

• 0 for read/write

• 1 for ready only

– Better form having 3 bits for indication

(55)

Page Table Entry

– Modified bit or dirty bit (reflects page’s state)

• “dirty”

– If it is written and needs to write back to disk then

• “clean”

– If it is not modified

– Referenced bit (helps OS choose a page to evict)

• Set or 1

– Referenced either for reading or writing

• Otherwise not set or 0

– Caching Disabled bit

• Set or 1 (disabled)

(56)

Speeding Up Paging

• There are two main Goals

– The mapping from virtual address to physical address must be fast.

• TLB

– If the virtual address space is large, the page table will be large.

• Multilevel tables for 32-bit addresses

(57)

Speeding Up Paging

• Page Table can reside on

– Disk (too slow)

– Memory (faster but more references)

• Register to register copy needs atleast two references whereas it could have been done in single reference without page table

– Separate hardware/registers (fast but little expensive)

• Solution: TLB

– Page table in memory and

(58)

TLBs – Translation Lookaside Buffers

• small hardware device for mapping virtual

addresses to physical addresses without going through the page table

• Also called associative memory • Inside MMU

(59)

TLBs – Translation Lookaside Buffers

(60)

TLB

• If found in TLB then

– It is a hit

• If not found in TLB then

– It is miss and TLB needs to evict an entry and take entry from page table after page table lookup

– Two type of miss

• Soft miss (not in TLB but in memory)

• Hard miss (not in TBL nor in memory) so disk I/O is needed

(61)

Software TLB Management

• Currently, TLB management and handling TLB faults are handled by MMU

• To simplify MMU and increase performance

instead of hardware TLB management, faults can be maintained by OS.

(62)

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

(63)

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

(64)

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

(65)

FIFO Page Replacement Algorithm

• Maintain a linked list of all pages

– in order they came into memory

• Page at beginning of list replaced

• Disadvantage

(66)

Second Chance Page Replacement Algorithm

• Operation of a second chance

– pages sorted in FIFO order

(67)
(68)

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 (NFU)

– choose page with lowest value counter

(69)

Working Set Page Replacement Algorithm

• Focus on reducing page fault rate

• Working set

– set of pages that a process is currently using

– as at a time process only references to few pages, not all of them

(70)

The Working Set Page Replacement Algorithm (1)

• The working set is the set of pages used by the k

(71)

The Working Set Page Replacement Algorithm (2)

(72)
(73)
(74)

Fragmentation

• Internal Fragmentation

– Wastage of memory due to extra allocated space within a page or unit

• External Fragmentation

– Wastage of memory due to allocation

algorithms leaving spaces or holes between

(75)

Some terms you should know

• Demand paging

– pages are loaded only on demand, rather than in advance

• Thrashing

– A program causing page faults every few instructions is said to be

• Locality of reference

– during any phase of execution, the process

(76)

Reading Task

• Buddy Systems

References

Related documents