chapter 10 memory management part_1. 2 overview basic concepts the major tasks of the memory manger...

Post on 01-Jan-2016

217 Views

Category:

Documents

3 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Chapter 10Memory

ManagementPart_1

2

Overview Basic Concepts• The major tasks of the memory manger are

the allocation and deallocation of main memory .

• Main Memory is an important affect of system performances and functions of the OS

• In early OS with Multiprogramming, memory was divided into a number of partitions/blocks to allocate processes

3

Memory Management• Swapping • Overlaying – not used any more• Partitioning• Paging • Segmentation• Virtual memory - program can reference more

memory than what is available in main (physical) memory. In computing, virtual memory is a memory management technique that is implemented using both hardware and software. It maps memory addresses used by a program, called virtual addresses, into physical addresses in computer memory.

In computing, virtual memory is a memory management technique that is implemented using both hardware and software. It maps memory addresses used by a program, called virtual addresses, into physical addresses in computer memory.

• Physical addressing means that your program actually knows the real layout of RAM. When you access a variable at address 0x8746b3, that's where it's really stored in the physical RAM chips.

• Virtual addressing, all application memory accesses go to a page table, which then maps from the virtual to the physical address. So every application has its own "private" address space, and no program can read or write to another program's memory. This is called segmentation.

• Virtual addressing has many benefits. It protects programs from crashing each other through poor pointer manipulation, etc. Because each program has its own distinct virtual memory set, no program can read another's data - this is both a safety and a security plus.

Physical and Virtual Memory

VM• Virtual memory also enables paging, where a

program's physical RAM may be stored on a disk (or, now, slower flash) when not in use, then called back when an application attempts to access the page.

• Also, since only one program may be resident at a particular physical page, in a physical paging system, either a) all programs must be compiled to load at different memory addresses or b) every program must use Position-Independent Code, or c) some sets of programs cannot run simultaneously.

6

Process Address Space

• A logical address is a reference to some location of a process

• The set of logical addresses that a process references in its code.

• When memory is allocated to the process, its set of logical addresses will be bound to physical addresses.

7

Logical and Physical AddressesHigh-Level view of the mapping of logical address to the physical address of two process (P1, P2)Three types of address are

used in a program before and after memory is allocated.

1.Symbolic address(used in Source code, like variables names, const, etc.)

2. Relative address(a compiler converts symbolic address into relative address)

3. Physical address(The final address generated when a program is loaded and ready to execute in physical memory)

8

Binding

• The association of instructions and data to memory addresses

• Can occur at any of the following steps– Compile time– Load time– Execution time

9

Phases of a Program

1. Compile time: If memory location known a priori, absolute code can be generated; must recompile code if starting location changes.

2. Linkage time: Program is combined with other modules.

3. Load time: Program is loaded into memory.4. Execution time: Binding delayed until run time if

the process can be moved during its execution from one memory segment to another. Need hardware support for address maps (e.g., base and limit registers).

10

Program Phases and Addresses

11

Managing the Address Space• The compiler or assembler generates the

program as a relocatable object module• The linker combines several modules into a load

module• During memory allocation, the loader places the

load module in the allocated block of memory• The loader binds the logical address to a

physical address

12

Dynamic Loading• Routines or modules to be used by a program are not

loaded until called

– All routines are stored on a disk in relocatable form– Better memory-space utilization; unused routines

are never loaded.– Useful when large amounts of code are needed to

handle infrequently occurring cases.– No special support from the operating system is

required to be implemented through program design.

13

Dynamic Linking• Linking postponed until execution time.• Example: Dynamic Linked Libraries (DLL)• Small piece of code, stub, used to locate the

appropriate memory-resident library routine.• Stub replaces itself with the address of the

routine, and executes the routine.• Operating system needed to check if the routine

is in the processes’ memory address.

14

Memory-Management Unit (MMU)

• Hardware device that maps virtual to physical address.

• In MMU scheme, the value in the relocation register is added to every address generated by a user process at the time it is sent to memory.

• The user program deals with logical addresses; it never directly references the real physical addresses.

15

Static Relocation• A process executes at a specific address. • Addresses are assigned at load time.• Relocation modifies instructions that

address locations in memory.• Static relocation does not solve protection.

16

Dynamic Relocation

• Addresses can be changed at execution time.

• Facilitates Protection using: –Addressing via Base (& Limit) Register.

• Additional benefit, program may be moved during execution.

17

Memory Protection

Necessary because malicious or incompetent programs may access the program or data space of another program and contaminate it.

18

Contiguous Memory Allocation

• Main memory is divided into several partitions

• A partition is a contiguous block of memory that can be allocated to an individual process

• The degree of multiprogramming is determined by the number of partitions in memory.

19

Multiple Partitions

• Fixed partitions (static) – the number and sizes of the partitions do not change

• Variable partitions (dynamic) – partitions are created dynamically according to:– available memory– the memory requirements of processes

20

Fixed Partitions

• Memory is divided into fixed-sized partitions. These are not normally of the same size.

• The number and the size of the partitions are fixed. • One partition is allocated to each active process in the

multiprogramming set. • There is one special partition, the system partition, in

which the memory-resident portion of the operating system is always stored.

21

Fixed Partitions

Fixed partition is configure from address 0 to address 100K

Partition 1 is configured from address100K to address 300K

Partition 2 from address 300K to address 450K

Partition 3 from address 450K to address 700K

Partition 4 from address 700K to address 850K

Partition 5 from address 850K to address 1100K

Five active process that have been allocated partitions.

22

Fragmentation in Fixed Partition

Fragmentation problem

• Internal fragmentation - A partition is only partially used.

• A partition is available, but not large enough for any waiting progress.

23

Holes in Memory

• Hole – means a contiguous block of available memory; holes of various size are scattered throughout memory.

• When a process requests memory, it is allocated memory from a hole large enough to accommodate it.

• Operating system maintains data about:– allocated partitions– Available memory blocks (holes)

Memory Allocation to P7

24

If a hole is sufficiently large, it can be allocated to a process of the same or smaller memory

Assume P7 has a size of 200K bytes

Smaller hole of 75K

Assume P5 has a size of 200K

De-allocating Memory to P5

25

Memory state after the system deallocates the memory from process P5 and the process is removed from the memory.There fore, new hole is created of size 200K

26

Advantages of Dynamic Partitions• In dynamic partitioning, the holes

represent the memory that is available, and if they are too small, they cannot be allocated. They represent external fragmentation.

• Memory utilization is generally better for variable-partition schemes.

• There is little or no internal fragmentation. • There can be external fragmentation.

27

Memory Allocation

• 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.

How to satisfy a process request of size n from a list of free holes.

First-fit and best-fit better than worst-fit in terms of speed and storage utilization.

Memory After Compaction

28

29

Swapping• A process can be swapped temporarily out of

memory to a backing store, and then brought back into memory for continued execution– Total physical memory space of processes can exceed

physical memory• A process can be swapped temporarily out of

memory to secondary storage, and then loaded into memory again to resume execution.

• Secondary storage – fast disk large enough to accommodate copies of all memory images for all users; must provide direct access to these memory images.

30

Pages

• A page is a unit of logical memory of a program• A frame is a unit of physical memory• All pages are of the same size• All frames are of the same size• A frame is of the same size as a page

31

Paging• Physical memory is divided into fixed-sized blocks

called frames (size is power of 2 ).• Logical memory is divided into blocks of same size

called pages.• Size of process is measured in the number of

pages.• Physical memory is divided into small fixed-size blocked

of physical memory called frames. • If a 15-page process is waiting for memory, the system

needs to find any 15 frames to allocate to this process• The size of a page is a power of two

32

• Example of Page shows an example with paging in which the physical memory is divided into 32 frames. f0 to f31, and not all the frames have been allocated.

• The frames with page of a process are not all contiguous (Used).

• Example:f5 allocated to page 0 of P3; f20 allocated to page 1 of P6

33

Paging(2)

• The OS keeps track of all free (available) frames, and allocated frames in the page table.

• To run a program of size n pages, the OS needs n free frames to load program.

• The OS sets up a page table for every process• The page table is used for converting logical

addresses to physical addresses. • There is a small amount of internal

fragmentation.

34

Logical vs Physical Memory

• Logical memory corresponds to the user’s view of memory

• Physical memory is the actual contents and addresses of memory

• The user’s view of memory is mapped onto physical memory

• This mapping is done on every reference to logical memory

Block Mapping

Mapping Virtual addresses to real addresses

36

Logical Address (2)Address generated by the compiler/assembler 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) – the relative address in the page. • This pair of numbers will be converted to the

physical memory address that is sent to the memory unit.

37

Example of a Logical Address

Offset = 478

2 power 10 = 1024

Page number = 2

20 –bit address is a system that used 1k pages

Lower bits of the addressHigher 10 bits are used to reference the page number

38

Physical Address• When the system allocates a frame to a page,

it translates this logical address into a physical address that consists of a frame number and the offset.

• For this, the system needs to know the correspondence of a page of a process to a frame in physical memory and it uses a page table

39

Example of a Physical Address

Frame number = 4 Offset = 1DEh

hexa decimal = Group each bit in 4 bits

40

General Concepts of Address Translation Architecture

41

Page Table Example

Chapter 10Memory

Management(continued - Part_2)

Segmentation• Segmentation is a technique for breaking memory up

into logical pieces• Each “piece” is a grouping of related information

– data segments for each process– code segments for each process– data segments for the OS– etc.

• Like paging, use virtual addresses and use disk to make memory look bigger than it really is

• Segmentation can be implemented with or without paging

Segmentation

logical address space

OS Code OS data OS stack

P1 data

P1 code

printfunction

P2 code

P2 data

Addressing Segments• Let’s first assume no paging in the system• User generates logical addresses• These addresses consist of a segment number

and an offset into the segment• Use segment number to index into a table• Table contains the physical address of the start of

the segment– often called the base address

• Add the offset to the base and generate the physical address– before doing this, check the offset against a limit– the limit is the size of the segment

Addressing Segments

Start Offset

o < limit

Limit Is the sizeof segment

Base Often called physical Address

+Physical Address

error

yes

no

segment table

User generates logical address

Page number (p) – used as an index into a page table (which contains base address of each page in physical memory).

Page offset (d) – the relative address in the page. This pair of numbers will be converted to the physical memory address that is sent to the memory unit.

Segmentation Hardware• Sounds very similar to paging• Big difference – segments can be variable in size• As with paging, to be effective hardware must be

used to translate logical address• Most systems provide segment registers• If a reference isn’t found in one of the segment

registers– trap to operating system– OS does lookup in segment table and loads new

segment descriptor into the register– return control to the user and resume

• Again, similar to paging

Program Modules

Segments are modules of a program:• main program• Procedure• function• local variables, global variables• common block• stack• symbol table, arrays

48

49

Segmentation and Memory Allocation

• Before a program can execute, all its segments need to be loaded into memory.

• For every segment, the operating system needs to find a contiguous block of available memory to allocate to the segment.

Example of Segmentation

50

5 se

gmen

ts is

set

up

for

proc

ess

Segment table for the processes of 5 segments

The logical address of a process consist of two parts: -segment number -offset.

51

Virtual Memory

• The memory space of a process is normally divided into blocks that are either pages or segments.

• Virtual memory management takes advantage of the typical behavior of a process: not all blocks of the process are needed during the execution of a process.

• Therefore, the physical address space of a process is smaller than its logical address space.

52

Address Space

• The virtual address space of a process is the entire set of all its addresses in the absolute program.

• After linkage, the absolute version of the program is stored on disk. The disk area that stores all the processes in absolute form is called the virtual memory

• The physical address space of a process is much smaller than its virtual address because only a portion of the process will ever be loaded into main memory.

Address Translation in VM

53

The physical address is built using the frame address and the page offset.

index

54

Page FaultEach page table entry has a resident bit, it

indicates whether the corresponding page is currently in memory.

• If the page is not in memory, a page fault has occurred and the control is trapped to the OS.

• During address translation, if valid–invalid bit in page table entry is 0, a page fault has occurred.

55

Demand Paging

In demand paging, then a page fault occurs when a reference is made to a page not in memory. The page fault may occur while:

ALU• fetching an instruction, or• fetching an operand of an instruction.

56

Paging Policies

• Fetch policy -- decides when a page should be loaded into memory

• Replacement policy -- decides which page in memory should be replaced

• Placement policy -- decides where in memory should a page be loaded

57

FRAME ALLOCATIONAllocation schemes:• Equal allocation

• Proportional allocation (based on size, priority, )

Global vs. local replacement• Global: a replacement frame can be selected from the set

of all frames

• Local: a replacement frame for a process can be selected only from its own set of allocated frames.

Localities of a Process

58

59

Page ReferenceA page reference string is a sequence of page

numbers in order of reference. A subsequence of the same page number can be reduced to a single occurrence.

• An example of a sequence of page references is: < 3,6,2,1,4,7,3,5,8,9,2,8,10,7 >

• The sequence of page references represents the behavior of a process during execution.

60

Static Replacement Algorithms• The static paging algorithms implement the

replacement policy when the frame allocation to a process is fixed.

• The three most common static paging algorithms: – First-in-first-out (FIFO) replacement – Optimal replacement– Least recently used (LRU) replacement

61

FIFO Algorithm• When a page fault occurs and there are no

empty frames for the process, the page selected to be replaced is the one that has been in memory the longest time, the oldest page.

• FIFO page replacement– Replace page that has been in the system the

longest– Likely to replace heavily used pages– Can be implemented with relatively low overhead– Impractical for most systems

First-In-First-Out (FIFO) Page Replacement

No fault zone weblink

FIFO Anomaly

• Belady’s (or FIFO) Anomoly – Certain page reference patterns actually cause

more page faults when number of page frames allocated to a process is increased

FIFO anomaly–page faults can increase with page frame allocation.

FIFO Anomaly

65

FIFO Example

* The first page referenced is page 1. The system loads this page into one of the allocated frames and an start (*) placed at the bottom of the column indicated that a page fault occurred.

66

Optimal Algorithm• The optimal algorithm for page replacement requires

knowledge of the entire page reference stream in advance.

• When a page fault occurs and there are no empty frames for the process, the algorithm looks ahead in the page reference stream to find out about future references to the pages currently in physical memory.

• The approach used is to replace the page in memory that will not be used for the longest period.

67

Optimal Example

68

LRU Algorithm• The least recently used (LRU) replacement

algorithm replaces the page that has not been used for the longest period.

• The assumption is that recent page references give a good estimation of page references in the near future.

• When a page fault occurs and there are no empty frames the algorithm selects the least recently referenced page for replacement.

Least-Recently-Used (LRU) Page Replacement

• LRU page replacement– Exploits temporal locality by replacing the page that

has spent the longest time in memory without being referenced

– Can provide better performance than FIFO– Increased system overhead– LRU can perform poorly if the least-recently used

page is the next page to be referenced by a program that is iterating inside a loop that references several pages

Least-Recently-Used (LRU) Page Replacement Strategy

71

LRU Example

72

Thrashing• A process is thrashing if is spending more time

paging than executing• A thrashing process can cause other processes

to thrash if a global page replacement strategy is used

• When CPU utilization is low, the OS may increase the degree of multiprogramming and cause other processes to thrash

73

Locality

• Why does paging work?– The locality model is used -- the set of pages used

for that particular phase of computation– A process migrates from one locality to the next,

as the process moves to a different phase of computation

– Localities may overlap

top related