Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 7 Memory Management

Similar presentations


Presentation on theme: "Chapter 7 Memory Management"— Presentation transcript:

1 Chapter 7 Memory Management
Operating System Chapter 7 Memory Management

2 Chapter 7 Memory Management
Goals of memory management subdividing memory to accommodate multiple processes(为支持多道程序将内存进行划分) Memory needs to be allocated to ensure a reasonable supply of ready processes to consume available processor time(内存管理应确保有适当数目的就 绪进程使用处理器时间)

3 Chapter 7 Memory Management
The External View of the Memory Manager

4 Chapter 7 Memory Management
7.1 Memory Management Requirements 7.2 Memory Partitioning 7.3 Paging 7.4 Segmentation 7.5 Summary

5 7.1 Memory Management Requirements
7.1.1 Five requirements 7.1.2 Preparing Program for Execution

6 7.1.1 Five requirements(1/6) 1. Relocation
We need to relocate the process to a different area of memory. Programmer does not know where the program will be placed in memory when it is executed. While the program is executing, it may be swapped(交换) to disk and returned to main memory at a different location (relocated). Memory references(访问) found in the code must be translated to actual physical memory address(物理内存地址).

7 7.1.1 Five requirements(2/6) 1. Relocation
Relocation could raise some technical concerns related to addressing

8 7.1.1 Five requirements(3/6) 2. Protection
Processes should not be able to reference memory locations in another process without permission Normally, processes cannot access any portion of the OS, neither program nor data. Impossible to check absolute addresses at compile time, must be checked at rum time. Memory protection requirement must be satisfied by the processor (hardware) rather than the operating system Operating system cannot anticipate all of the memory references a program will make

9 7.1.1 Five requirements(4/6) 3. Sharing
Allow several processes to access the same portion of memory Share same copy of the program Share data structure to cooperate on some task The memory management system must allow controlled access to shared areas of memory without compromising essential protection.

10 7.1.1 Five requirements(5/6) 4. Logical Organization(逻辑组织)
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

11 7.1.1 Five requirements(6/6) 5. Physical Organization(物理组织)
Memory is organized into at least two levels, referred to as main memory and secondary memory Memory available for a program plus its data may be insufficient(内存对程序和其数据来说可能不足) Overlaying(覆盖) allows various modules to be assigned the same region of memory Programmer does not know how much space will be available and where his/her program will be loaded in memory

12 7.1 Memory Management Requirements
7.1.1 Five requirements 7.1.2 Preparing Program for Execution

13 7.1.2 Preparing Program for Execution(1/9)
Program Transformations Translation (Compilation) Linking Loading

14 7.1.2 Preparing Program for Execution(2/9)
A Sample Code Segment ... static int gVar; int proc_a(int arg) { gVar = 7; put_record(gVar); }

15 7.1.2 Preparing Program for Execution(3/9)
The Relocatable Object module

16 7.1.2 Preparing Program for Execution(4/9)
The Absolute Program

17 7.1.2 Preparing Program for Execution(5/9)
The Program Loaded at Location 4000

18 7.1.2 Preparing Program for Execution(6/9)
Address Binding Assign Physical Addresses = Relocation Static binding Programming time Compilation time Linking time Loading time Dynamic binding Execution time

19 7.1.2 Preparing Program for Execution(7/9)
Static Address Binding Static Binding = At Programming, Compilation, Linking, and/or Loading Time

20 7.1.2 Preparing Program for Execution(8/9)
Dynamic Address Binding Dynamic Binding = At Execution Time

21 7.1.2 Preparing Program for Execution(9/9)
Address Binding How to implement dynamic binding Perform for each address at run time: pa = address_map(la) Simplest form of address_map: pa = la + RR(Relocation Register)

22 Chapter 7 Memory Management
7.1 Memory Management Requirements 7.2 Memory Partitioning 7.3 Paging 7.4 Segmentation 7.5 Summary

23 7.2 Memory Partitioning Fundamental Memory Management Problem
How do we manage applications whose size may be larger than the size of memory available? Partition in blocks and load as necessary

24 7.2 Memory Partitioning Memory sharing schemes Single-program systems
2 partitions (OS/user) Multi-programmed Divide memory into partitions of different sizes Several schemes

25 7.2 Memory Partitioning 7.2.1 Fixed Partitioning
7.2.2 Dynamic Partitioning 7.2.3 Buddy System 7.2.4 Relocation

26 7.2.1 Fixed Partitioning(1/7)
Alternatives for Fixed Partitioning Equal-size partitions(大小相等的分区) Unequal-size partitions(大小不等的分区)

27 7.2.1 Fixed Partitioning(2/7)
Equal-size partitions: features Any process whose size is less than or equal to the 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

28 7.2.1 Fixed Partitioning(3/7)
Limitations of fixed partitions A program may be too big to 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. internal fragmentation(内部碎片).

29 7.2.1 Fixed Partitioning(4/7)
Unequal-size partitions(大小不等的分区) Both of these problems of equal-size partitions can be lessened, though not solved, by using unequal-size partitions.

30 7.2.1 Fixed Partitioning(5/7)
How to assign processes to partitions Placement Algorithm Equal-size partitions trivial Unequal-size partitions Queue for each partition: Some partitions may be unused Single Queue: More complex, but more flexible Choose the one that fits the best

31 7.2.1 Fixed Partitioning(6/7)

32 7.2.1 Fixed Partitioning(7/7)
Disadvantages of Fixed Partitions The number of active processes in the system is limited by the number of partitions Small jobs will not utilize partition efficiently

33 7.2 Memory Partitioning 7.2.1 Fixed Partitioning
7.2.2 Dynamic Partitioning 7.2.3 Buddy System 7.2.4 Relocation

34 7.2.2 Dynamic Partitioning(1/10)
Memory not partitioned a priori Each request is allocated portion of free space Memory = Sequence of variable-size blocks Some are occupied, some are free (holes) External fragmentation occurs Adjacent holes (right, left, or both) must be coalesced to prevent increasing fragmentation

35 7.2.2 Dynamic Partitioning(2/10)

36 7.2.2 Dynamic Partitioning(3/10)
Placement algorithms 1. First-fit algorithm(首次适配) Scans memory from the beginning and chooses the first available block that is large enough Simplest and usually fastest and best May have many process loaded in the front end of memory that must be searched over when trying to find a free block

37 7.2.2 Dynamic Partitioning(4/10)
Placement algorithms 2. 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

38 7.2.2 Dynamic Partitioning(5/10)
Placement algorithms 3. Worst-fit algorithm(最坏适配) Worst Fit allocates a process to the partition which is largest sufficient among the freely available partitions available in the main memory.

39 7.2.2 Dynamic Partitioning(6/10)
Placement algorithms 4. Next-fit(邻近适配) Scans memory from the location of the last placement and chooses the next available block that is large enough 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

40 7.2.2 Dynamic Partitioning(7/10)

41 7.2.2 Dynamic Partitioning(8/10)
Linked List Implementation All available space tied together through a linked list(空闲链表) Type/Size tags at the start of each Block Checking neighbors of released block b

42 7.2.2 Dynamic Partitioning(9/10)
Bitmap Implementation Memory divided into fix-size blocks Each block represented by a 0/1 bit in a binary string: the“bitmap a) memory with 5 processes and 3 free blocks b) the bitmap (1: occupy 0: free) c) represent by free linked list

43 7.2.2 Dynamic Partitioning(10/10)
Bitmap Implementation Operations use bit masks Release: B[i] = B[i] & ' ' Allocate: B[i] = B[i] | ' ' Search: Repeatedly, Check left-most bit and Shift mask right: TEST = B[i] & ' '

44 7.2 Memory Partitioning 7.2.1 Fixed Partitioning
7.2.2 Dynamic Partitioning 7.2.3 Buddy(伙伴) System 7.2.4 Relocation

45 7.2.3 Buddy System(1/3) Compromise between fixed and variable partitions Fixed number of possible hole sizes; typically, 2i Each hole can be divided (equally) into 2 buddies. Track holes by size on separate lists When n bytes requested, find smallest i so that n≤2i If hole of this size available, allocate it; otherwise, consider larger holes. Recursively split each hole into two buddies until smallest adequate hole is created Allocate it and place other holes on appropriate lists On release, recursively coalesce buddies Buddy searching for coalescing can be inefficient

46 7.2.3 Buddy System(2/3)

47 7.2.3 Buddy System(3/3)

48 7.2 Memory Partitioning 7.2.1 Fixed Partitioning
7.2.2 Dynamic Partitioning 7.2.3 Buddy System 7.2.4 Relocation

49 7.2.4 Relocation(1/5) Why?? When program loaded into(载入) memory the actual (absolute) memory locations are determined A process may occupy different partitions which means different absolute memory locations during execution (from swapping,交换) Compaction(压缩) will also cause a program to occupy a different partition which means different absolute memory locations

50 7.2.4 Relocation(2/5) Addresses Logical Address(逻辑地址)
Reference to a memory location independent of the current assignment of data to memory(与当前数据在内存 中的分配无关的访问地址) Translation must be made to the physical address Relative Address(相对地址): Address expressed as a location relative to some known point Physical Address(物理地址) The absolute address(绝对地址) or actual location in main memory

51 7.2.4 Relocation(3/5) 相对地址 基址寄存器 加法器 绝对地址 界限寄存器 比较器 给操作系统的中断 进程映像

52 7.2.4 Relocation(4/5) Registers Used during Execution
Base register(基址寄存器) Starting address for the process Bounds register(界限寄存器) Ending location of the process These values are set when the process is loaded(加载) or when the process is swapped in(换入)

53 7.2.4 Relocation(5/5) Registers Used during Execution
The value of the base register is added to a relative address to produce an absolute address The resulting address is compared with the value in the bounds register If the address is not within bounds, an interrupt is generated to the operating system

54 Chapter 7 Memory Management
7.1 Memory Management Requirements 7.2 Memory Partitioning 7.3 Paging 7.4 Segmentation 7.5 Summary

55 7.3 Paging 7.3.1 Dealing with Insufficient Memory
7.3.2 Paging scheme in OS

56 7.3.1 Dealing with Insufficient Memory(1/3)
Memory compaction How much and what to move? Swapping Temporarily move process to disk Requires dynamic relocation Overlays Allow programs large than physical memory Programs loaded as needed according to calling structure.

57 7.3.1 Dealing with Insufficient Memory(2/3)
Memory compaction

58 7.3.1 Dealing with Insufficient Memory(3/3)
Overlays Allow programs large than physical memory Programs loaded as needed according to calling structure

59 7.3 Paging 7.3.1 Dealing with Insufficient Memory
7.3.2 Paging scheme in OS

60 7.3.2 Paging scheme in OS(1/5) Partition memory into small equal fixed-size chunks(块) which are called frames(帧) Divide each process into small equal fixed-size chunks which are called pages(页). The size of pages are equal to the size of frames 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

61 7.3.2 Paging scheme in OS(2/5) 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

62 7.3.2 Paging scheme in OS(3/5) Paging Example

63 7.3.2 Paging scheme in OS(4/5) Assignment of Process Pages to Free Frames

64 7.3.2 Paging scheme in OS(5/5) Page Tables for Example

65 Chapter 7 Memory Management
7.1 Memory Management Requirements 7.2 Memory Partitioning 7.3 Paging 7.4 Segmentation 7.5 Summary

66 7.4 Segmentation(1/5) Segmentation is a technique that partitions memory into logically related data units Module, procedure, stack, data, file, etc. Virtual addresses become <segment #, offset> Units of memory from user's perspective Segmentation = many segments/process Hardware support Multiple base/limit pairs, one per segment (segment table) Segments named by #, used to index into table

67 7.4 Segmentation(2/5)

68 7.4 Segmentation(3/5)

69 7.4 Segmentation(4/5)

70 7.4 Segmentation(5/5) Segmentation and Paging
Can combine segmentation and paging The x86 supports segments and paging Use segments to manage logically related units Module, procedure, stack, file, data, etc. Segments vary in size, but usually large (multiple pages) Use pages to partition segments into fixed size chunk Makes segments easier to manage within physical memory

71 Chapter 7 Memory Management
7.1 Memory Management Requirements 7.2 Memory Partitioning 7.3 Paging 7.4 Segmentation 7.5 Summary


Download ppt "Chapter 7 Memory Management"

Similar presentations


Ads by Google