12. 記憶體管理(Memory Management)

zhudachang發表於2024-04-30

記憶體管理目標

  1. MAIN MEMORY
  • Main memory(主記憶體) is central(中心) to the operation(運作) of a modern computer system.
  • Memory consists of a large array of bytes(一大組位元組資料或者位元組序列),each with its own address(地址).(記憶體是由一大組位元組組成,每一個位元組都有自己的地址)。
  • The CPU fetches(獲取 ) instructions from memory according to the value of the program counter(PC).These instructions may cause additional loading from and storing to specific memory addresses.(這些指令可能會導致從特定記憶體地址載入和儲存額外的資料。)
  • A typical instruction-execution cycle, for example ,first fetches an instruction from memory.The instruction is then decoded and may cause operands to be fetched from memory.After the instruciton has been executed on the operands on the operands ,results may be stored back in memory.(典型的指令執行週期,例如,首先從記憶體中取出一條指令。然後對指令進行解碼,可能需要從記憶體中取出運算元。在對運算元執行了指令後,結果可能會被存回記憶體。)
  1. 快取記憶體cache
  • cache
    • 第一次:cpu讀取發現cache
    • 第二次:cpu發現cache,那麼就會去讀記憶體
  • 現在有三級快取,每一個單核中有兩個cache,然後多核之外還有一個cache
  1. 保護作業系統和使用者程序
  • 使用者程序不可以訪問作業系統記憶體資料,以及使用者程序空間之間不能互相影響(使用者程序空間之間不能互相影響,有些惡意軟體,去干擾其他聊天軟體的資料)
    • 透過硬體實現, 因為作業系統一般不干預cpu對記憶體的訪問
      • base register:基址暫存器
      • limit register:限長暫存器
    • 上述兩個暫存器的值只能被作業系統的特權指令載入,然後限制只可以訪問兩個暫存器之間的記憶體空間去讀取
  1. 記憶體管理目標

邏輯地址和實體地址

  1. 地址空間和地址轉換
  2. 地址轉換時機
  • 一個程式需要進行邏輯地址轉換到實體地址,應該在run time這個時機比較好。
  • 沒有邏輯地址轉向實體地址重新計算的煩惱。
  1. 記憶體管理單元MMU
  • Memory-Management Unit 完成邏輯地址到實體地址‘執行時’的轉換工作
    • 重定位暫存器(relocation register)或基址暫存器

連續記憶體分配

  1. contiguous memory allocation(連續記憶體分配)
  • In contiguous memory allocation,each process is contained in a single section of memory that is contiguous to the section containing the next process.(在連續記憶體分配中,每個程序都包含在一個塊中與包含下一個程序的部分相鄰的記憶體部分中。)
    • Memory allocation(記憶體分配)
    • Memory recycle(記憶體回收)
    • Memory protection(記憶體保護)
  1. fixed-sized partition(固定大小分割槽)
  • Memory is divided(分開) to several(幾個) fixed-sized partitions(分割槽) Each partition may contain exactly one process.(記憶體被分成幾個固定大小的分割槽。每個分割槽可能只包含一個程序。)
  • 一個程序佔用一個分割槽,程序再執行的時候會把虛擬地址透過mmu將分割槽的起始地址加偏移地址轉換為實體地址。
  • 會有一個表,記錄每一個區域的起始地址,長度,並記錄是否有程序佔有它。
  • 這個管理方案是早期的管理方案,不靈活。
  1. variable-partition(可變分割槽)
  • In the variable-partition scheme(可變分割槽方案),the operating system keeps two tables indicating(指令) which parts of memory are available and which are occupied.(在可變分割槽方案中,作業系統會維護兩個表,指示哪些記憶體空間是可用的,哪些是被佔用的。)
  • Initially,all memory is available for user processes and is considered(被考慮) one large block of available memory ,a hole(孔洞).(最初,所有記憶體都可供使用者程序使用,被視為一大塊可用記憶體,一個孔洞。)
  • Eventually ,as you will see ,memory contains a set of holes of various sizes.(最終,正如你將會看到的那樣,記憶體中包含著各種大小的空洞。)
  1. 動態儲存分配問題
  • 後面兩個演算法需要遍歷所有空洞。
  1. 地址轉換與保護
  • 兩種連續分配方案的地址轉換方式是相似的:
    • 實體地址= 基址+ 邏輯地址
  • 地址保護策略:與無限長limit進行比較
  • 大致流程:與limit register進行比較如果是yes,那麼就把邏輯地址轉換為實體地址(透過基址),如果是no那麼就會報錯addressing error.
  1. 碎片
  • Fragmentation:some little pieces of memory hardly to be used.(碎片化:一些幾乎無法使用的小記憶體片段。)
    • internal fragmentation(內部碎片,是對固定大小分割槽的情況的)
    • external fragmentation(外部碎片,是對可變分割槽)
      • compaction(壓縮)
        • static relocation(考慮地址轉換是否是動態的,只有是執行時候地址轉換的方案才可以使用這個方案)
        • cost(成本,考慮需要額外的空間和時間)
  1. 可變分割槽和固定分割槽這兩種方案已經被放棄掉了,請看下一章

筆記來自b站
【【Linux作業系統】Lecture 12 Memory Management】https://www.bilibili.com/video/BV1gE411F74T?vd_source=7183d94355e25d7a846a2242f0eb9c7d
感謝老師的網課

相關文章