操作系统 首次适应算法 最佳适应算法(C++)

首次适应算法代码

#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 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 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

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