上篇博客说到了操作系统调度算法,那么这篇博客不用想也会猜测到是关于操作系统的内存分配与回收的,这里主要模拟实现了操作系统的首次适应算法和最佳适应算法来分配内存的,代码已经完成有一段时间了,但是忘记了分享一下,今天想起来了,所以特意写出来和大家分享一下,地址:http://https://github.com/admin-zou/DS/tree/master/systemMemDR
代码:
#ifndef _FIRSTFIT_H_
#define _FIRSTFIT_H_
#include <iostream>
#include <assert.h>
#include <stdlib.h>
using namespace std;
struct Pcb
{
int pcbid;
size_t needMem; //申请的内存大小
size_t realMem; //实际内存
size_t begin;
size_t end;
Pcb* next;
Pcb(int id = 0, int need = 0)
:pcbid(id), needMem(need), realMem(0), begin(0), end(0), next(NULL)
{}
};
struct memList //空闲链表
{
size_t msize; //大小
size_t first; //起始地址
size_t end; //终止地址
memList* next;
memList* prev;
memList(size_t f = 0, size_t e = 0)
:msize(e-f),first(f), end(e), next(NULL), prev(NULL)
{}
};
namespace FIRSTFIT
{//首次适应算法
class Firstfit
{
public:
Firstfit(size_t total) :_memNum(total), _blockNum(1), _pcbNum(0)
{
_pcbHead = new Pcb();
_memHead = new memList(0, total); //全部空闲
}
void push()
{
int size = 0;
cout << "请输入进程个数" << endl;
cin >&g