Storage management operating system of _2019-11-20

Storage Management

 Early computer programming does not require too much storage management

 As computers become more complex and procedures, storage management becomes necessary

 Divided into continuous and non-continuous memory management in the storage, the storage is divided into non-contiguous memory pages, the page segment memory and storage segment.

 

 Storage management purposes:

  Make sure your computer has enough memory to process data

  Ensure that the program can be obtained from the use of part of the memory available memory

  Ensure that the program memory can be returned after use for other programs

 Memory allocation and recovery

  Memory allocation process

   A single continuous distribution:

    The system memory is divided into regions (the system being used) and user areas

    Can only be used in single-user operating system, in a single process

 

   Fixed partition allocation:

    Memory space is divided into a plurality of areas of a fixed size, each partition uses only to a non-interfering

    Support multi-channel program of the easiest storage allocation

 

    Dynamic partition allocation (the most common method of memory allocation):

    Process according to actual needs, dynamic allocation of memory space

    Related data structures (computer storage, organized data ):

     (1) dynamic partitioning idle table data structure

       Storage usage, is used or, if of sufficient capacity

       
     (2) dynamic partitioning idle chain data structure

       All nodes connected to the free end to end, forming a doubly linked list

       A continuous free area can be combined into one node to reduce the number of nodes

       

    Dynamic partitioning related allocation algorithm:

     First fit algorithm (FF algorithm):

      The main use of free chain data structure, from the head node adapted to start until you find the capacity to meet the needs of the size of the free area

      Each time starting from the head so that the head address space continue to be divided, if the head have been assigned, you need to find the node is on to find the right memory

      To improve this situation, there has been circulating adaptation algorithm, each time from the end of the first retrieval position to start looking.

 

     Best-fit algorithm (BF algorithm):

      Best-fit algorithm requires an empty area list sorted by capacity size ,

      Traverse the list to find the best suitable free zone free zone, from small to large to traverse, you can reduce the generation of debris, to avoid overkill. 

 

     Fast adaptation algorithm (QF algorithm):

      Fast adaptation algorithm requires more than an empty area list

      List storing one empty area is left in each spare area, the efficiency is higher than BF algorithm, you can quickly find a suitable memory area.

      

  Memory recovery process

   For different four cases, there are four ways of recycling. 

   

    ① ② situation situation situation situation ③ ④

   Case ①:

    You do not need to create a new free list node

    Simply put capacity of a free area of ​​the space area can be increased to 

   Case ②:

    The recovery area combined with the free area

    New free zone using the address recovery zone 

   The case ③:

    The spare area 1, and spare area merging recovery zone 2

    New free zone using the address of a free zone 1 

   Case ④:

    Create a new free node recovery area

    Inserted into the respective linked list of free area

 Section on page storage management

  Operating system process management space, space in the general process is not mapped to physical memory, virtual memory is not only in auxiliary storage.

  Paged memory management

   Word block: definition of relative physical devices (physical memory), a group of words as a processing unit called "block"
   Page: Definition of relative logical space
 

   Management process:

    The process is divided into several logical space size pages, corresponding to the physical memory space divided into a physical block page size

    Units of pages in the process space loaded into the physical memory dispersed physical block , the process space in each logical page into physical memory blocks go

 

   Memory fragmentation:

    Process space page size is smaller than the size of physical memory free area, there is some cause can not be used nor recovered memory area.

    Page size should be moderate, difficult to allocate too large, too small, too memory fragmentation, multiple page size is typically 512B ~ 8K.

 

    

   Page table:

    Logical space and physical space of the recording process of mapping table, because the process is divided into logical space of the page

    Dispersed in main memory plane different physical blocks dispersed in, it is necessary to record the page table.

    Page table how many pages there are that many page table entries.

    

    The logical address structure:

    By the page number and page offset P W (the page address) of two parts

 

    Related formula:

     The logical address of the logical address A = P * page size page number L + W is offset ( "in-page address" also known as "page offset")

     Physical block number B specifies a program address A = * page size page offset L + W

     W = the logical address offset A - page number L = P * page size logical address A% (mod) page size L 

 

    Seeking a physical address the logical address is known:

     The first step: the logical address of A / L = size of the logical page address page number P (divisible) (supplier)

     Step two: the logical address A% (mod) page offset W (remainder) page size of the logical address L =

     The third step: generating a page table, the page frame number to find the page corresponding to the logical address, correspondence, block number corresponding to page number, from small to large.

         In practice, since the virtual memory is usually larger than the actual memory, the number of pages is typically equal to or greater than the number of word blocks.

      Such as:

       

 

      Step Four: obtaining a physical address of a program designated block number = page page size + offset * W

 

   Multi-level page table:

    In order to address the page table memory space occupied and too appear, due to modern large computer memory, there can be a lot of pages, the page table entry would be very Pang

    Large, a 32-bit system, if the specified page is 4KB, then there will be 2 ^ 20 page table entry, if each page table entry occupied 1byte, each process page table will account for only

    用1M的内存空间,而且每个页表都将会有所有的页表项来对物理内存空间进行映射。

    如果是在64位地址的计算机的情况下,如果按照相同的计算方法,一个进程的页表占用的空间一个普通内存根本放不下。

    关键是,一个进程,真的会需要一整个虚拟地址空间去存放吗?

    解决问题的核心在于,避免把全部页表一直保存在内存中是多级页表的关键所在。特别是那些不需要的页表就不应该保留。

 

    https://blog.csdn.net/forDreamYue/article/details/78887035 多级页表就比一级页表要省空间?

 

    工作原理:

     根页表中,每个字块所指向的地址为内存中的一片空间,这一片空间存储的是二级的页表,假设每个

     二级页表有1024项,每一项指向的字块才是内存实际使用的内存。每个根页表可以指向多个二级页表。

     通过一个顶级页表为真正有用的页表提供索引。

     

  段式存储管理

   可以解决也是存储管理中的有一段连续的逻辑分布在多个页面中,在使用时将会大大降低执行效率的问题

 

   与页式存储管理的异同:  

    段式存储和页式存储都离散地管理了进程的逻辑空间

    页是物理单位,段是逻辑单位;

    分页是为了合理利用空间,分段是满足用户要求

    页大小由硬件固定,段长度可动态变化

    页表信息是一维(字块号)的,段表信息是二维(基址和段长)的

 

   实现:

    将进程逻辑空间划分成若干段(非等分

    段的长度由连续逻辑的长度决定,如:主函数MAIN、子程序段X、子函数Y等 

 

   段表:

    

  段页式存储管理

   分页可以有效提高内存利用率(虽然说存在页内碎片);

   分段可以更好满足用户需求,逻辑是用户写的;

   两者结合,形成段页式存储管理。

 

   实现:

    先将逻辑空间按段式管理分成若干段

    再把段内空间按页式管理等分成若干

    

    

  虚拟内存  

   虚拟内存实际是对物理内存的补充,速度接近于内存,成本接近于辅存。

 

   程序的局部性原理:

    局部性原理是指CPU访问存储器时,无论是存取指令还是存取数据,所访问的存储单元都趋于聚集在一个

    较小的连续区域中;

    根据此原理,程序运行时,无需全部装入内存,装载部分即可,如果访问页不在内存,则发出缺页中断,发起页面置换

    从用户层面看,程序拥有很大的空间,即是虚拟内存。

 

   使用虚拟内存的背景:

    ① 有些进程实际需要的内存很大,超过物理内存的容量;多道程序设计,使得每个进程可用物理内存更加稀缺

    不可能无限增加物理内存,物理内存总有不够的时候。

    ② 虚拟内存是操作系统内存管理的关键技术;使得多道程序运行和大程序运行成为现实;把程序使用内存划分,

    可以将部分暂时不使用的内存放置在辅存,只需把要使用的(红色)加载到物理内存中,

    如下:

     

   虚拟内存的置换算法  

    替换策略发生在Cache-主存层次、主存-辅存层次

    Cache-主存层次的替换策略主要是为了解决速度问题

    主存-辅存层次主要是为了解决容量问题,一般虚拟内存的置换指的是这个层次。

 

    主存页面的替换时机:

     

 

    高速缓存的替换时机:

      

  Linux的存储管理

   内存碎片:

    页内碎片:

     内部碎片是已经被分配出去(能明确指出属于哪个进程)的内存空间大于请求所需的内存空间,不能被利用的

     内存空间就是内部碎片。 空闲区容量大于页面大小,则造成业内碎片。

     

 

    页外碎片:

     外部碎片是指还没有分配出去(不属于任何进程),但是由于大小而无法分配给申请内存空间的新进程的内存空闲块。

     操作系统在不断分配空闲区,会根据进程的需要来分配不同的内存空间给进程使用,会造成一些较小的空闲区由于过小而

     无法被继续分配,造成页外碎片。

     

   Buddy(伙伴)内存管理算法

    Buddy算法是经典的内存管理算法,一种实际在内存中运行的一种内存管理算法,

    基于计算机处理二进制的优势具有极高的效率,主要是为了解决内存外碎片的问题,

    可以把内存外碎片问题转变为内存内碎片问题,把内存按2的幂次方大小来分配,可以避免出现内存外碎片。

    目的是努力让内存分配和相邻内存合并能快速进行。

 

    伙伴系统:

     “伙伴”指的是内存的“伙伴”;

     一片连续内存的“伙伴”是相邻的另一片大小一样的连续内存。

     

 

    Buddy实现:

     Buddy内存分配原则:      

      在每次分配内存时,都是默认向上取整为2的幂的大小

      如:

       70k→128k

       129k→256k

       666k→1024k

 

     实现过程:

      创建一系列空闲块链表,每一种都是2的幂,如容量大小为2^1=1的为一条双向链表,2^2、2^3 ... 以此类推。

      

      例子:

      假设存储空间有1M大小(为1M的空闲链表中有一个节点),按Buddy算法分配100k内存,这组空闲链中情形如下图:

       

      1. 100k向上取2的幂=128k

      2. 查询是否存在大小为128k的空闲内存块(看看表示128KB空闲区的链表上有没有节点)?

      3. 没有!查询是否有256k空闲内存块?

      4. 没有!查询是否有512k空闲内存块? 

      5. 没有!查询是否有1M空闲内存块?

      6. 有,摘下1M空闲内存块,分配出去 

      7. 拆下512k放在512k的空闲链表(由1M=2^10降幂1,得512KB *2,未得到所需的128KB),其余的分配出去 

      8. 拆下256k放在256k的空闲链表,其余的分配出去

      9. 拆下128k放在128k的空闲链表,其余的分配出去

      10. 分配完毕(最小只能分配给128KB大小的空闲区,若无则分配更大的空闲区供使用)

      

   Linux交换空间

    交换空间(Swap)是磁盘的一个分区, Linux物理内存满时,会把一些内存交换至Swap空间,但是交换空间在磁盘,速度较慢。

    Swap空间是初始化系统时配置的。

     冷启动内存依赖,很多大型的应用程序,在启动时需要使用大量的内存,但是这些内存在启动后就很少会被使用,可把

     这部分置换到交换空间里,以释放更多的物理内存给系统使用。

     系统睡眠依赖,当linux需要睡眠时,会把内存中所有的数据保存到swap空间里,下次启动时才重新加载到内存中,这样

     可以加快系统的启动速度。

     大进程空间依赖,对于一个应用确实需要使用十分大的物理内存空间,当物理内存不足时,把进程需要使用的内容暂时保存

     到swap交换空间中。

 

    与虚拟内存对比:

    

Guess you like

Origin www.cnblogs.com/pfcz1/p/11900401.html