用c语言写一个物理内存分配算法,给出注释
时间: 2024-05-13 13:15:42 浏览: 13
以下是一个简单的物理内存分配算法的C语言实现,使用了首次适应算法:
```c
#include <stdio.h>
#define MEM_SIZE 1000 //物理内存大小
#define FREE 0 //空闲内存标志
#define OCCUPIED 1 //占用内存标志
int memory[MEM_SIZE]; //物理内存数组
int process_num = 0; //进程数量
//初始化物理内存数组
void init_memory() {
for (int i = 0; i < MEM_SIZE; i++) {
memory[i] = FREE;
}
}
//分配内存
int allocate_memory(int psize) {
int start = -1; //起始位置
for (int i = 0; i < MEM_SIZE; i++) {
if (memory[i] == FREE) { //找到空闲内存
int j = i;
while (j < MEM_SIZE && memory[j] == FREE && j-i+1 < psize) { //查找连续空闲内存
j++;
}
if (j-i+1 == psize) { //找到足够大的连续空闲内存
start = i;
break;
}
}
}
if (start == -1) { //未找到足够大的连续空闲内存
return -1;
}
for (int i = start; i < start+psize; i++) { //将内存标记为占用
memory[i] = OCCUPIED;
}
process_num++; //进程数量加1
return start;
}
//释放内存
void free_memory(int start, int psize) {
for (int i = start; i < start+psize; i++) { //将内存标记为空闲
memory[i] = FREE;
}
process_num--; //进程数量减1
}
int main() {
init_memory(); //初始化物理内存
int pid1 = allocate_memory(200); //分配200字节内存
if (pid1 != -1) {
printf("Process 1 allocated memory from %d to %d.\n", pid1, pid1+199);
printf("Memory usage: %d / %d\n", process_num, MEM_SIZE);
} else {
printf("Memory allocation failed.\n");
}
int pid2 = allocate_memory(300); //分配300字节内存
if (pid2 != -1) {
printf("Process 2 allocated memory from %d to %d.\n", pid2, pid2+299);
printf("Memory usage: %d / %d\n", process_num, MEM_SIZE);
} else {
printf("Memory allocation failed.\n");
}
free_memory(pid1, 200); //释放200字节内存
printf("Process 1 freed memory from %d to %d.\n", pid1, pid1+199);
printf("Memory usage: %d / %d\n", process_num, MEM_SIZE);
int pid3 = allocate_memory(500); //分配500字节内存
if (pid3 != -1) {
printf("Process 3 allocated memory from %d to %d.\n", pid3, pid3+499);
printf("Memory usage: %d / %d\n", process_num, MEM_SIZE);
} else {
printf("Memory allocation failed.\n");
}
return 0;
}
```
注释:
- `#define MEM_SIZE 1000`:定义物理内存大小为1000字节。
- `#define FREE 0`:定义空闲内存标志为0。
- `#define OCCUPIED 1`:定义占用内存标志为1。
- `int memory[MEM_SIZE]`:定义物理内存数组。
- `int process_num = 0`:定义进程数量,初始值为0。
- `void init_memory()`:初始化物理内存数组,将所有元素赋值为FREE。
- `int allocate_memory(int psize)`:分配内存,psize为请求内存大小,返回分配的起始位置,若分配失败返回-1。
- `void free_memory(int start, int psize)`:释放内存,start为释放的起始位置,psize为请求内存大小。
- `int main()`:主函数。
- `init_memory()`:初始化物理内存。
- `int pid1 = allocate_memory(200)`:分配200字节内存,返回分配的起始位置,若分配失败返回-1。
- `if (pid1 != -1)`:如果分配成功。
- `printf("Process 1 allocated memory from %d to %d.\n", pid1, pid1+199)`:输出分配的起始位置和结束位置。
- `printf("Memory usage: %d / %d\n", process_num, MEM_SIZE)`:输出内存使用情况。
- `int pid2 = allocate_memory(300)`:分配300字节内存。
- `if (pid2 != -1)`:如果分配成功。
- `printf("Process 2 allocated memory from %d to %d.\n", pid2, pid2+299)`:输出分配的起始位置和结束位置。
- `printf("Memory usage: %d / %d\n", process_num, MEM_SIZE)`:输出内存使用情况。
- `free_memory(pid1, 200)`:释放200字节内存。
- `printf("Process 1 freed memory from %d to %d.\n", pid1, pid1+199)`:输出释放的起始位置和结束位置。
- `printf("Memory usage: %d / %d\n", process_num, MEM_SIZE)`:输出内存使用情况。
- `int pid3 = allocate_memory(500)`:分配500字节内存。
- `if (pid3 != -1)`:如果分配成功。
- `printf("Process 3 allocated memory from %d to %d.\n", pid3, pid3+499)`:输出分配的起始位置和结束位置。
- `printf("Memory usage: %d / %d\n", process_num, MEM_SIZE)`:输出内存使用情况。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)