C++記憶體管理中首次適配演算法程式


給定n個程序和m個記憶體塊大小,任務是使用首次適配記憶體管理演算法找到對應程序的最佳適配記憶體塊。

什麼是首次適配記憶體管理演算法?

作業系統使用多種記憶體分割槽演算法來為程序分配記憶體塊,例如:

  • 首次適配演算法
  • 下一次適配演算法
  • 最佳適配演算法
  • 最差適配演算法
  • 快速適配演算法

首次適配演算法是所有演算法中最簡單的記憶體塊分配技術。在這個演算法中,指標跟蹤記憶體中所有空閒塊,並接受為即將到來的程序分配記憶體塊的請求。之後,指標開始搜尋程序的第一個最大空閒塊,並將該記憶體塊分配給即將到來的程序。這樣會建立兩個分割槽,一個用於空閒空間,另一個用於儲存程序。

優點:首次適配演算法能夠最快地為即將到來的程序分配記憶體,因為它將第一個最大適配演算法分配給新程序。

缺點:使用首次適配演算法會導致記憶體浪費,從而導致其他程序缺乏記憶體。

示例

Input-: block_size[] = {300, 50, 200, 350, 70}
process_size[] = {200, 47, 212, 426, 10}
Output-:
Process No. Process Size Block no.
1             200       1
2             47       1
3             212       4
4             426       Not Allocated
5             10       1

以下程式中使用的方案如下

  • 將塊和程序輸入到陣列中
  • 將所有記憶體塊設定為可用
  • 檢查(程序大小) <= (記憶體塊大小),如果成立,則將程序分配到記憶體塊
  • 否則,繼續遍歷其他塊,直到(程序大小) <= (記憶體塊大小) 不成立。

演算法

Start
Step 1->  declare function to calculate the best fit memory block
   void First_Fit(int block_size[], int total_blocks, int process_size[], int total_process)
   Declare int allocation[total_process]
   Call function memset(allocation, -1, sizeof(allocation))
   Loop For i = 0 and i < total_process and i++
      Loop for j = 0 and j < total_blocks and j++
         IF block_size[j] >= process_size[i]
            Set allocation[i] = j
            Set block_size[j] -= process_size[i]
         End
      End
   End
   Loop For i = 0 and i < total_process and i++
      IF allocation[i] != -1
         Set allocation[i] + 1
      End
      Else
         Print Not Allocated
      End
   End
Step 2-> In main()
   Declare an array for blocks as int block_size[] = {300, 50, 200, 350, 70}
   Declare an array for processes int process_size[] = {200, 47, 212, 426, 10}
   Calculate total blocks int total_blocks = sizeof(block_size) / sizeof(block_size[0]
   Calculate total processes int total_process = sizeof(process_size) / sizeof(process_size[0])
   Call First_Fit(block_size, total_blocks, process_size, total_process)
Stop

示例

 線上演示

#include<bits/stdc++.h>
using namespace std;
// Function to allocate memory to  
// blocks as per First fit algorithm
void First_Fit(int block_size[], int total_blocks, int process_size[], int total_process) {
   int allocation[total_process];
   memset(allocation, -1, sizeof(allocation));
   //this for loop wll pick eact process and allocate a first fit block to it
   for (int i = 0; i < total_process; i++) {
      for (int j = 0; j < total_blocks; j++) {
         if (block_size[j] >= process_size[i]) {
            allocation[i] = j;
            block_size[j] -= process_size[i];
            break;
         }
      }
   }
   cout << "\nProcess No.\tProcess Size\tBlock no.\n";
   for (int i = 0; i < total_process; i++) {
      cout << " " << i+1 << "\t\t" << process_size[i] << "\t\t";
      if (allocation[i] != -1)
         cout << allocation[i] + 1;
      else
         cout << "Not Allocated";
         cout << endl;
   }
}
int main() {
   //create array to store block sizes
   int block_size[] = {300, 50, 200, 350, 70};  
    //create array to store process sizes
   int process_size[] = {200, 47, 212, 426, 10};
    //variable total_blocks that contain total number of blocks
   int total_blocks = sizeof(block_size) / sizeof(block_size[0]);
    //variable total_process that contain total number of blocks
   int total_process = sizeof(process_size) / sizeof(process_size[0]);
    //calling the function First_fit
   First_Fit(block_size, total_blocks, process_size, total_process);
   return 0 ;
}

輸出

Process No.    Process Size   Block no.  
1                  200            1  
2                  47             1          
3                  212            4  
4                  426         Not Allocated  
5                  10            1

更新於:2019年12月23日

6K+ 瀏覽量

開啟您的職業生涯

完成課程獲得認證

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