用c语言编写动态分区存储管理的模拟程序实现首次适应算法、最佳适应算法(源代码)代码提示部分用中文
时间: 2024-05-04 21:22:21 浏览: 146
首次适应算法:
```c
#include <stdio.h>
#define MAX 50
int bsize[MAX], psize[MAX], b[MAX], p[MAX], nb, np;
void read()
{
int i;
printf("\nMemory Management Scheme - First Fit");
printf("\nEnter the number of blocks:");
scanf("%d", &nb);
printf("Enter the size of the blocks:-\n");
for (i = 1; i <= nb; i++)
{
printf("Block %d:", i);
scanf("%d", &bsize[i]);
}
printf("\nEnter the number of processes:");
scanf("%d", &np);
printf("Enter the size of the processes :-\n");
for (i = 1; i <= np; i++)
{
printf("Process %d:", i);
scanf("%d", &psize[i]);
}
}
void first_fit()
{
int i, j;
for (i = 1; i <= np; i++)
{
for (j = 1; j <= nb; j++)
{
if (b[j] != 1) //if block is not allocated
{
if (bsize[j] >= psize[i])
{
p[i] = j;
break;
}
}
}
if (p[i] != 0)
{
b[j] = 1;
printf("\nProcess %d allocated to Block %d\n", i, p[i]);
}
else
{
printf("\nProcess %d can't be allocated\n", i);
}
}
}
int main()
{
int i;
read();
first_fit();
return 0;
}
```
最佳适应算法:
```c
#include <stdio.h>
#define MAX 50
int bsize[MAX], psize[MAX], b[MAX], p[MAX], nb, np;
void read()
{
int i;
printf("\nMemory Management Scheme - Best Fit");
printf("\nEnter the number of blocks:");
scanf("%d", &nb);
printf("Enter the size of the blocks:-\n");
for (i = 1; i <= nb; i++)
{
printf("Block %d:", i);
scanf("%d", &bsize[i]);
}
printf("\nEnter the number of processes:");
scanf("%d", &np);
printf("Enter the size of the processes :-\n");
for (i = 1; i <= np; i++)
{
printf("Process %d:", i);
scanf("%d", &psize[i]);
}
}
void best_fit()
{
int i, j, k;
for (i = 1; i <= np; i++)
{
int min = 9999;
for (j = 1; j <= nb; j++)
{
if (b[j] != 1) //if block is not allocated
{
if (bsize[j] >= psize[i])
{
if (bsize[j] - psize[i] < min)
{
min = bsize[j] - psize[i];
k = j;
}
}
}
}
if (min != 9999)
{
p[i] = k;
b[k] = 1;
printf("\nProcess %d allocated to Block %d\n", i, p[i]);
}
else
{
printf("\nProcess %d can't be allocated\n", i);
}
}
}
int main()
{
int i;
read();
best_fit();
return 0;
}
```
阅读全文