最差适应算法
#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. Android ImageView,ImageButton 与 Button

    1. ImageButton 继承自 ImageView.两者具备甚小,因为 ImageView 同样可以点击相应,同样有点击的阴影效果.实际上他们的区别在于默认 style.比如同样放一个背景和一个 ...

  2. 【Oracle 12c】最新CUUG OCP-071考试题库(55题)

    55.(13-3) choose the best answer: Which statement is true regarding the SESSION_PRIVS dictionary vie ...

  3. 【Oracle 12c】最新CUUG OCP-071考试题库(53题)

    53.(12-14) choose the best answer: Examine the command to create the BOOKS table. SQL>CREATE TABL ...

  4. 【OCP认证12c题库】CUUG 071题库考试原题及答案(27)

    27.choose two The SQL statements executed in a user session are as follows: SQL> CREATE TABLE pro ...

  5. powerdesign设计、实现简单的数据库模型

    1,新建CDM,打开powerdesign,选择Categories----Infoomation------Conceptual Data.开始画图,如果此时如表示关系的图标是灰色的.Tools-- ...

  6. Requests Header | Http Header

    Requests Header | Http Header Header 解释 示例 Accept 指定客户端能够接收的内容类型 Accept: text/plain, text/html Accep ...

  7. 老男孩Day12作业:RabbitMQ-RPC版主机管理程序

    一.作业需求 1.可以对指定机器异步的执行多个命令 例子: 请输入操作指令>>>:run ipconfig --host 127.0.0.0 in the call     tack ...

  8. iOS学习笔记(5)——显示简单的TableView

    1. 创建工程 创建一个新的Xcode工程命名为SimpleTableTest. 删除main.storyboard文件和info.plist中有关storyboard的相关属性. 按command+ ...

  9. Java_异常处理(Exception)

    异常:Exception try{ //捕获异常 }catch{ //处理异常 } 异常处理机制: 1.在try块中,如果捕获了异常,那么剩余的代码都不会执行,会直接跳到catch中, 2.在try之 ...

  10. localhost, 127.0.0.1, 0.0.0.0

    总结: localhost:是一个域名.域名可以认为是某个ip的别称,便于记忆.通常localhost对应的ip是127.0.0.1,不过这个也可以设置,参见知乎回答 127.0.0.1:是一个回环地 ...