Memory Management - cs.tufts.edu · Partitioning Wednesday, October 27, 2004 4:58 AM Memory Management Page 6. Understanding fixed partitioning Some actions are time-critical, e.g.,

Post on 28-Aug-2019

213 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

Transcript

Memory Management:○ The process by which memory is shared,

allocated, and released. ○ Not applicable to cache memory.

Two kinds of memory management exposed in the operating system: ○ Physical: what is the fast memory used

for? ○ Virtual: using disk to simulate extra

memory.

Memory Management Wednesday, October 27, 20044:54 AM

Memory Management Page 1

Memory management tasks○ Relocation: moving processes from one

area of memory to another. ○ Protection: not allowing user processes to

access privileged data. ○ Sharing: allocation and de-allocation of

shared resources. ○ Logical organization: what do pages of

memory "mean" to processes. ○ Physical organization: what real pages are

used for what logical tasks.

Memory Management Page 2

Internal fragmentation: Fragmentation exists in physical memoryFragmentation does not exist in mapped memory.

External fragmentation:Fragmentation exists in mapped memory. Fragmentation may or may not exist in physical memory.

Memory Management Page 3

What's going on?In the normal process of allocation and de-allocation, memory gets "fragmented"

Memory Management Page 4

Defragmentationcollect all fragments, put them back together, and make access faster. can't do this with real memory. Reason: in C, you can't tell the difference at runtime between pointer and integer. If you move something, you have to move all pointers. Can't "defragment" logical memory. No memory manager can compensate for invariant weaknesses of language.

Memory Management Page 5

Partitioning○ The process by which regions of memory

are dedicated to particular tasks.○ Fixed partitioning: pages of memory that

always are used for the same thing.○ Operating system:

Always loadedIn same placeRegardless of what happens in processes

○ Resident programs: "Lock" themselves into memory. Use same pages for their lifetime.

Partitioning Wednesday, October 27, 20044:58 AM

Memory Management Page 6

Understanding fixed partitioning○ Some actions are time-critical, e.g.,

responding to an I/O device or emptying a buffer.

○ In these cases, "swapping" in order to load code to run is unacceptable.

○ Must "lock" certain time-critical code (including the kernel and any required modules) into memory for fast access.

Memory Management Page 7

Things you don't need to know about fixed partitioning○ No modern operating system uses fixed

partitioning for user processes. ○ Instead, modern operating systems depend upon:

Pagination: breaking process maps into "pages". Segmentation: separating process memory into segments by use.

Memory Management Page 8

Memory management challengesFragmentation: memory is broken up into segments with gaps between them; must find a "fragment" big enough for each use.

Internal fragmentation: must allocate more memory than needed. External fragmentation: unused memory lies in small gaps between processes

Memory management challenges

Wednesday, October 27, 20047:59 AM

Memory Management Page 9

Fragmentation kinds:

Memory Management Page 10

Fragmentation causes:○ Fixed page size.

Program size is not a multiple of page sizeSo there's room "left over"; internal fragmentation.

○ "Buddy system"Many algorithms depend upon dividing up resources. Allocation sizes are a power of two of pages.

Memory Management Page 11

Buddy system algorithm○ Start with 2k bytes/blocks/pages○ If you need m bytes, you actually get 2p

bytes, for some p, 2(p-1)<m<=2p

○ "Divy up" large blocks into smaller blocks by dividing size by two

○ Trick: if we keep linked lists of each size, and limit sizes to 2^p, then can search for a "matching" unused element in o(1).

Buddy System Algorithm Wednesday, October 27, 20048:14 AM

Memory Management Page 12

Buddy system in use:

Memory Management Page 13

Buddy system descriptors○ For every memory allocation algorithm,

need some kind of description of what memory is in use and what is unused.

○ Buddy system descriptors: array of linked lists of memory descriptors, which themselves live in an array.// simple descriptor table // separate from allocated memory.struct descriptor { struct descriptor *next;

void *memory; } descriptors[TABLESIZE], *free[POWERS], *used[POWERS];

Memory Management Page 14

Buddy system example

Memory Management Page 15

Buddy system example

Memory Management Page 16

Buddy system example

Memory Management Page 17

Buddy system example

Memory Management Page 18

Details of buddy algorithm

When you need a chunk of memory, Start at free list for least power of two greater than size; call its number "current". If that free list contains an element,

or divide(current + 1) thenUnlink it.Link to used list.Return pointer to memory.

int divide(int current) {If current>=POWERS) return false;

if (free[current] || divide(current+1)) {

// unlink first element of liststruct descriptor *head = free[current]; free[current]=free[current]->next;

// Get new descriptorstruct descriptor *newone = getdesc();

// point new descriptor at upper halfnewone->memory = (void *)((char *)(head->memory)+(1<<(current-1)));

// link two elements into next smaller free listhead->next=free[current-1];free[current-1]=head;newone->next=free[current-1]; free[current-1]=newone;

// successreturn true;

} else {Return false;

}

If current list contains an element,

Memory Management Page 19

If current list contains an element, Divide element in halfUnlink element from free list, Get new descriptor for top half, Link both descriptors into free[current-1]

Memory Management Page 20

More details of buddy algorithm

When you free a chunk of memoryLet the current used list it is in be called "current".Unlink it from used list for its size. Link it into free list for its sizeIf its nearest neighbor is contiguous, then

combine(current);

void combine(int current) { // unlink contiguous componentsIf (free[current] && free[current]->next

and these are contiguous blocks, Unlink both from current free list.Return descriptor of high block to descriptor pool.Link descriptor of low block into next higher free list.

}

Memory Management Page 21

Strengths of 'buddy system"○ Can defragment unused memory easily;

low external fragmentation.○ Principle of locality => defragmentation is

likely. ○ Can reuse segments by division or

combination with other segments. ○ Speed: descriptor table is really easy to

search for unallocated blocks of the appropriate size.

Weaknesses of "buddy system"○ Resource overhead: for every request,

there is wasted memory (internal fragmentation)

○ (though at release, the fragmentation ceases)

○ Still used for dynamic memory allocation (malloc and free) and kernel memory allocation.

○ Not used anymore for static process memory.

Memory Management Page 22

What's wrong with this picture: o(n) used list scan for freeo(1) allocation. <= n steps, n=levels => constant. How do we fix the o(n)?

Fixes get rid of used listuse local descriptors to replace it. "trust" application to return a valid pointer(note: in the previous version, we didn't need to trust, if we sent an invalid element, free could INFER that)

First, must change each memory element:

Memory Management Page 23

Way this is written

struct desc { struct desc *next; int power; } ;

char *buf = { pointer to 128 bytes of memory }

Manipulate (struct desc *buf) as LL element. Hand out (void *) ((char *)buf+8) as address from malloc. Get back same address, call it b. Get struct desc as (struct desc *)((char *)b-8)

This solution is fast: o(1) for allocate and deallocate. unsafe: can easily mess up storage descriptor with the application, e.g.,

int *a = (int *)malloc(5*sizeof(int)); a[-1]=10; // lie about length of block.

Memory Management Page 24

To fix thisseparate storage descriptorskeep used listo(n) for free. (enable malloc debugging and it does this)

Memory Management Page 25

We need a new language to understand how an operating system handles memory allocation○ Page: a unit of memory that a process needs.

Every page has a ○ Logical address within the process.○ Frame: a unit of memory that the processor has.

Every frame has a ○ Physical address: where it is in the machine○ Segment: a group of logically contiguous pages

with a particular function within a process.

Game: want to be precise about exactly what a process can do with a page.

read/writeread-onlynot accessible

Can't be arbitrarily expressiveminimum unit that can have an attribute: 1 pagesegment: operates on a group of pages to assign one attribute

Nomenclature Thursday, October 28, 20046:17 AM

Memory Management Page 26

Memory Management Page 27

Kernel viewall process pages are also physical frames with physical addresseskernel knows what segment they're, as well as map.

How segments are determined: ld takes .o files, create a.out, has segments. when you run it, ld.so (shared linker) adds "shared libraries". ".so" files. final image contains segment markers.

Memory Management Page 28

fundamental difference between an "application function" and an "OS function".

application function: lives in logical memory.

doesn't make system calls (except for limited reasons, e.g., sbrk)does not use the kernel.

system functionuses the kernelmay modify the memory mapmay take advantage of info at physical addresses.

basic principle: minimize system functionsprovide "cohesive" system interface.

Whysbrk MUST modify the memory map and run in kernel mode. there MUST be a map change in order for sbrk to worksbrk takes significantly more time than an application function call.

Memory Management Page 29

Memory Management Page 30

Properties of a memory mapbijective: uniqueness in both directions.

Each physical frame corresponds to a unique logical page. Each logical page corresponds to a unique physical frame.

Memory Management Page 31

Basic memory mapping model○ Establish a map between pages and frames○ When the process requests memory from a page,

translate into the appropriate frame address, then do the work.

○ If there is no frame address for a page address, crash. "Segmentation fault; core dumped"

Basic memory mapping model Thursday, October 28, 20046:20 AM

Memory Management Page 32

Memory Management Page 33

Basic memory mapping: processor support○ Base register: physical address of logical

address 0.○ Bounds register: contains size of map

Memory Management Page 34

Hardware translation

Memory Management Page 35

Memory Management Page 36

Generalization of simple memory mappingEach logical address is separated into a page number and an offset

0xc400

A page table keeps track of the correspondence between pages and frames.

Page tables Thursday, October 28, 20046:38 AM

Memory Management Page 37

Caveats about page tablesPage length is always a power of 2For large processes, page table can only be partly loaded into the processor. Processor contains a page table cache of part of the page table. This is a part of process context!

Memory Management Page 38

Four levelsis page table cache enough? is page in memory? is page on disk? else FAIL.

Memory Management Page 39

Segmentation○ A logical way of grouping a set of pages together.○ Independent of paging scheme. ○ Intent: group pages by function, protection, sharing,

etc. ○ Segment descriptor table: encodes protections for

segments.○ An OS can use both page maps and segments. ○ Typically not supported by the processor; a

software abstraction. ○ Book's example of segmentation without paging

does not occur in practice.

Segmentation Thursday, October 28, 20045:48 AM

Memory Management Page 40

Segments in a UNIX process

Memory Management Page 41

Missing piece: malloc and free (new and delete)calls sbrk to get a page at a time. manages heap memory. "allows holes""buddy system"

System calls:Malloc might call sbrk. Free never calls sbrk. From point of view of OS, memory for a process only grows. Reason: pointers can't be unravelled; need new model of memory in order to recover blocks.

Thursday, November 11, 20046:03 PM

Memory Management Page 42

top related