当前位置:   article > 正文

编写C语言程序,模拟实现首次/最佳/最坏适应算法的内存块分配和回收,要求每次分配和回收后显示出空闲分区和已分配分区的情况。假设初始状态下,可用的内存空间为640KB。(江西师范大学软件学院 操作系统)_编写c程序,采用最优适应算法的内存块分配与回收,要求每次分配与回收后显示出空闲

编写c程序,采用最优适应算法的内存块分配与回收,要求每次分配与回收后显示出空闲

【操作系统】分区分配算法 (首次适应算法、最佳适应算法、最坏适应算法)(C语言实现)

为了实现动态分区分配,通常将系统中的空闲分区链接成一个链。所谓顺序查找是指依次搜索空闲分区链上的空闲分区,去寻找一个大小能满足要求的分区。 --------计算机操作系统(第四版)

可变分区也称动态分区,在指作业装入内存时,从可用的内存中划出一块连续的区域分配给他,且分区大小正好等于改作业的大小。

可变分区分配策略:

1.首次适应算法:地址递增,从链首开始

2.最佳适应算法:性能最差,容量递减,浪费最小

3.最坏适应算法:分区大小递减,整合碎片,提高利用率

首次适应算法的话可以不断的去遍历寻找空间是否为空余的。

最佳适应算法的话是要找到最佳适配的空余区域,但是也会导致空闲区被利用之后可能会有一下片内存没被利用,而这小的碎片也很难再次被利用。

最坏适应算法的话是要找到最大空间来分配内存,这样剩余的空间也会最大,这样的话可以更有效的去减少出现小碎片的情况。

分配内存的时候,总是会想到C语言有个malloc函数可以分配内存。所以我写这份作业的时候抱有这是理解malloc函数的成分在里面的。一开始本来是用vector来存放空闲链表,后来觉得要符合底层的话,还是得用纯的c语言来写更好一点。

  1. #include <stdio.h>
  2. #define MEMORY_SIZE 640 // 内存大小(单位:KB)
  3. #define BLOCK_SIZE 1 // 内存块大小(单位:KB)
  4. // 内存块结构体
  5. typedef struct {
  6. int size; // 大小(单位:KB)
  7. int is_free; // 是否空闲
  8. } block_t;
  9. // 内存块数组
  10. block_t memory[MEMORY_SIZE / BLOCK_SIZE];
  11. // 初始化内存块数组
  12. void init_memory() {
  13. int i;
  14. for (i = 0; i < MEMORY_SIZE / BLOCK_SIZE; i++) {
  15. memory[i].size = BLOCK_SIZE;
  16. memory[i].is_free = 1;
  17. }
  18. }
  19. // 显示内存分配情况
  20. void print_memory() {
  21. int i, free_blocks = 0, allocated_blocks = 0, free_size = 0, allocated_size = 0;
  22. printf("\n------------------------------\n");
  23. printf(" Memory Allocation\n");
  24. printf("------------------------------\n");
  25. for (i = 0; i < MEMORY_SIZE / BLOCK_SIZE; i++) {
  26. printf("%d ", i);
  27. if (memory[i].is_free) {
  28. printf("Free ");
  29. free_blocks++;
  30. free_size += memory[i].size;
  31. }
  32. else {
  33. printf("Allocated ");
  34. allocated_blocks++;
  35. allocated_size += memory[i].size;
  36. }
  37. printf("%dKB\n", memory[i].size);
  38. }
  39. printf("------------------------------\n");
  40. printf("Total Blocks: %d\n", free_blocks + allocated_blocks);
  41. printf("Free Blocks: %d\n", free_blocks);
  42. printf("Allocated Blocks: %d\n", allocated_blocks);
  43. printf("Total Size: %dKB\n", free_size + allocated_size);
  44. printf("Free Size: %dKB\n", free_size);
  45. printf("Allocated Size: %dKB\n", allocated_size);
  46. printf("------------------------------\n\n");
  47. }
  48. // 首次适应算法分配内存
  49. int first_fit_allocation(int size) {
  50. int i, j;
  51. int blocks_needed = (size + BLOCK_SIZE - 1) / BLOCK_SIZE; // 需要的块数
  52. for (i = 0; i < MEMORY_SIZE / BLOCK_SIZE; i++) {
  53. if (memory[i].is_free) { // 如果当前块为空闲块
  54. int free_blocks = 0;
  55. for (j = i; j < MEMORY_SIZE / BLOCK_SIZE && memory[j].is_free; j++) {
  56. free_blocks++;
  57. if (free_blocks == blocks_needed) { // 如果找到了足够的空闲块
  58. for (j = i; j < i +
  59. blocks_needed; j++) {
  60. memory[j].is_free = 0;
  61. }
  62. return i; // 返回分配的起始块的索引
  63. }
  64. }
  65. }
  66. }
  67. return -1; // 分配失败
  68. }
  69. // 最佳适应算法分配内存
  70. int best_fit_allocation(int size) {
  71. int i, j;
  72. int blocks_needed = (size + BLOCK_SIZE - 1) / BLOCK_SIZE; // 需要的块数
  73. int best_index = -1, best_size = MEMORY_SIZE / BLOCK_SIZE + 1; // 初始化为无效值
  74. for (i = 0; i < MEMORY_SIZE / BLOCK_SIZE; i++) {
  75. if (memory[i].is_free && memory[i].size >= blocks_needed) { // 如果当前块为空闲块并且大小足够
  76. if (memory[i].size < best_size) { // 如果当前块更小
  77. best_index = i;
  78. best_size = memory[i].size;
  79. }
  80. }
  81. }
  82. if (best_index == -1) { // 分配失败
  83. return -1;
  84. }
  85. else {
  86. for (j = best_index; j < best_index + blocks_needed; j++) {
  87. memory[j].is_free = 0;
  88. }
  89. return best_index; // 返回分配的起始块的索引
  90. }
  91. }
  92. // 最坏适应算法分配内存
  93. int worst_fit_allocation(int size) {
  94. int i, j;
  95. int blocks_needed = (size + BLOCK_SIZE - 1) / BLOCK_SIZE; // 需要的块数
  96. int worst_index = -1, worst_size = -1; // 初始化为无效值
  97. for (i = 0; i < MEMORY_SIZE / BLOCK_SIZE; i++) {
  98. if (memory[i].is_free && memory[i].size >= blocks_needed) { // 如果当前块为空闲块并且大小足够
  99. if (memory[i].size > worst_size) { // 如果当前块更大
  100. worst_index = i;
  101. worst_size = memory[i].size;
  102. }
  103. }
  104. }
  105. if (worst_index == -1) { // 分配失败
  106. return -1;
  107. }
  108. else {
  109. for (j = worst_index; j < worst_index + blocks_needed; j++) {
  110. memory[j].is_free = 0;
  111. }
  112. return worst_index; // 返回分配的起始块的索引
  113. }
  114. }
  115. // 回收内存
  116. void deallocation(int start_index) {
  117. int i;
  118. for (i = start_index; i < MEMORY_SIZE / BLOCK_SIZE && !memory[i].is_free; i++) {
  119. memory[i].is_free = 1;
  120. }
  121. }
  122. int main() {
  123. int choice, size, start_index;
  124. init_memory();
  125. do {
  126. print_memory();
  127. printf("1. First Fit Allocation\n");
  128. printf("2. Best Fit Allocation\n");
  129. printf("3. Worst Fit Allocation\n");
  130. printf("4. Deallocation\n");
  131. printf("0. Exit\n");
  132. printf("Enter your choice: ");
  133. scanf_s("%d", &choice);
  134. switch (choice) {
  135. case 1:
  136. printf("Enter the size to be allocated (in KB): ");
  137. scanf_s("%d", &size);
  138. start_index = first_fit_allocation(size);
  139. if (start_index == -1) {
  140. printf("Memory allocation failed.\n"); } else {
  141. printf("Memory allocated from block %d to %d.\n", start_index, start_index + (size + BLOCK_SIZE - 1) / BLOCK_SIZE - 1);
  142. }
  143. break;
  144. case 2:
  145. printf("Enter the size to be allocated (in KB): ");
  146. scanf_s("%d", &size);
  147. start_index = best_fit_allocation(size);
  148. if (start_index == -1) {
  149. printf("Memory allocation failed.\n");
  150. }
  151. else {
  152. printf("Memory allocated from block %d to %d.\n", start_index, start_index + (size + BLOCK_SIZE - 1) / BLOCK_SIZE - 1);
  153. }
  154. break;
  155. case 3:
  156. printf("Enter the size to be allocated (in KB): ");
  157. scanf_s("%d", &size);
  158. start_index = worst_fit_allocation(size);
  159. if (start_index == -1) {
  160. printf("Memory allocation failed.\n");
  161. }
  162. else {
  163. printf("Memory allocated from block %d to %d.\n", start_index, start_index + (size + BLOCK_SIZE - 1) / BLOCK_SIZE - 1);
  164. }
  165. break;
  166. case 4:
  167. printf("Enter the starting block index to be deallocated: ");
  168. scanf_s("%d", &start_index);
  169. deallocation(start_index);
  170. printf("Memory deallocated from block %d.\n", start_index);
  171. break;
  172. case 0:
  173. printf("退出...\n");
  174. break;
  175. default:
  176. printf("没有这个选项\n");
  177. }
  178. } while (choice != 0);
  179. return 0;
  180. }

因为是用vs写的代码,所以用的是scanf_s。如果换别的编译器的话得改一下。

(大家看完点个赞再走,这个对我真的很重要QwQ)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/625171
推荐阅读
相关标签
  

闽ICP备14008679号