最差适应算法
#ifdef USING_WORST_FIT
{
//先找到第一个满足要求的空洞,
//再以第一个为标准寻找最适合的空洞。
//当最适合的空洞完全吻合
//就直接划给它,当空洞较大时就切割。 //首先注册目标指针、目标前一个指针、头指针
//记录目标大小和目前最适合大小
register struct hole *hp;
register struct hole *prevAim_ptr;
register struct hole *Aim_ptr;
phys_clicks old_base; //如果循环一次都没找到
//就把可以退出内存的进程赶出去
//再循环 ;
do{
hp = hole_head;
prevAim_ptr = NIL_HOLE;
Aim_ptr = NIL_HOLE; for(;hp != NIL_HOLE && hp->h_base < swap_base;
prevAim_ptr=hp,hp=hp->h_next)
{ //当从没记录过合适内存时
//把遇到的第一个合适节点保存在aim中
if(hp->h_len >= clicks && Aim_ptr == NIL_HOLE)
{
Aim_ptr=hp;
} //当找到一个比原来aim更合适的空间
//记录新的空间
if(hp->h_len >= clicks && hp->h_len > Aim_ptr->h_len)
{
//mark it down
Aim_ptr=hp;
} } //we found bigest
if(Aim_ptr!=NIL_HOLE)
{
old_base =Aim_ptr->h_base;
Aim_ptr->h_base+=clicks;
Aim_ptr->h_len-=clicks; /* Remember new high watermark of used memory. */
if(Aim_ptr->h_base > high_watermark)
high_watermark = Aim_ptr->h_base; return(old_base);
} }while(swap_out());
return(NO_MEM);
}
#endif
 最佳适应:
#ifdef USING_BEST_FIT
{
//先找到第一个满足要求的空洞,
//再以第一个为标准寻找最适合的空洞。
//当最适合的空洞完全吻合
//就直接划给它,当空洞较大时就切割。 //首先注册目标指针、目标前一个指针、头指针
//记录目标大小和目前最适合大小
register struct hole *hp;
register struct hole *prevAim_ptr;
register struct hole *Aim_ptr;
phys_clicks old_base; //如果循环一次都没找到
//就把可以退出内存的进程赶出去
//再循环
do{
hp = hole_head;
prevAim_ptr = NIL_HOLE;
Aim_ptr = NIL_HOLE; for(;hp != NIL_HOLE && hp->h_base < swap_base;
prevAim_ptr=hp,hp=hp->h_next)
{ //当从没记录过合适内存时
//把遇到的第一个合适节点保存在aim中
if(hp->h_len > clicks && Aim_ptr == NIL_HOLE)
{
Aim_ptr=hp;
} //当找到一个比原来aim更合适的空间
//记录新的空间
if(hp->h_len > clicks && hp->h_len < Aim_ptr->h_len)
{
//mark it down
Aim_ptr=hp;
}
}
//we found it
if(Aim_ptr != NIL_HOLE)
{
old_base = Aim_ptr->h_base; /* remember where it started */
Aim_ptr->h_base += clicks; /* bite off */
Aim_ptr->h_len -= clicks; /* ditto */ /* Remember new high watermark of used memory. */
if(Aim_ptr->h_base > high_watermark)
high_watermark = Aim_ptr->h_base; return(old_base);
} }while(swap_out());
return(NO_MEM);
}
#endif
 下一个最适合 :
#ifdef USING_NEXT_FIT //161 error
{
register struct hole *hp, *prev_ptr;
static register struct hole *start_ptr; phys_clicks old_base;
   start_ptr = hole_head;
   Prev_ptr=NIL_HOLE;
hp=start_ptr; do{ if(hp->h_next==start_ptr)
{
return(NO_MEM);
} while (hp->h_next != start_ptr && hp->h_base < swap_base){ if (hp->h_len >= clicks) {
/* We found a hole that is big enough. Use it. */
old_base = hp->h_base; /* remember where it started */
hp->h_base += clicks; /* bite a piece off */
hp->h_len -= clicks; /* ditto */ /* Delete the hole if used up completely. */
if (hp->h_len == ) del_slot(prev_ptr, hp); If((start_ptr=hp->h_next)==NIL_HOLE)
Start_ptr=hole_head; /* Return the start address of the acquired block. */
return(old_base);
} prev_ptr = hp;
hp = hp->h_next; If(hp==NIL_HOLE)
hp=hole_head;
} }while(swap_out());
}
#endif
FIRST_FIT
*===========================================================================*
* alloc_mem分配内存 *
*===========================================================================*/
PUBLIC phys_clicks alloc_mem(clicks)
phys_clicks clicks; /* amount of memory requested */
{
/* Allocate a block of memory from the free list using first fit. The block
* consists of a sequence of contiguous bytes, whose length in clicks is
* given by 'clicks'. A pointer to the block is returned. The block is
* always on a click boundary. This procedure is called when memory is
* needed for FORK or EXEC. Swap other processes out if needed.
*/
register struct hole *hp, *prev_ptr;
phys_clicks old_base; do {
prev_ptr = NIL_HOLE;
hp = hole_head;
while (hp != NIL_HOLE && hp->h_base < swap_base) {
if (hp->h_len >= clicks) {
/* We found a hole that is big enough. Use it. */
old_base = hp->h_base; /* remember where it started */
hp->h_base += clicks; /* bite a piece off */
hp->h_len -= clicks; /* ditto */ /* Remember new high watermark of used memory. */
if(hp->h_base > high_watermark)
high_watermark = hp->h_base; /* Delete the hole if used up completely. */
if (hp->h_len == ) del_slot(prev_ptr, hp); /* Return the start address of the acquired block. */
return(old_base);
} prev_ptr = hp;
hp = hp->h_next;
}
} while (swap_out()); /* try to swap some other process out */
return(NO_MEM);
}

MINI3内存分配算法的更多相关文章

  1. Java实现内存分配算法 FF(首次适应算法) BF(最佳适应算法)

    一.概述 因为这次os作业对用户在控制台的输入输出有要求,所以我花了挺多的代码来完善控制台的显示. MemoryAlgorithm类里只是和控制台输入输出有关的操作,而对内存的所有逻辑操作都是用Mem ...

  2. python的list内存分配算法

    前提:python为了提高效率会为list预先分配一定的内存空间供其使用,避免在每次append等操作都去申请内存,下面简单分析下list的内存分配算法,主要就是两段. 1.当没有元素时,newsiz ...

  3. Java实现操作系统中四种动态内存分配算法:BF+NF+WF+FF

    1 概述 本文是利用Java实现操作系统中的四种动态内存分配方式 ,分别是: BF NF WF FF 分两部分,第一部分是介绍四种分配方式的概念以及例子,第二部分是代码实现以及讲解. 2 四种分配方式 ...

  4. Buddy内存分配算法

    Buddy(伙伴的定义): 这里给出伙伴的概念,满足以下三个条件的称为伙伴:1)两个块大小相同:2)两个块地址连续:3)两个块必须是同一个大块中分离出来的: Buddy算法的优缺点: 1)尽管伙伴内存 ...

  5. c模拟内存分配算法(首次适应算法,最佳适应算法,最坏适应算法)

    #include<bits/stdc++.h> using namespace std; /*定义内存的大小为100*/ #define MEMSIZE 100 /*如果小于此值,将不再分 ...

  6. [图解tensorflow源码] [转载] tensorflow设备内存分配算法解析 (BFC算法)

    转载自 http://weibo.com/p/1001603980563068394770   @ICT_吴林阳 tensorflow设备内存管理模块实现了一个best-fit with coales ...

  7. TLFS 内存分配算法详解

    文章目录 1. DSA 背景介绍 1.1 mmheap 1.2 mmblk 2. TLFS 原理 2.1 存储结构 2.2 内存池初始化 2.3 free 2.4 malloc 参考资料 1. DSA ...

  8. 内存分配---FF、BF、WF三种算法

    动态分区分配是根据进程的实际需要,动态的为之分配内存空间.而在实现可变分区分配时,将涉及到分区分配中 所用的数据结构.分区分配算法和分区的分配与内存回收的过程. 分区分配中的数据结构:(1)描述空闲块 ...

  9. SQLite剖析之动态内存分配

    SQLite通过动态内存分配来获取各种对象(例如数据库连接和SQL预处理语句)所需内存.建立数据库文件的内存Cache.保存查询结果. 1.特性    SQLite内核和它的内存分配子系统提供以下特性 ...

随机推荐

  1. django系列3.3--view视图函数, render, reverse(未完待续)

    1.view视图函数 urls分发之后所用到的处理函数 2.render 用于渲染页面 在views.py中常用 from django.shortcuts import render, HttpRe ...

  2. Javascript实例 -- 计时器, 搜索框,selected联动

    计时器: <body> <input type="text" id="i1"> <input type="button& ...

  3. ClassNotFoundException和 NoClassDefFoundError的区别

    ##### 1. 类型 ClassNotFoundException继承自Exception,属于java异常类.NoClassDefFoundError继承自Error,在java中Error一般属 ...

  4. git 使用merge 对本地分支进行合并 并进行代码提交的流程

    1.只有当将修改内容commit后 该修改才完全生效,进行merge前需要将两个分支修改的内容都进行commit 2.假设本地两个分支   用于开发的分支:dev    用于同步远程仓库的分支:mas ...

  5. Python(模块&包)

    参考:https://www.cnblogs.com/yuanchenqi/articles/5732581.html 在linux下给pycharm安装第三方库,需要在.bashrc中加: 因为对应 ...

  6. 使用deque模块固定队列长度,用headq模块来查找最大或最小的N个元素以及实现一个优先级排序的队列

    一. deque(双端队列) 1. 使用 deque(maxlen=N)会新建一个固定大小的队列.当新的元素加入并且这个队列已满的时候,最老的元素会自动被移除掉 >>> from c ...

  7. 浅谈HashMap原理,记录entrySet中的一些疑问

    HashMap的底层的一些变量: transient Node<K,V>[] table; //存储数据的Node数组 transient Set<java.util.Map.Ent ...

  8. 类型转换 / BOOL 类型

    /* Swift不允许隐式类型转换, 但可以使用显示类型转换(强制类型转换) OC: int intValue = 10; double doubleValue = (double)intValue; ...

  9. 【转载】基于Redis实现分布式锁

    背景在很多互联网产品应用中,有些场景需要加锁处理,比如:秒杀,全局递增ID,楼层生成等等.大部分的解决方案是基于DB实现的,Redis为单进程单线程模式,采用队列模式将并发访问变成串行访问,且多客户端 ...

  10. C#-WinForm-TextBox中只能输入数字的几种常用方法(C#)

    方法一: private void tBox_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == 0x20) e.KeyCh ...