最差适应算法
#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);
}
05-11 17:43