3. Memory Management - René Doursat

Jul 3, 2006 - B . and Gagne. G . (2003). Operating Systems Concep ts w ith. Java. (6th. Ed ition). .... first-fit placement scans the memory from the beginning and chooses the first ... From object codes to executable in memory. Loading: ...
609KB taille 1 téléchargements 44 vues
Principles of Operating Systems CS 446/646

3. Memory Management

René Doursat Department of Computer Science & Engineering University of Nevada, Reno Spring 2006

Principles of Operating Systems CS 446/646 0. Course Presentation 1. Introduction to Operating Systems 2. Processes 3. Memory Management 4. CPU Scheduling 5. Input/Output 6. File System 7. Case Studies

3/7/2006

CS 446/646 - Principles of Operating Systems - 3. Memory Management

2

Principles of Operating Systems CS 446/646 3. Memory Management a. Goals of Memory Management b. Partitioning c. Linking & Loading d. Simple Paging & Segmentation e. Virtual Memory f.

3/7/2006

Page Replacement Algorithms

CS 446/646 - Principles of Operating Systems - 3. Memory Management

3

Principles of Operating Systems CS 446/646 3. Memory Management a. Goals of Memory Management 9 9 9 9

How to distribute multiple processes in memory? Relocation of address references Protection & sharing of address spaces Logical vs. physical organization

b. Partitioning c. Linking & Loading d. Simple Paging & Segmentation e. Virtual Memory f.

3/7/2006

Page Replacement Algorithms

CS 446/646 - Principles of Operating Systems - 3. Memory Management

4

3.a Goals of Memory Management How to distribute multiple processes in memory?

¾ The O/S must fit multiple processes in memory 9 memory needs to be subdivided to accommodate multiple processes 9 memory needs to be allocated to ensure a reasonable supply of ready processes so that the CPU is never idle 9 memory management is an optimization task under constraints

Fitting processes into memory is like fitting boxes into a fixed amount of space 3/7/2006

CS 446/646 - Principles of Operating Systems - 3. Memory Management

5

3.a Goals of Memory Management How to distribute multiple processes in memory?

¾ Memory management must satisfy various requirements 9 relocation of address references ƒ must translate memory references to physical addresses 9 protection of memory spaces ƒ forbid cross-process references 9 sharing of memory spaces ƒ allow several processes to access a common memory area 9 logical organization (of programs) ƒ programs are broken up into independent modules 9 physical organization (of memory) ƒ fit multiple programs and modules in physical memory 3/7/2006

CS 446/646 - Principles of Operating Systems - 3. Memory Management

6

3.a Goals of Memory Management Relocation of address references

¾ Relocation of address references 9 the programmer does not know where the program will be placed in memory when it is executed 9 while the program is executing, it may also be swapped to disk and returned to main memory at a different location (relocated) → thus, logical (relative) memory references must be translated to physical (absolute) addresses 3/7/2006

Stallings, W. (2004) Operating Systems: Internals and Design Principles (5th Edition).

Process addressing requirements

CS 446/646 - Principles of Operating Systems - 3. Memory Management

7

3.a Goals of Memory Management Protection of address spaces

¾ Protection of address spaces 9 processes must not use memory locations in other processes 9 addressing must be checked at run time, as it is impossible to check physical addresses at compile time 9 however, the operating system cannot anticipate all of the (calculated) memory references a program will make → thus, fine-level protection is ultimately carried out in hardware Silberschatz, A., Galvin, P. B. and Gagne. G. (2003) Operating Systems Concepts with Java (6th Edition).

One type of memory protection using base and limit registers 3/7/2006

CS 446/646 - Principles of Operating Systems - 3. Memory Management

8

3.a Goals of Memory Management Sharing of address spaces

¾ Sharing of address spaces 9 conversely, it should be possible to allow several processes to access the same portion of memory ƒ

for example, processes executing the same program can save resources by sharing the same copy of code in memory

ƒ

also, processes cooperating on some task may need access to the same data structure

→ we will see that mechanisms supporting relocation also support sharing capabilities

3/7/2006

CS 446/646 - Principles of Operating Systems - 3. Memory Management

9

3.a Goals of Memory Management Logical organization

¾ Logical organization (of programs) 9 the linear 1-D organization of memory does not reflect the way programs are typically constructed 9 large programs are often organized into modules and the O/S should be able to handle modular programs, so that: ƒ modules can be written and compiled independently ƒ

different degrees of protection can be given to different modules: read-only, execute-only, read-and-write, etc.

ƒ modules can be shared among processes → segmentation is a memory management technique that supports modularization 3/7/2006

CS 446/646 - Principles of Operating Systems - 3. Memory Management

10

3.a Goals of Memory Management Physical organization

¾ Physical organization (of memory) 9 two-level scheme ƒ main memory: fast access, high cost, volatile ƒ secondary memory: slow access, cheaper, long-term storage → thus, a major O/S concern is the flow of information between main and secondary memory (it should not be a user concern)

3/7/2006

CS 446/646 - Principles of Operating Systems - 3. Memory Management

11

Principles of Operating Systems CS 446/646 3. Memory Management a. Goals of Memory Management 9 9 9 9

How to distribute multiple processes in memory? Relocation of address references Protection & sharing of address spaces Logical vs. physical organization

b. Partitioning c. Linking & Loading d. Simple Paging & Segmentation e. Virtual Memory f.

3/7/2006

Page Replacement Algorithms

CS 446/646 - Principles of Operating Systems - 3. Memory Management

12

Principles of Operating Systems CS 446/646 3. Memory Management a. Goals of Memory Management b. Partitioning 9 Fixed partitioning: shelving the boxes 9 Dynamic partitioning: stacking the boxes 9 “Buddy system”: splitting & merging the shelves

c. Linking & Loading d. Simple Paging & Segmentation e. Virtual Memory f.

3/7/2006

Page Replacement Algorithms

CS 446/646 - Principles of Operating Systems - 3. Memory Management

13

3.b Partitioning Fixed partitioning: shelving the boxes

Stallings, W. (2004) Operating Systems: Internals and Design Principles (5th Edition).

(a) Equal-size partitions

(b) Unequal-size partitions

Example of fixed partitioning of a 64MB memory (the “shelves”) 3/7/2006

CS 446/646 - Principles of Operating Systems - 3. Memory Management

14

3.b Partitioning Fixed partitioning: shelving the boxes

¾ Fixed partition establishes fixed boundaries in memory & the oldest and simplest scheme to manage memory space ƒ any process whose size is less than or equal to a partition size can be loaded into an available partition ƒ if all partitions are full, the operating system can swap a process out of a partition ' also the least adequate scheme ƒ larger programs may not fit in any of the partitions, so the programmer must design “overlaying” modules ƒ memory use is very inefficient: even small programs occupy entire partitions, thus wasting space internal to the partitions → this waste of space is called internal fragmentation 3/7/2006

CS 446/646 - Principles of Operating Systems - 3. Memory Management

15

3.b Partitioning Fixed partitioning: shelving the boxes

Stallings, W. (2004) Operating Systems: Internals and Design Principles (5th Edition).

3/7/2006

Placement algorithms for unequal-size fixed partitioning CS 446/646 - Principles of Operating Systems - 3. Memory Management

16

3.b Partitioning Fixed partitioning: shelving the boxes

¾ Placement algorithms for fixed partitioning 9 equal-size partitions ƒ because all partitions are of equal size, it does not matter which partition is used → no special algorithm is needed 9 unequal-size partitions ƒ per-partition queue: to minimize internal fragmentation, processes must wait for a partition that best fits their size ƒ global queue: however, doing so needlessly prevents a process from running while another (bigger) partition might be available → tradeoff between wasting space and wasting time 3/7/2006

CS 446/646 - Principles of Operating Systems - 3. Memory Management

17

3.b Partitioning Dynamic partitioning: stacking the boxes

The effect of dynamic partitioning 3/7/2006

CS 446/646 - Principles of Operating Systems - 3. Memory Management

Stallings, W. (2004) Operating Systems: Internals and Design Principles (5th Edition).

18

3.b Partitioning Dynamic partitioning: stacking the boxes

¾ Dynamic partitioning stacks processes contiguously & also an old and relatively simple allocation scheme ƒ partitions are now of variable length and number ƒ a process is allocated exactly as much memory as required ' but also inadequate for today’s standards ƒ stacking processes will not prevent gaps as processes are continuously swapped in and out of memory → this is called external fragmentation ƒ

3/7/2006

O/S compaction routines can shift processes from time to time, but this is time-consuming in read/write operations and relocation (re-translating references) CS 446/646 - Principles of Operating Systems - 3. Memory Management

19

3.b Partitioning Dynamic partitioning: stacking the boxes

(14M)

Stallings, W. (2004) Operating Systems: Internals and Design Principles (5th Edition).

Before and after allocation of a 16M block under dynamic partitioning 3/7/2006

CS 446/646 - Principles of Operating Systems - 3. Memory Management

20

3.b Partitioning Dynamic partitioning: stacking the boxes

¾ Placement algorithms minimize external fragmentation 9 in order to minimize costly compaction (“garbage-collection” of fragmentation), position the processes cleverly; alternatives are: 9 best-fit placement ƒ

chooses the block that is closest in size to the request, so as to leave the smallest amount of fragmentation

9 first-fit placement ƒ

scans the memory from the beginning and chooses the first available block that is large enough

9 next-fit placement ƒ 3/7/2006

scans the memory from the last placement and chooses the next available block that is large enough CS 446/646 - Principles of Operating Systems - 3. Memory Management

21

3.b Partitioning Dynamic partitioning: stacking the boxes

¾ Placement algorithms: comparative results 9 while performance depends on the exact sequence of process requests and sizes, statistical conclusions can be reached: ' best-fit placement ƒ

paradoxically, the worst performer! it quickly litters memory with small fragments and requires compaction frequently

& first-fit placement ƒ the best and fastest $ next-fit placement ƒ

3/7/2006

the runner-up: slightly worse than first-fit, because it spreads fragmentation more evenly (whereas first-fit has a tendency to preserve big blocks at the end of memory) CS 446/646 - Principles of Operating Systems - 3. Memory Management

22

3.b Partitioning Dynamic partitioning: stacking the boxes

¾ Summary: fixed vs. dynamic partitioning 9 both fixed and dynamic partitioning schemes have drawbacks 9 fixed partitioning ƒ limits the number of active processes ƒ wastes space through internal fragmentation 9 dynamic partitioning ƒ ƒ ƒ

3/7/2006

more complex to maintain wastes space through external fragmentation requires the overhead of compaction

CS 446/646 - Principles of Operating Systems - 3. Memory Management

23

3.b Partitioning “Buddy system”: splitting & merging the shelves

¾ The buddy system: splitting and coalescing

Example of buddy system 3/7/2006

Stallings, W. (2004) Operating Systems: Internals and Design Principles (5th Edition).

CS 446/646 - Principles of Operating Systems - 3. Memory Management

24

Principles of Operating Systems CS 446/646 3. Memory Management a. Goals of Memory Management b. Partitioning 9 Fixed partitioning: shelving the boxes 9 Dynamic partitioning: stacking the boxes 9 “Buddy system”: splitting & merging the shelves

c. Linking & Loading d. Simple Paging & Segmentation e. Virtual Memory f.

3/7/2006

Page Replacement Algorithms

CS 446/646 - Principles of Operating Systems - 3. Memory Management

25

Principles of Operating Systems CS 446/646 3. Memory Management a. Goals of Memory Management b. Partitioning c. Linking & Loading 9 From object codes to executable in memory 9 Loading: binding logical references to physical addresses 9 Linking: weaving logical addresses together

d. Simple Paging & Segmentation e. Virtual Memory f.

3/7-16/2006

Page Replacement Algorithms

CS 446/646 - Principles of Operating Systems - 3. Memory Management

26

3.c Linking & Loading From object codes to executable

¾ Linkers and loaders 9 a linker or “link editor” is a program that takes a collection of object modules (created by compilers or assemblers) and combines them into a single executable program 9 a loader places the linked program in memory, possibly translating (relocating) addresses on the way Memory

program (Compiler or Assembler)

Loader Tanenbaum, A. S. (1999) Structured Computer Organization (4th Edition).

From source code to memory, via translating, linking and loading 3/7-16/2006

CS 446/646 - Principles of Operating Systems - 3. Memory Management

27

3.c Linking & Loading From object codes to executable

Tanenbaum, A. S. (1999) Structured Computer Organization (4th Edition).

Positioning modules before linking and loading 3/7-16/2006

CS 446/646 - Principles of Operating Systems - 3. Memory Management

28

3.c Linking & Loading From object codes to executable resolution of procedure reference & relocation

resolution of address reference & relocation by base shift + module A size = 500

Tanenbaum, A. S. (1999) Structured Computer Organization (4th Edition).

relocation by base shift = 100

Linking and relocating 3/7-16/2006

CS 446/646 - Principles of Operating Systems - 3. Memory Management

29

3.c Linking & Loading Loading: binding logical references to physical addresses

¾ Loading involves binding instructions and data to physical memory addresses 9 once an executable is finished compiling or is stored on disk, the loader places it in memory 9 modern systems allow a user process image to reside in any part of physical memory 9 three approaches to loading: ' absolute loading = binding can be done beforehand, at compile time (writing once) $ relocatable loading = binding is done at load time (rewriting) & dynamic runtime loading = binding is postponed until execution time (not writing) 3/7-16/2006

CS 446/646 - Principles of Operating Systems - 3. Memory Management

30

3.c Linking & Loading Loading: binding logical references to physical addresses

' Absolute loading 9 requires that a given module always be loaded into the same location in memory 9 thus, the compiler can bind symbolic addresses directly to absolute addresses 9 this was the case of the .COM format programs in MS-DOS → not acceptable: prevents swapping and/or multitasking, etc.

Stallings, W. (2004) Operating Systems: Internals and Design Principles (5th Edition).

3/7-16/2006

Absolute load module

CS 446/646 - Principles of Operating Systems - 3. Memory Management

31

3.c Linking & Loading Loading: binding logical references to physical addresses

$ Relocatable loading 9 we need modules that can be located and relocated anywhere in memory 9 for this, the compiler must produce relative addresses 9 then the task of the loader is basically to add one or several fixed offset(s) to all address references 9 problem: swapping in and out requires delocating and relocating every time

Stallings, W. (2004) Operating Systems: Internals and Design Principles (5th Edition).

3/7-16/2006

Relocatable load module

CS 446/646 - Principles of Operating Systems - 3. Memory Management

32

3.c Linking & Loading Loading: binding logical references to physical addresses

& Dynamic runtime loading 9 physical address binding does not happen until the very last moment, when the instruction is executed 9 done by special processor hardware (combined with protection) 9 gives the most freedom Stallings, W. (2004) Operating Systems: Internals and Design Principles (5th Edition).

Hardware support for relocation 3/7-16/2006

CS 446/646 - Principles of Operating Systems - 3. Memory Management

33

Principles of Operating Systems CS 446/646 3. Memory Management a. Goals of Memory Management b. Partitioning c. Linking & Loading 9 From object codes to executable in memory 9 Loading: binding logical references to physical addresses 9 Linking: weaving logical addresses together

d. Simple Paging & Segmentation e. Virtual Memory f.

3/7-16/2006

Page Replacement Algorithms

CS 446/646 - Principles of Operating Systems - 3. Memory Management

34

Principles of Operating Systems CS 446/646 3. Memory Management a. Goals of Memory Management b. Partitioning c. Linking & Loading d. Simple Paging & Segmentation 9 Paging 9 Hardware support for paging 9 Segmentation

e. Virtual Memory f.

3/7-16/2006

Page Replacement Algorithms

CS 446/646 - Principles of Operating Systems - 3. Memory Management

35

3.d Simple Paging & Segmentation Paging

¾ (Sub)divide and conquer 9 new idea: partition process images, too, so that their physical domain doesn’t have to be contiguous anymore ƒ

memory is partitioned into small, equal-size chunks

process images or also subdivided into the same size chunks 9 the chunks of a process are called pages and chunks of memory are called frames ƒ

9 the O/S maintains a page table for each process

3/7-16/2006

CS 446/646 - Principles of Operating Systems - 3. Memory Management

36

3.d Simple Paging & Segmentation Paging

Stallings, W. (2004) Operating Systems: Internals and Design Principles (5th Edition).

3/7-16/2006

CS 446/646 - Principles of Operating Systems - 3. Memory Management

37

3.d Simple Paging & Segmentation Paging

Stallings, W. (2004) Operating Systems: Internals and Design Principles (5th Edition).

3/7-16/2006

CS 446/646 - Principles of Operating Systems - 3. Memory Management

38

3.d Simple Paging & Segmentation Paging

Silberschatz, A., Galvin, P. B. and Gagne. G. (2003) Operating Systems Concepts with Java (6th Edition).

3/7-16/2006

CS 446/646 - Principles of Operating Systems - 3. Memory Management

39

3.d Simple Paging & Segmentation Paging

Silberschatz, A., Galvin, P. B. and Gagne. G. (2003) Operating Systems Concepts with Java (6th Edition).

3/7-16/2006

CS 446/646 - Principles of Operating Systems - 3. Memory Management

40

3.d Simple Paging & Segmentation Paging

Silberschatz, A., Galvin, P. B. and Gagne. G. (2003) Operating Systems Concepts with Java (6th Edition).

3/7-16/2006

CS 446/646 - Principles of Operating Systems - 3. Memory Management

41

3.d Simple Paging & Segmentation Hardware support for paging

Stallings, W. (2004) Operating Systems: Internals and Design Principles (5th Edition).

3/7-16/2006

CS 446/646 - Principles of Operating Systems - 3. Memory Management

42

3.d Simple Paging & Segmentation Hardware support for paging

Stallings, W. (2004) Operating Systems: Internals and Design Principles (5th Edition).

3/7-16/2006

CS 446/646 - Principles of Operating Systems - 3. Memory Management

43

3.d Simple Paging & Segmentation Hardware support for paging

Silberschatz, A., Galvin, P. B. and Gagne. G. (2003) Operating Systems Concepts with Java (6th Edition).

3/7-16/2006

CS 446/646 - Principles of Operating Systems - 3. Memory Management

44

3.d Simple Paging & Segmentation Segmentation

Silberschatz, A., Galvin, P. B. and Gagne. G. (2003) Operating Systems Concepts with Java (6th Edition).

3/7-16/2006

CS 446/646 - Principles of Operating Systems - 3. Memory Management

45

3.d Simple Paging & Segmentation Segmentation Logical

Tanenbaum, A. S. (1999) Structured Computer Organization (4th Edition).

3/7-16/2006

CS 446/646 - Principles of Operating Systems - 3. Memory Management

46

3.d Simple Paging & Segmentation Segmentation

Tanenbaum, A. S. (1999) Structured Computer Organization (4th Edition).

3/7-16/2006

CS 446/646 - Principles of Operating Systems - 3. Memory Management

47

3.d Simple Paging & Segmentation Segmentation

Silberschatz, A., Galvin, P. B. and Gagne. G. (2003) Operating Systems Concepts with Java (6th Edition).

3/7-16/2006

CS 446/646 - Principles of Operating Systems - 3. Memory Management

48

3.d Simple Paging & Segmentation Segmentation

must also have: offset < length

Stallings, W. (2004) Operating Systems: Internals and Design Principles (5th Edition).

3/7-16/2006

CS 446/646 - Principles of Operating Systems - 3. Memory Management

49

3.d Simple Paging & Segmentation Segmentation

Silberschatz, A., Galvin, P. B. and Gagne. G. (2003) Operating Systems Concepts with Java (6th Edition).

3/7-16/2006

CS 446/646 - Principles of Operating Systems - 3. Memory Management

50

3.d Simple Paging & Segmentation Segmentation

Silberschatz, A., Galvin, P. B. and Gagne. G. (2003) Operating Systems Concepts with Java (6th Edition).

3/7-16/2006

CS 446/646 - Principles of Operating Systems - 3. Memory Management

51

Principles of Operating Systems CS 446/646 3. Memory Management a. Goals of Memory Management b. Partitioning c. Linking & Loading d. Simple Paging & Segmentation 9 Paging 9 Hardware support for paging 9 Segmentation

e. Virtual Memory f.

3/7-16/2006

Page Replacement Algorithms

CS 446/646 - Principles of Operating Systems - 3. Memory Management

52