C++中作業系統中的固定(或靜態)分割槽


在本教程中,我們將學習作業系統中的固定分割槽。

固定分割槽是一種管理作業系統中記憶體的方法。這是一種古老的技術。它將記憶體劃分為相等的塊。每個塊的大小是預定義的,不能更改。

該記憶體用於連續的程序。

例子

讓我們看看根據程序大小分配記憶體的示例程式。

 線上演示

#include<iostream>
using namespace std;
int main() {
   int blockNumber = 5, processesNumber = 3;
   int blockSize[5] = {4, 4, 4, 4, 4}, processSize[3] = {1, 2, 3};
   int flags[5], allocation[5];
   for(int i = 0; i < 5; i++) {
      flags[i] = 0;
      allocation[i] = -1;
   }
   // allocating the blocks to processes
   for(int i = 0; i < processesNumber; i++) {
      for(int j = 0; j < blockNumber; j++) {
         if(flags[j] == 0 && blockSize[j] >= processSize[i]) {
            allocation[j] = i;
            flags[j] = 1;
            break;
         }
      }
   }
   for (int i = 0; i < blockNumber; i++) {
      if (flags[i] == 1) {
         cout << "Process " << processSize[allocation[i]] << " is allocated" << endl;
      }
   }
   return 0;
}

輸出

如果你執行上述程式,那麼你將得到以下結果。

Process 1 is allocated
Process 2 is allocated
Process 3 is allocated

結論

如果你在學習本教程時有任何疑問,請在評論區提出。

更新於: 2020 年 12 月 29 日

955 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.