[Operating system] storage model (a)

Basic Concept 1 - Address Relocation (Relocation)

1.1 Introduction

  • Program is loaded into memory before they can run , usually to save the program format executable file on disk
  • Multi-channel programming model that allows multiple programs into the memory
  • Each process has its own unique address space can not be accessed by another process when a process executes its own address space and memory can not be executed as inappropriate operations.

1.2 process address space

Here Insert Picture Description
Since not sure what the process is loaded into memory , it is not possible to calculate the physical address before the process is running, so the process is not the final address of the physical address .

1.3 Address Relocation

1.3.1 concept

Relates to the process address space has logical addresses and physical addresses of the two concepts, the difference in the following table.
表1-1 逻辑地址和物理地址

concept Aliases feature
Logical address Relative address, virtual address After a user is compiled object code form, object code typically in the form of a relative address, which first address is 0, the remaining addresses are relative to the first address addressed. Logical address information can not be read directly in memory
Physical address Absolute address, a real address The memory address of the memory cell, directly addressable

After the concept of logical and physical addresses of clarity, can be further address relocation define: Address relocation also called address mapping, the operating system in order to ensure correctly when the CPU executes the instruction to access memory unit, user needs to be the logical address of the program into a physical address by the machine runs directly addressable .

1.3.2 Static and dynamic relocation relocation

Static relocation: When a user program is loaded into memory, the disposable implementing logical addresses to physical addresses
dynamic relocation: the process execution (i.e., when the instruction by instruction execution ) for address conversion. Implemented as shown below:
Here Insert Picture Description

2 Physical Memory Management

2.1 free memory management

Free memory is divided mainly into two categories: as long division and unequal division.

2.2 Data Structure

**位图:**每个分配单元对应于位图中的一位,0表示空闲,1表示占用。等长划分可用这种数据结构。
**空闲区表(已分配区表):**表中每一项记录了空闲区(或已分配区)的起始地址、长度、标志
空闲块链表

2.3 内存分配算法

表2-1 内存分配算法

算法 特点
首次适配first fit 从表头开始匹配,在空闲表中找到第一个满足进程要求的空闲区
下次适配next fit 从上次找到的空闲区处接着查找
最佳适配best fit 查找整个空闲区表,找到能够满足进程要求的最小空闲区
最差适配worst fit 总是分配满足进程要求的最大空闲区

无论是哪种分配算法,都要实现将空闲区划分为两个部分,一部分供进程使用,另一部分形成新的空闲区

2.4 回收问题

当某一块内存归还后,前后空闲空间合并,修改内存空闲区表,四种情况:上相邻、下相邻、上下都相邻、上下都不相邻。

3 伙伴系统(Buddy System)

核心思想:将内存按照2的幂进行划分,组成若干空闲块链表;查找该链表找到能满足进程需求的最佳匹配块。然后在内存回收阶段,互为伙伴关系的空闲块会合并成一个更大的空闲块。
[Image dump the chain fails, the source station may have security mechanisms chain inserted here, built description!] The proposed picture https: // pass (imblog.-nimg.cn/202002049QDq185857211.png?x-oss-process= image / watermark, type_ZmFuZ3pnaGVpdGk, shadow_10, text_aHR0cHM6LyZibG9nLmNzZG4ubmV0L0NhcnNvbl9DaHU =, size_16, color_FFFFFF, t_50973) (https: //img-blog.csdni mg.cn/20200209185857211.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0NhcnNvbl9DaHU =, size_16, color_FFFFFF, t_70)]

4 基本内存管理方案

4.1 单一连续区

一段时间内只有一个进程在内存中,简单,但是内存利用率低。

4.2 固定分区

  • 把内存空间分割成若干区域,称为分区
  • 每个分区的大小可以相同也可以不同
  • 分区大小固定不变
  • 每个分区装一个且只能装一个进程

4.3 可变分区

根据进程的需要把内存空闲空间分割出一个分区分配给该进程,剩余部分成为新的空闲区(也称为洞)。这些剩余出的空间区碎片也叫外碎片,这些外碎片很多的话则会导致内存利用率低。

碎片问题解决:碎片是指很小且不易被利用的空闲区,碎片过多带来的影响是内存利用率下降。
解决方案是通过内存移动程序将所有小的空闲区合并为较大的空闲区,即压缩技术(memory compaction)

以上三种存储方案都是针对连续内存区域,实际上随着操作系统的不断演进,离散式内存管理方案也得到了不断了发展。离散式主要有三种:页式存储、段式存储、段页式存储。

4.4 页式存储

4.4.1 核心思想

  • 逻辑空间等分为页,并从0开始编号
  • 物理内存空间等分为块,与页面大小相同。从0开始编号
  • 分配内存时,以块为单位将进程中的若干个页分别装入到多个可以不相邻接的物理块中。

4.4.2 地址结构

逻辑地址=页号&页内地址(&号是连接符号,是将页号作为逻辑地址的最高位);
物理地址=块号&块内地址。地址结构如下图所示。
Here Insert Picture Description

4.4.3 地址映射原理及步骤

因为块的大小=页的大小,所以块内地址=页内地址,所以由逻辑地址重定位到物理地址只要求取块号即可。而关于块号的求解,需要用到页表,页表保存了页号和块号的一一对应关系。页式存储地址映射原理如下图所示。
Here Insert Picture Description
1) 在被调进程的PCB中取出页表始址和页表大小,装入页表寄存器;
2) 页号与页表寄存器的页表长度比较,若页号大于等于页表长度,发生地址越界中断,停止调用,否则继续;
3) 由页号结合页表始址求出块号;
4) 块号&块内地址得到物理地址

4.5 段式存储

4.5.1 核心思想

逻辑空间分为若干个段,每个段定义了一组有完整逻辑意义的信息(如主程序段、子程序段、数据段等)。 段的长度由相应的逻辑信息组的长度决定,因而各段长度不等。内存空间为每个段分配一个连续的分区。
完整的逻辑意义信息,就是说将程序分页时,页的大小是固定的,只根据页面大小大将程序分割开;而分段时比较灵活,只有一段程序有了完整的意义才将这一段分割开。(例如将一条1米长的鱼按照定长切分,即为分页,分割长度为50cm则结果是两页;而将鱼分割为(剁椒)鱼头、(红烧)鱼身、(清蒸)鱼尾则是分段,结果为三段。)

4.5.2 地址结构

逻辑地址=段号&段内地址(&号是连接符号,是将段号作为逻辑地址的最高位);
物理地址=基址+段内地址。地址结构如下图所示。
Here Insert Picture Description

4.5.3 地址映射原理及步骤

Here Insert Picture Description
如上图,逻辑地址到物理地址的变换流程包括:
1) 在被调进程的PCB中取出段表始址和段表长度,装入控制寄存器;
2) 段号与控制寄存器的页表长度比较,若页号大于等于段表长度,发生地址越界中断,停止调用,否则继续;
3) 由段号结合段表始址求出基址;
4) 基址+段内地址,即得到物理地址。

4.6 段页式存储

用户程序先分段,每个段内部再分页(内部原理和基本的页式存储、段式存储相同)。
无论是哪种内存管理方案,内存中装载单位都是进程

5 内存扩充技术

针对在较小的内存空间运行较大的进程,主要有以下四种技术可实现内存“扩充”:

  • 内存压缩技术(如上一节的可变分区)
  • 覆盖技术 overlaying
  • 交换技术swapping
  • 虚拟存储技术virtual memory

5.1 覆盖技术

是指程序在执行过程中,程序的不同部分在内存中相互替代。该技术在早期的操作系统中应用的较多。

  • 按照进程自身的逻辑结构,将那些不会同时执行的程序段共享同一块内存区域
  • 要求程序各模块之间有明确的调用结构
  • 由程序员声明覆盖结构,操作系统完成自动覆盖
    覆盖区的简单示例如下图所示。
    Here Insert Picture Description

5.2 交换技术(Swapping)

5.2.1 核心设计思想

在内存空间紧张时,系统将内存中某些进程暂时移到外存,把外存中某些进程换进内存,占据前者所占用的区域(进程在内存与磁盘之间的动态调度)。

5.2.2 实现时遇到的问题

1 哪些内容会被交换到磁盘?
运行时创建或修改的栈和堆的内容
2 在磁盘的什么位置保存被交换出的进程?
交换区:一般系统会指定一块特殊的磁盘区域作为交换空间(swap space),包含连续的磁道,操作系统可以使用底层的磁盘读写操作对其进行高效访问。
3 交换时机?
4 只要不用或者很少再用的进程就被换出;内存空间不够或者有不够 的风险时换出。交换时需要与调度器结合使用。
5 如何选择被交换出的进程?
需要考虑进程的各种属性;不应换出处于等待I/O状态的进程。
6 如何处理进程空间增长?
主要有两种方案,如下所示。
Here Insert Picture Description

发布了40 篇原创文章 · 获赞 15 · 访问量 43万+

Guess you like

Origin blog.csdn.net/Carson_Chu/article/details/104234297