操作系统实验内存管理算法c语言源代码
时间: 2023-10-26 14:29:49 浏览: 171
操作系统循环首次适应算法
4星 · 用户满意度95%
操作系统中的内存管理算法有很多种,以下是其中的几种常见的算法的 C 语言源代码实现。
1. 首次适应算法(First Fit)
```c
#include <stdio.h>
#define MAX_SIZE 100
int mem[MAX_SIZE];
int mem_size = MAX_SIZE;
void init_mem() {
for (int i = 0; i < mem_size; i++) {
mem[i] = -1;
}
}
int first_fit(int size) {
int start = -1;
int cnt = 0;
for (int i = 0; i < mem_size; i++) {
if (mem[i] == -1) {
if (start == -1) {
start = i;
}
cnt++;
} else {
start = -1;
cnt = 0;
}
if (cnt >= size) {
return start;
}
}
return -1;
}
void alloc_mem(int size, int pid) {
int start = first_fit(size);
if (start == -1) {
printf("Memory allocation failed for process %d\n", pid);
} else {
for (int i = start; i < start + size; i++) {
mem[i] = pid;
}
printf("Memory allocated for process %d starting from address %d\n", pid, start);
}
}
void dealloc_mem(int pid) {
for (int i = 0; i < mem_size; i++) {
if (mem[i] == pid) {
mem[i] = -1;
}
}
}
void print_mem() {
for (int i = 0; i < mem_size; i++) {
if (i % 10 == 0) {
printf("\n");
}
if (mem[i] == -1) {
printf(". ");
} else {
printf("%d ", mem[i]);
}
}
printf("\n");
}
int main() {
init_mem();
alloc_mem(5, 1);
print_mem();
alloc_mem(3, 2);
print_mem();
dealloc_mem(1);
print_mem();
alloc_mem(4, 3);
print_mem();
return 0;
}
```
2. 循环首次适应算法(Next Fit)
```c
#include <stdio.h>
#define MAX_SIZE 100
int mem[MAX_SIZE];
int mem_size = MAX_SIZE;
int last_alloc = 0;
void init_mem() {
for (int i = 0; i < mem_size; i++) {
mem[i] = -1;
}
}
int next_fit(int size) {
int start = -1;
int cnt = 0;
for (int i = last_alloc; i < mem_size; i++) {
if (mem[i] == -1) {
if (start == -1) {
start = i;
}
cnt++;
} else {
start = -1;
cnt = 0;
}
if (cnt >= size) {
last_alloc = start + size;
return start;
}
}
for (int i = 0; i < last_alloc; i++) {
if (mem[i] == -1) {
if (start == -1) {
start = i;
}
cnt++;
} else {
start = -1;
cnt = 0;
}
if (cnt >= size) {
last_alloc = start + size;
return start;
}
}
return -1;
}
void alloc_mem(int size, int pid) {
int start = next_fit(size);
if (start == -1) {
printf("Memory allocation failed for process %d\n", pid);
} else {
for (int i = start; i < start + size; i++) {
mem[i] = pid;
}
printf("Memory allocated for process %d starting from address %d\n", pid, start);
}
}
void dealloc_mem(int pid) {
for (int i = 0; i < mem_size; i++) {
if (mem[i] == pid) {
mem[i] = -1;
}
}
}
void print_mem() {
for (int i = 0; i < mem_size; i++) {
if (i % 10 == 0) {
printf("\n");
}
if (mem[i] == -1) {
printf(". ");
} else {
printf("%d ", mem[i]);
}
}
printf("\n");
}
int main() {
init_mem();
alloc_mem(5, 1);
print_mem();
alloc_mem(3, 2);
print_mem();
dealloc_mem(1);
print_mem();
alloc_mem(4, 3);
print_mem();
return 0;
}
```
3. 最佳适应算法(Best Fit)
```c
#include <stdio.h>
#define MAX_SIZE 100
int mem[MAX_SIZE];
int mem_size = MAX_SIZE;
void init_mem() {
for (int i = 0; i < mem_size; i++) {
mem[i] = -1;
}
}
int best_fit(int size) {
int start = -1;
int cnt = 0;
int min_size = mem_size + 1;
for (int i = 0; i < mem_size; i++) {
if (mem[i] == -1) {
if (start == -1) {
start = i;
}
cnt++;
} else {
if (cnt >= size && cnt < min_size) {
min_size = cnt;
start = i - cnt;
}
start = -1;
cnt = 0;
}
}
if (cnt >= size && cnt < min_size) {
min_size = cnt;
start = mem_size - cnt;
}
if (min_size == mem_size + 1) {
return -1;
}
return start;
}
void alloc_mem(int size, int pid) {
int start = best_fit(size);
if (start == -1) {
printf("Memory allocation failed for process %d\n", pid);
} else {
for (int i = start; i < start + size; i++) {
mem[i] = pid;
}
printf("Memory allocated for process %d starting from address %d\n", pid, start);
}
}
void dealloc_mem(int pid) {
for (int i = 0; i < mem_size; i++) {
if (mem[i] == pid) {
mem[i] = -1;
}
}
}
void print_mem() {
for (int i = 0; i < mem_size; i++) {
if (i % 10 == 0) {
printf("\n");
}
if (mem[i] == -1) {
printf(". ");
} else {
printf("%d ", mem[i]);
}
}
printf("\n");
}
int main() {
init_mem();
alloc_mem(5, 1);
print_mem();
alloc_mem(3, 2);
print_mem();
dealloc_mem(1);
print_mem();
alloc_mem(4, 3);
print_mem();
return 0;
}
```
以上是三种常见的内存管理算法的 C 语言源代码实现,希望能够对你有所帮助。
阅读全文