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.