码迷,mamicode.com
首页 > 编程语言 > 详细

【操作系统】最坏适应算法

时间:2019-05-19 13:50:50      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:ext   str   tail   nbsp   namespace   star   class   link   ios   

#include<iostream>
using namespace std;
/*
内存共有640K
系统占用64K
t1   A  B    C    D
    8K  16K 64K 124K
t2  C完成 
*/
struct freeLink {
    int length;//分区长度
    int address;//地址
    freeLink* next;
};
struct busyLink {
    string processname;//进程名称
    int length;//分区长度
    int address;//地址
    busyLink* next;
};
class WorstFit {
private:
    freeLink* freehead;
    busyLink* busyhead;
    busyLink* busytail;
public:
    WorstFit()//创建两个链表并初始化
    {
        freehead = new freeLink();
        freehead->next = NULL;
        busyhead = new busyLink();
        busyhead->next = NULL;
        busytail = busyhead;
    }

    void start();//开始
    void requireMemo(string process, int require);//模拟内存分配
    void freeMemo(string process);//模拟内存回收
    void past(int time);//模拟系统过了time时间
    void printLink();//输出内存空闲情况(自由链的结点)
    void sortList();//排序
    void hebing();
};
void WorstFit::start()
{
    /*内存共有640K
    系统占用64K
    t1   A  B    C    D
        8K  16K 64K 124K
    */
    busyLink* sys = new busyLink();
    sys->processname = "系统资源";
    sys->next = NULL;
    sys->length = 64;
    sys->address = 64;
    busyhead->next = sys;
    busytail = sys;
    freeLink* free = new freeLink();
    free->address = 64;
    free->length = 640 - 64;
    freehead->next = free;
}
void WorstFit::requireMemo(string process, int require)
{
    if (freehead->next->length >= require)//位于freeLink首部的为最大空间
    {
        /*在busyLink中申请结点*/
        busyLink* p = new busyLink();
        p->address = busytail->address + require;
        p->length = require;
        p->next = NULL;
        p->processname = process;
        busytail->next = p;
        busytail = p;
        /*在freeLink中减去已占用的内存*/
        freehead->next->address = freehead->next->address + require;
        freehead->next->length = freehead->next->length - require;
    }

}
void WorstFit::freeMemo(string process)
{
    busyLink* p = busyhead->next;
    while (p->next != NULL)
    {
        if (p->next->processname == process)
        {
            int free_length = p->next->length;
            int free_address = p->next->address;
            p->next = p->next->next;
            //delete[]p;
            freeLink* free = new freeLink();
            free->address = free_address;
            free->length = free_length;
            freeLink* q = freehead->next;
            while (q->next != NULL) q = q->next;
            q->next = free;
            break;
        }
        p = p->next;
    }
    hebing();
    sortList();
}
void WorstFit::hebing()
{
    freeLink *p = freehead->next;
    while (p->next!=NULL)
    {
        freeLink* q= p->next;
        while (q!= NULL)
        {
            int p_end = p->address + p->length;
            int p_start = p->address;
            int q_end = q->address + q->length;
            int q_start = q->address;
            if (p_end == q_start||q_end==p_start)
            {
                p->length = p->length + q->length;
                p->next = p->next->next;
            }
            q = q->next;
        }
        p = p->next;
    }
}
void WorstFit::past(int time)
{
    cout << "----------------------------------------------------------" << endl;
    cout << "                过去了" << time << "时间" << endl;
    cout << "----------------------------------------------------------" << endl;
}
void WorstFit::sortList()
{
    //对freeLink链表进行排序,从大到小
    freeLink* p = freehead->next;
    freeLink* q;
    while (p->next != NULL)
    {
        q = p->next;
        while (q->next != NULL)
        {
            if (p->length < q->length)
            {
                int temp_length = q->length;
                int temp_address = q->address;
                q->length = p->length;
                q->address = p->address;
                p->length = temp_length;
                p->address = temp_address;
            }
            q = q->next;
        }
        p = p->next;
    }
}
void WorstFit::printLink()
{
    freeLink* p = freehead->next;
    int cont = 1;
    while (p != NULL)
    {
        cout << "" << cont << "块空闲长度为:" << p->length << "\t" << "地址范围:" << p->address  << "~" << p->address + p->length << endl;
        p = p->next;
        cont++;
    }

}
int main()
{
    WorstFit *test = new WorstFit();
    test->start();
    test->past(1);
    test->requireMemo("A", 8);
    test->requireMemo("B", 16);
    test->requireMemo("C", 64);
    test->requireMemo("D", 124);
    test->printLink();
    test->past(2);
    test->freeMemo("C");
    test->printLink();
    test->past(3);
    test->requireMemo("E", 124);
    test->printLink();
    test->past(4);
    test->freeMemo("D");
    test->printLink();

    return 0;
}

 

【操作系统】最坏适应算法

标签:ext   str   tail   nbsp   namespace   star   class   link   ios   

原文地址:https://www.cnblogs.com/robotpaul/p/10888953.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!