动态分区分配方式的模拟c语言代码
时间: 2023-10-15 14:04:02 浏览: 247
以下是一个简单的动态分区分配方式的模拟c语言代码:
```c
#include<stdio.h>
int main() {
int n, m, i, j, flag = 0;
int size[20], memory[20], alloc[20];
printf("Enter the number of memory blocks: ");
scanf("%d", &n);
printf("Enter the size of each memory block:\n");
for(i = 0; i < n; i++) {
scanf("%d", &size[i]);
memory[i] = i+1;
}
printf("Enter the number of processes: ");
scanf("%d", &m);
printf("Enter the size of each process:\n");
for(i = 0; i < m; i++)
scanf("%d", &alloc[i]);
printf("\nMemory Block\tMemory Size\tProcess\t\tProcess Size\tAllocation\n");
for(i = 0; i < m; i++) {
flag = 0;
for(j = 0; j < n; j++) {
if(size[j] >= alloc[i]) {
flag = 1;
printf("%-15d\t%-15d\t%-15d\t%-15d\t%-15d\n", memory[j], size[j], i+1, alloc[i], memory[j]);
size[j] -= alloc[i];
break;
}
}
if(flag == 0)
printf("%-15s\t%-15s\t%-15d\t%-15d\t%-15s\n", "N/A", "N/A", i+1, alloc[i], "Not Allocated");
}
return 0;
}
```
这段代码首先要求用户输入内存块数量和每个内存块的大小,然后要求用户输入进程数量和每个进程的大小。然后,它模拟了动态分区分配方式,并将分配结果输出到控制台。
阅读全文