1.57k likes | 1.58k Views
Chapter 3. Memory Management. Content. Why need memory management Memory partitioning Paging Segmentation. 3.1 Memory Management Requirements (p289). Relocation( 重定位 ) Memory Protection (保护) Memory Sharing (共享) Logical Organization (逻辑组织) Physical Organization (物理组织).
E N D
Chapter 3 Memory Management
Content Why need memory management Memory partitioning Paging Segmentation
3.1 Memory Management Requirements (p289) • Relocation(重定位) • Memory Protection(保护) • Memory Sharing(共享) • Logical Organization(逻辑组织) • Physical Organization(物理组织)
Relocation (重定位p290) • The processor and OS translate the memory references found in the code of the program into actual physical memory addresses, which reflecting the current location of the program in main memory.(程序代码中地址引用翻译为主存中当前地址的过程)
Relocation (p290) • Static relocation(静态,一次性重定位) • Relocate when compile program or load in program. • Dynamic relocation(动态,可多次重定位) • Relocate when running program. • Contents for relocation(定位内容) • Branch instruction(分支指令) • Reference to data (数据引用)
Memory Protection (p291) • Each process should be protected against unwanted interference(访问) by other processes. • All memory references generated by a process must be checked(检查) at run time. • The memory protection requirement is satisfied(完成) by the processor (hardware) rather than the OS (software).
Memory Sharing(共享p291) • Share data and code for save(节省) the memory. • The memory-management system must allow controlled access to shared areas of memory without compromising(影响) essential protection.
Logical Organization(p291) • OS should deal with programs in the form of modules(模块) • Modules can be written and compiled independently. • Different degrees of protection can be given to different modules. • It is easy to implement share. • E.g. segmentation(分段管理)
Physical Organization(p292) • Computer memory is organized(组织) into at least two levels, referred to as main memory and secondary memory. • Swapping the modules in and out as needed. • The programmer does not know how much space will be available or where that space will be.
Loading\ Linking (P311) • Create a process image(f-7.13) • A loading scenario(f-7.14)
A loading Scenario (Fig 7.14) Library Module#1 Load Module Loader Linker RAM Module#2 Module#n
LoadingType(p313) • Absolute loading • Relocatable loading • Dynamic run-time loading
Absolute Loading • Absolute address(F7.15b). • Always be loaded into the same location. • The assignment of specific address values to memory reference(内存引用的地址变换) be done by programming or compile time. • Not transparent to programmer. • All code in a program must be recompiled after modified.
Relocatable Loading • Relative address(引用相对地址)(F7.15c) • No need always load into same location. • Need a relocation dictionary(重定位表) in loading modules (generated at compile time).
Dynamic Run-Time Loading • Can support swap the process. • Done by processor hardware for not degrade performance.(F7.8)
Loading (table 7.2a p314) • Address Binding(绑定) Time • Programming time • Compile time • Load time • Run time
Linking(F7.16) • Linking time(table 7.2b) • Programming time • Compile or assembly time • Load module creation • Load time • Run time
Type of Link • Static linking • Compile time • Link to a executable modules • Dynamic linking • Loading time • Easy to upgrade modules and need not source code of modules to upgrade.(模块升级不需要联编)
Type of Link • Easy to share. • High efficient to use main memory. • Running time • Can move process when it running. • Can defer the loading time of a module to the time that must run it.
Memory Management Techniques(p292) • Fixed Partitioning(固定分区) • Dynamic Partitioning(动态分区) • Simple Paging(简单分页) • Simple Segmentation(简单分段) • Virtual-Memory Paging(虚拟分页) • Virtual-Memory Segmentation(虚拟分段)
3.2 Memory Partitioning(p292) • Fixed Partitioning • Dynamic Partitioning • Buddy System(伙伴系统)
Fixed Partitioning(p294) • Main memory is divided into a number of static partitions at system generation(装入) time. A process may be loaded into a partition of equal or greater size. • The partitions can be equal size or not.
Fixed Partitioning • Equal size partitions(固定等大小) • Inefficient to use main memory, generating internal fragmentation. • Big program can not be loaded into partition. • The size of partition not equal(固定不等大小) • Can alleviate(缓解)the disadvantage.
Placement (放置) Algorithm(p295) • If the partitions is equal size. Just choose a free partitions to place a process. • If the partitions is not equal size, using the following tow methods: • To guarantee the choosing partition can be the best suitable size one(最佳). • To guarantee the choosing partition can contain the correspond process.
Adv. of fixed partition • Simple to implement • System spending(开销) is small.
Disadv. of Fixed Partition(p296) • Having internal fragment(内碎片), inefficient to use main memory. • Can not run big size program. • The number of process is fixed by the number of partition.
Dynamic Partitioning(动态分区p296) • The number and size(数目和大小) of partition is change while the processes are in and out from main memory.
External Fragment(外碎片p298) • When a process is loaded in, system may choose a suitable partition, and no internal fragment appear. But external fragment will appear.
External/Internal fragmentation • Internal: There is wasted space internal to a partition due to the data loaded is smaller than it.(由于数据和分区大小不完全匹配引起的) • External: The small partition that can not allocate to any process.(不能分配给任何进程的小分区)
Compaction(紧凑p298) • OS shifts(移动) the processes so that they are contiguous(相邻) and all of the free memory is together in one block. • To implement compaction, the dynamic reallocate(动态重定位) technology must be used.
Placement Algorithm(p298) • Best-Fit Algorithm(最优) • First-Fit Algorithm(首次) • Next-Fit Algorithm(下次/循环首次)
Replacement Algorithm(p299) • Swapping process(以进程为单位交换) to avoid wasting processor time waiting for an active process to become unblocked. • Active process is in RAM.
Buddy System(伙伴系统) • A dynamic partition algorithm which need not use compaction(紧凑). • 2U is the size of the entire memory(全部内存) available for allocation. To begin, the entire space available for allocation is treated as a single block size of 2U .
Buddy System • If a request of size k such that 2i-1<k ≤ 2i is made, a block size of 2i will be allocated as follows: 1、if i >U, the request is larger than the whole main memory, return fail; 2、if the list of 2i is null, that means there is no free partition size of 2i,so:
Buddy System ⑴ Find a free partition size of 2i+1, if not find, change i to i+1 and call the algorithm again; ⑵ Split(分裂) the partition size of 2i+1 into two buddy partitions of size 2i; ⑶ Insert the two buddy partitions of size 2i into the free list size of 2i; 3、Get the first free partition in the free list size of 2i,return.
Buddy System Void * get_hole(i) { if (i > U) return null; if (i _list==null) { tmp=get_hole(i+1); split_hole(tmp,tmp1,tmp2); insert_hole(i,tmp1,tmp2); } return first hole on i_list; }
Buddy System:Partition Release(释放分区) • When release a partition size of 2i, if its buddy is not free, then insert it into free list size of 2i. • Otherwise • Delete the buddy from the free list size of 2i; • Merge(融合) the release partition and the buddy partition to a partition size of 2i+1; • Call the release algorithm again.
Buddy System • Internal fragmentation existing • No external fragmentation
Data Structure of Partition Management(补充) • Partition table(分区表) • Table entry(表项) • Size、start address、allocate id • Single list • Multi-list • Free list • Allocate list
Data Structure of Partition Management • Operation(操作) of partition table • Insert • Modify • Merge and split
Relocation(重定位) of Partition Management(p302) • Physical Address(物理地址) • Absolute address(绝对地址). An actual location in main memory. • If a process does not move during its lifeline(进程不移动), the physical address can be used as the command or data location to be accessed.
Relocation of Partition Management • If using swapping or compaction technology (virtual memory or dynamic partition management), a process may be assigned to a different partition in its lifeline(生存期). • In the cases, logical address (relative address) may be used.