操作系统最坏适应算法
Posted robotpaul
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了操作系统最坏适应算法相关的知识,希望对你有一定的参考价值。
#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; }
以上是关于操作系统最坏适应算法的主要内容,如果未能解决你的问题,请参考以下文章
连续分配管理方式的动态分区分配算法(首次适应最佳适应最坏适应邻接适应算法)
连续分配管理方式的动态分区分配算法(首次适应最佳适应最坏适应邻接适应算法)