操作系統 首次適應算法 最佳適應算法

首次適應算法代碼

#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <string.h>
#include <cstdio>
#include <cstdlib>
#include <queue>
using namespace std;
const int maxn = 10;
int N;          ///操作次數
int init_room;  ///總的內存可用空間
char s[] = "申請";

typedef struct Node{
    int start;  ///地址起點
    int ended;  ///地址終點
    int room;   ///地址長度
    int id = 0;     ///任務序號(空閒內存都爲0,佔用則爲對應的序號)
    Node* next;
}Node;

Node* HE;  ///空閒內存鏈表的頭結點
Node* HF;   ///佔用內存鏈表的頭結點

void add_next(int start, int ended, int room, int id, Node* next){
    next -> start = start;
    next -> ended = ended;
    next -> room = room;
    next -> id = id;
    next -> next = NULL;
}

void init(){
    HE = (Node*)malloc(sizeof(Node));
    HF = (Node*)malloc(sizeof(Node));
    HE -> next = (Node*)malloc(sizeof(Node));
    if(HE == NULL || HF == NULL || HE -> next == NULL){
        cout << "鏈表頭結點分配內存失敗或鏈表尾部添加結點時分配內存失敗" << endl;
        exit(-1);
    }

    add_next(0, init_room - 1, init_room, 0, HE -> next);
    HF -> next = NULL;
}

/*
 *遍歷鏈表並將連續的空閒內存合併
 */
void conbine(){

}

void out_put_empty(){
    Node* p = HE -> next;
    cout << "空閒內存情況"  << endl;
    cout << "---------------------------" << endl;
    printf("%10s %10s %10s\n", "起始地址", "結束地址", "內存大小");
    while(p != NULL){
        printf("%10d %10d %10d\n", p -> start, p -> ended, p -> room);
        p = p -> next;
    }
    cout << "---------------------------" << endl;
}

void out_put_full(){
    Node* p = HF -> next;
    cout << "佔用內存情況"  << endl;
    cout << "---------------------------" << endl;
    printf("%10s %10s %10s %10s\n", "任務id", "起始地址", "結束地址", "內存大小");
    while(p != NULL){
        printf("%10d %10d %10d %10d\n",p -> id, p -> start, p -> ended, p -> room);
        p = p -> next;
    }
    cout << "---------------------------" << endl;
}

/*
 * 指向尾結點
 */
Node* get_tail(){
    Node* p = HF;
    while(p -> next != NULL){
        p = p -> next;
    }
    return p;
}


void insert_full(Node* p, int id, int room){
    Node* tail = get_tail();
    tail -> next = (Node*)malloc(sizeof(Node));
    if(tail -> next == NULL){
        cout << "佔用內存鏈表尾部添加結點時分配內存失敗" << endl;
        exit(-1);
    }
    add_next(p -> start, p -> start + room - 1, room, id, tail -> next);
}

void delete_emtpy(Node* p, int room){
    p -> start += room;
    p -> room -= room;
}

/*
 * 在空閒內存中查找是否有連續的room空間
 * 有則返回空閒內存塊的指針,沒有則返回NULL
 */
bool search_delete_empty_insert_full(int id, int room){
    Node* p = HE -> next;
    while(p != NULL){
        if(p -> room >= room){
            insert_full(p, id, room);
            delete_emtpy(p, room);
            return true;
        }
        p = p -> next;
    }
}

void free_full(Node* p, Node* pre){
    pre -> next = p -> next;
    free(p);
    p = NULL;
}

/*
 * 將釋放的空間加回空閒內存鏈表
 */
void add_empty(Node* pFull){
    Node* p = HE;
    while(p != NULL){
        if(p -> next == NULL || !(pFull -> start > p -> next -> ended)){
            Node * new_empty = (Node*)malloc(sizeof(Node));
            if(new_empty == NULL){
                cout << "空閒內存鏈表創建新的結點失敗" << endl;
                exit(-1);
            }
            new_empty -> start = pFull -> start;
            new_empty -> ended = pFull -> ended;
            new_empty -> room = pFull -> room;
            new_empty -> next = p -> next;
            p -> next = new_empty;
            return ;
        }
        p = p -> next;
    }
    return ;
}

bool search_id_free_full_add_empty(int id){
    Node* p = HF -> next;
    Node* pre = HF;
    while(p != NULL){
        if(p -> id == id){
            add_empty(p);
            free_full(p, pre);
            return true;
        }
        pre = p;
        p = p -> next;
    }
    return false;
}

void conbine_empty(){
    Node* p = HE -> next;
    //printf("%d %d\n", p == NULL, p -> next == NULL);
    while(p != NULL && p -> next != NULL){
        if(p -> ended + 1 == p -> next -> start){
            p -> ended = p -> next -> ended;
            p -> room += p -> next -> room;
            Node* tem = p -> next;
            p -> next = p -> next -> next;
            free(tem);
            tem = NULL;
        }else{
            p = p -> next;
        }
    }
}

void solve(int id, char* behave, int room){
    if(strcmp(behave, s) == 0){///申請操作
        if(search_delete_empty_insert_full(id, room)){
            cout << "申請成功" << endl;
        }else{
            cout << "空間不足,申請失敗" << endl;
        }
    }else{///釋放
        if(search_id_free_full_add_empty(id)){
            cout << "釋放成功" << endl;
        }else{
            cout << "無此任務,釋放失敗" << endl;
        }
    }
    conbine_empty();
}

int main(){
    cout << "請輸入操作次數:";
    cin >> N;
    cout << "請輸入內存可用空間:";
    cin >> init_room;
    getchar();
    init();
    while(N--){
        int id, room;
        char behave[maxn];   ///要執行的操作
        scanf("作業%d %s %dKB", &id, behave, &room);
        getchar();
        solve(id, behave, room);
        out_put_empty();
        out_put_full();
    }
    return 0;
}
/*
11
640
作業1 申請 130KB
作業2 申請 60KB
作業3 申請 100KB
作業2 釋放 60KB
作業4 申請 200KB
作業3 釋放 100KB
作業1 釋放 130KB
作業5 申請 140KB
作業6 申請 60KB
作業7 申請 50KB
作業8 申請 60KB
*/

最佳適應代碼

合併連續空閒內存的模塊要改
增加一個空閒內存鏈表大小 從小到大排序的模塊,用插入排序就好,這裏不用改變指針的排序方式,用結點賦值的方式。

#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <string.h>
#include <cstdio>
#include <cstdlib>
#include <queue>
using namespace std;
const int maxn = 10;
int N;          ///操作次數
int init_room;  ///總的內存可用空間
char s[] = "申請";

typedef struct Node{
    int start;  ///地址起點
    int ended;  ///地址終點
    int room;   ///地址長度
    int id = 0;     ///任務序號(空閒內存都爲0,佔用則爲對應的序號)
    Node* next;
}Node;

Node* HE;  ///空閒內存鏈表的頭結點
Node* HF;   ///佔用內存鏈表的頭結點

void add_next(int start, int ended, int room, int id, Node* next){
    next -> start = start;
    next -> ended = ended;
    next -> room = room;
    next -> id = id;
    next -> next = NULL;
}

void init(){
    HE = (Node*)malloc(sizeof(Node));
    HF = (Node*)malloc(sizeof(Node));
    HE -> next = (Node*)malloc(sizeof(Node));
    if(HE == NULL || HF == NULL || HE -> next == NULL){
        cout << "鏈表頭結點分配內存失敗或鏈表尾部添加結點時分配內存失敗" << endl;
        exit(-1);
    }

    add_next(0, init_room - 1, init_room, 0, HE -> next);
    HF -> next = NULL;
}

/*
 *遍歷鏈表並將連續的空閒內存合併
 */
void conbine(){

}

void out_put_empty(){
    Node* p = HE -> next;
    cout << "空閒內存情況"  << endl;
    cout << "---------------------------" << endl;
    printf("%10s %10s %10s\n", "起始地址", "結束地址", "內存大小");
    while(p != NULL){
        printf("%10d %10d %10d\n", p -> start, p -> ended, p -> room);
        p = p -> next;
    }
    cout << "---------------------------" << endl;
}

void out_put_full(){
    Node* p = HF -> next;
    cout << "佔用內存情況"  << endl;
    cout << "---------------------------" << endl;
    printf("%10s %10s %10s %10s\n", "任務id", "起始地址", "結束地址", "內存大小");
    while(p != NULL){
        printf("%10d %10d %10d %10d\n",p -> id, p -> start, p -> ended, p -> room);
        p = p -> next;
    }
    cout << "---------------------------" << endl;
}

/*
 * 指向尾結點
 */
Node* get_tail(){
    Node* p = HF;
    while(p -> next != NULL){
        p = p -> next;
    }
    return p;
}


void insert_full(Node* p, int id, int room){
    Node* tail = get_tail();
    tail -> next = (Node*)malloc(sizeof(Node));
    if(tail -> next == NULL){
        cout << "佔用內存鏈表尾部添加結點時分配內存失敗" << endl;
        exit(-1);
    }
    add_next(p -> start, p -> start + room - 1, room, id, tail -> next);
}

void delete_emtpy(Node* p, int room){
    p -> start += room;
    p -> room -= room;
}

/*
 * 在空閒內存中查找是否有連續的room空間
 * 有則返回空閒內存塊的指針,沒有則返回NULL
 */
bool search_delete_empty_insert_full(int id, int room){
    Node* p = HE -> next;
    while(p != NULL){
        if(p -> room >= room){
            insert_full(p, id, room);
            delete_emtpy(p, room);
            return true;
        }
        p = p -> next;
    }
}

void free_full(Node* p, Node* pre){
    pre -> next = p -> next;
    free(p);
    p = NULL;
}

/*
 * 將釋放的空間加回空閒內存鏈表
 */
void add_empty(Node* pFull){
    Node* p = HE;
    while(p != NULL){
        if(p -> next == NULL || !(pFull -> start > p -> next -> ended)){
            Node * new_empty = (Node*)malloc(sizeof(Node));
            if(new_empty == NULL){
                cout << "空閒內存鏈表創建新的結點失敗" << endl;
                exit(-1);
            }
            new_empty -> start = pFull -> start;
            new_empty -> ended = pFull -> ended;
            new_empty -> room = pFull -> room;
            new_empty -> next = p -> next;
            p -> next = new_empty;
            return ;
        }
        p = p -> next;
    }
    return ;
}

bool search_id_free_full_add_empty(int id){
    Node* p = HF -> next;
    Node* pre = HF;
    while(p != NULL){
        if(p -> id == id){
            add_empty(p);
            free_full(p, pre);
            return true;
        }
        pre = p;
        p = p -> next;
    }
    return false;
}

void conbine_empty(){
    Node* p = HE -> next;
    while(p != NULL && p -> next != NULL){
        Node* p2 = p -> next;
        Node* pre = p;
        bool flag = false;///標記是否有發生合併
        while(p2 != NULL){
            if(p -> ended + 1 == p2 -> start){
                p -> ended = p2 -> ended;
                p -> room += p2 -> room;
                pre -> next = p2 -> next;
                free(p2);
                p2 = NULL;
                flag = true;
            }
            else{
                pre = p2;
                p2 = p2 -> next;
            }
        }
        if(!flag){
            p = p -> next;
        }
    }
}

void swap_Node(Node* a, Node* b){
    Node tem = *a;
    a -> start = b -> start;
    a -> ended = b -> ended;
    a -> room = b -> room;

    b -> start = tem.start;
    b -> ended = tem.ended;
    b -> room= tem.room;
}

void sort_empty(){
    Node* p = HE;
    while(p != NULL && p -> next != NULL){
        Node* tem = p -> next;
        Node* pmini = tem;
        while(tem != NULL){
            if(tem -> room < pmini -> room){
                pmini = tem;
            }
            tem = tem -> next;
        }
        swap_Node(p -> next, pmini);
        p = p -> next;
    }
}

void solve(int id, char* behave, int room){
    if(strcmp(behave, s) == 0){///申請操作
        if(search_delete_empty_insert_full(id, room)){
            cout << "申請成功" << endl;
        }else{
            cout << "空間不足,申請失敗" << endl;
        }
    }else{///釋放
        if(search_id_free_full_add_empty(id)){
            cout << "釋放成功" << endl;
        }else{
            cout << "無此任務,釋放失敗" << endl;
        }
    }
    conbine_empty();
    sort_empty();
}

int main(){
    cout << "請輸入操作次數:";
    cin >> N;
    cout << "請輸入內存可用空間:";
    cin >> init_room;
    getchar();
    init();
    while(N--){
        int id, room;
        char behave[maxn];   ///要執行的操作
        scanf("作業%d %s %dKB", &id, behave, &room);
        getchar();
        solve(id, behave, room);
        out_put_empty();
        out_put_full();
    }
    return 0;
}
/*

11
640
作業1 申請 130KB
作業2 申請 60KB
作業3 申請 100KB
作業2 釋放 60KB
作業4 申請 200KB
作業3 釋放 100KB
作業1 釋放 130KB
作業5 申請 140KB
作業6 申請 60KB
作業7 申請 50KB
作業8 申請 60KB

*/
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章