最初/最良/最悪の適合アルゴリズムのメモリ ブロックの割り当てと回復をシミュレートする C 言語プログラムを作成し、各割り当てと回復後に空きパーティションと割り当てられたパーティションの表示を要求します。初期状態での空きメモリ容量は640KBであるとします。(江西師範大学ソフトウェア学院のオペレーティングシステム)

【オペレーティングシステム】 パーティション割り当てアルゴリズム(ファーストアダプテーションアルゴリズム、ベストアダプテーションアルゴリズム、ワーストアダプテーションアルゴリズム)(C言語実装)

動的なパーティション割り当てを実現するために、通常、システム内の空きパーティションがチェーンにリンクされます。いわゆる順次検索とは、空きパーティションチェーン上の空きパーティションを順番に検索して、要件を満たすサイズのパーティションを見つけることを指します。--------コンピュータ オペレーティング システム (第 4 版)

可変パーティションはダイナミック パーティションとも呼ばれ、ジョブがメモリにロードされると、使用可能なメモリから連続領域が描画されて割り当てられ、パーティションのサイズはジョブのサイズとまったく同じになります。

可変パーティション割り当て戦略:

1. 最初の適応アルゴリズム: チェーンの先頭から開始して、アドレスがインクリメントされます。

2. 最適な適応アルゴリズム: パフォーマンスは最悪、容量は減少、無駄は最小限

3. 最悪の適応アルゴリズム: パーティション サイズが減少し、フラグメントが統合され、使用率が向上します。

初めてアルゴリズムに適応する場合は、スペースが空いているかどうかを確認するために継続的にトラバースできます。

最適適応アルゴリズムは、最適に適応した空き領域を見つけることですが、空き領域が使用された後にメモリの一部が未使用になることになり、この小さなフラグメントを再度使用することが困難になります。

最悪の適応アルゴリズムは、小さなフラグメントの発生をより効果的に減らすことができるように、残りのスペースが最大になるようにメモリを割り当てる最大のスペースを見つけることです。

メモリを確保するときにいつも思うのですが、C言語にはメモリを確保できるmalloc関数があります。そこで、この課題を書いたときに、その中にある malloc 関数を理解しようと思いつきました。当初はフリーリンクリストの保存にvectorを使っていましたが、下位層に合わせるために純粋なC言語で書いた方が良いと感じました。

#include <stdio.h>

#define MEMORY_SIZE 640 // 内存大小(单位:KB)
#define BLOCK_SIZE 1 // 内存块大小(单位:KB)

// 内存块结构体
typedef struct {
    int size; // 大小(单位:KB)
    int is_free; // 是否空闲
} block_t;

// 内存块数组
block_t memory[MEMORY_SIZE / BLOCK_SIZE];

// 初始化内存块数组
void init_memory() {
    int i;
    for (i = 0; i < MEMORY_SIZE / BLOCK_SIZE; i++) {
        memory[i].size = BLOCK_SIZE;
        memory[i].is_free = 1;
    }
}

// 显示内存分配情况
void print_memory() {
    int i, free_blocks = 0, allocated_blocks = 0, free_size = 0, allocated_size = 0;
    printf("\n------------------------------\n");
    printf("      Memory Allocation\n");
    printf("------------------------------\n");
    for (i = 0; i < MEMORY_SIZE / BLOCK_SIZE; i++) {
        printf("%d ", i);
        if (memory[i].is_free) {
            printf("Free   ");
            free_blocks++;
            free_size += memory[i].size;
        }
        else {
            printf("Allocated ");
            allocated_blocks++;
            allocated_size += memory[i].size;
        }
        printf("%dKB\n", memory[i].size);
    }
    printf("------------------------------\n");
    printf("Total Blocks: %d\n", free_blocks + allocated_blocks);
    printf("Free Blocks: %d\n", free_blocks);
    printf("Allocated Blocks: %d\n", allocated_blocks);
    printf("Total Size: %dKB\n", free_size + allocated_size);
    printf("Free Size: %dKB\n", free_size);
    printf("Allocated Size: %dKB\n", allocated_size);
    printf("------------------------------\n\n");
}

// 首次适应算法分配内存
int first_fit_allocation(int size) {
    int i, j;
    int blocks_needed = (size + BLOCK_SIZE - 1) / BLOCK_SIZE; // 需要的块数
    for (i = 0; i < MEMORY_SIZE / BLOCK_SIZE; i++) {
        if (memory[i].is_free) { // 如果当前块为空闲块
            int free_blocks = 0;
            for (j = i; j < MEMORY_SIZE / BLOCK_SIZE && memory[j].is_free; j++) {
                free_blocks++;
                if (free_blocks == blocks_needed) { // 如果找到了足够的空闲块
                    for (j = i; j < i +
                        blocks_needed; j++) {
                        memory[j].is_free = 0;
                    }
                    return i; // 返回分配的起始块的索引
                }
            }
        }
    }
    return -1; // 分配失败
}

// 最佳适应算法分配内存
int best_fit_allocation(int size) {
    int i, j;
    int blocks_needed = (size + BLOCK_SIZE - 1) / BLOCK_SIZE; // 需要的块数
    int best_index = -1, best_size = MEMORY_SIZE / BLOCK_SIZE + 1; // 初始化为无效值
    for (i = 0; i < MEMORY_SIZE / BLOCK_SIZE; i++) {
        if (memory[i].is_free && memory[i].size >= blocks_needed) { // 如果当前块为空闲块并且大小足够
            if (memory[i].size < best_size) { // 如果当前块更小
                best_index = i;
                best_size = memory[i].size;
            }
        }
    }
    if (best_index == -1) { // 分配失败
        return -1;
    }
    else {
        for (j = best_index; j < best_index + blocks_needed; j++) {
            memory[j].is_free = 0;
        }
        return best_index; // 返回分配的起始块的索引
    }
}

// 最坏适应算法分配内存
int worst_fit_allocation(int size) {
    int i, j;
    int blocks_needed = (size + BLOCK_SIZE - 1) / BLOCK_SIZE; // 需要的块数
    int worst_index = -1, worst_size = -1; // 初始化为无效值
    for (i = 0; i < MEMORY_SIZE / BLOCK_SIZE; i++) {
        if (memory[i].is_free && memory[i].size >= blocks_needed) { // 如果当前块为空闲块并且大小足够
            if (memory[i].size > worst_size) { // 如果当前块更大
                worst_index = i;
                worst_size = memory[i].size;
            }
        }
    }
    if (worst_index == -1) { // 分配失败
        return -1;
    }
    else {
        for (j = worst_index; j < worst_index + blocks_needed; j++) {
            memory[j].is_free = 0;
        }
        return worst_index; // 返回分配的起始块的索引
    }
}

// 回收内存
void deallocation(int start_index) {
    int i;
    for (i = start_index; i < MEMORY_SIZE / BLOCK_SIZE && !memory[i].is_free; i++) {
        memory[i].is_free = 1;
    }
}

int main() {
    int choice, size, start_index;
    init_memory();
    do {
        print_memory();
        printf("1. First Fit Allocation\n");
        printf("2. Best Fit Allocation\n");
        printf("3. Worst Fit Allocation\n");
        printf("4. Deallocation\n");
        printf("0. Exit\n");
        printf("Enter your choice: ");
        scanf_s("%d", &choice);
        switch (choice) {
        case 1:
            printf("Enter the size to be allocated (in KB): ");
            scanf_s("%d", &size);
            start_index = first_fit_allocation(size);
            if (start_index == -1) {
                printf("Memory allocation failed.\n");            } else {
                    printf("Memory allocated from block %d to %d.\n", start_index, start_index + (size + BLOCK_SIZE - 1) / BLOCK_SIZE - 1);
            }
            break;
        case 2:
            printf("Enter the size to be allocated (in KB): ");
            scanf_s("%d", &size);
            start_index = best_fit_allocation(size);
            if (start_index == -1) {
                printf("Memory allocation failed.\n");
            }
            else {
                printf("Memory allocated from block %d to %d.\n", start_index, start_index + (size + BLOCK_SIZE - 1) / BLOCK_SIZE - 1);
            }
            break;
        case 3:
            printf("Enter the size to be allocated (in KB): ");
            scanf_s("%d", &size);
            start_index = worst_fit_allocation(size);
            if (start_index == -1) {
                printf("Memory allocation failed.\n");
            }
            else {
                printf("Memory allocated from block %d to %d.\n", start_index, start_index + (size + BLOCK_SIZE - 1) / BLOCK_SIZE - 1);
            }
            break;
        case 4:
            printf("Enter the starting block index to be deallocated: ");
            scanf_s("%d", &start_index);
            deallocation(start_index);
            printf("Memory deallocated from block %d.\n", start_index);
            break;
        case 0:
            printf("退出...\n");
            break;
        default:
            printf("没有这个选项\n");
        }
    } while (choice != 0);
    return 0;
}

コードは vs で記述されているため、scanf_s が使用されます。別のコンパイラに変更する場合は、それも変更する必要があります。

(皆さん、帰る前にいいねをお願いします。これは私にとって本当に重要なことです QwQ)

おすすめ

転載: blog.csdn.net/qq_63165951/article/details/130661239