最佳适应(BestFit)算法

基本思想:BF的空闲分区表(或空闲分区链)按空闲分区大小的升序方式组织。分配时,按空闲分区表(或空闲分区链)的先后次序,从头查找,找到符合要求的第一个分区。就说明它是最适合的。大的空闲分区可以被保留下来

#include  
#include  
#include  
#define NULL 0 
typedef struct table 
{
    int address;     /*存储分区起始地址*/ 
    int length;      /*存储分区长度*/ 
    int flag;        /*存储分区标志,0 为空闲,1 为被作业占据*/ 
    char name[10];   /*当flag==1 时存储分区占用标志作业名,否则存储空nil*/ 
    struct table *next; 
}node; 

int success=0;  /*分配成功与否的标志*/ 

node *insert(node *head, node *p)  /*按照“地址递增方式”将p 结点插入链表相应位置 参考优先级*/ 
  { 
    node *r,*q;
    r=head;
    if(head->flag)
{
    if(r->address>p->address)
    {
        p->next=r;
        return p;
    }
    else
    {
        while(r->addressaddress)
        {
             if(r->next==NULL)
             {
                 r->next=p;
                 return head;
             }
             q=r;
             r=r->next;
        }
    }
      q->next=p;
       p->next=r;
       return head;
}
    else
{
    if(r->length>p->length)
    {
        p->next=r;
        return p;
    }
    else
    {
        while(r->lengthlength)
        {
             if(r->next==NULL)
             {
                 r->next=p;
                 return head;
             }
             q=r;
             r=r->next;
        }
    }
      q->next=p;
       p->next=r;
       return head;
    }

}

node *creat()   /*根据地址递增方式建立分配分区表(flag==1)或空闲分区表(flag==0)*/ 
{ 
   node *head,*p1;
   int n=0; 
   char a[10]="nil";
   head=NULL;
   //建立分配分区表
   printf("address  length  flag<0 or 1>\n");
   p1=(node *)malloc(sizeof(node));
   scanf("%d %d %d",&p1->address,&p1->length,&p1->flag);
   while(p1->length!=0||p1->address!=0)
    {
        if(p1->flag)
   {
       printf("\t\tinput job_name:");
        scanf("%s",&p1->name);
   }
   else
   {
       strcpy(p1->name, "nil"); 
   }
   p1->next=NULL;
       n=n+1;
   if (n==1) {head=p1;p1->next=NULL;}
     else head=insert(head,p1);
     p1=(node *) malloc (sizeof(node));
   scanf("%d",&p1->address);
   scanf("%d",&p1->length);
   scanf("%d",&p1->flag);

    }
   return head;
} 
void print (node *head) /*输出链表*/ 
{
    node *p;
    p=head;
    if(p->flag){
        printf("The distributed table is !\n");
    }
    else{
        printf("The free table is !\n");
    }

    if(!p)
    {
        printf("该链表为空!\n");
    }
    else{
        while(p)
        {
        printf("%d,%d,%d,%s\n",p->address,p->length,p->flag,p->name);
        p=p->next;
        }
        printf("\n");
    }
} 
void distribute(node *freehead, node *distributedhead, node *work) 
/*在空闲分区表中找出首次合适work 的分区,同时修改空闲分区表和分配分区表*/ 
{ //填补程序
 node *p,*q,*r,*t,*news;
 int a,b,c,d;
  p=freehead;
  q=distributedhead;
  while(p)
  {
      if(p->length>=work->length)
      {
        d=p->address;
        p->address=p->address+work->length;
        p->length=p->length-work->length;
        if(p->length==0)
        {
            t->next=p->next;
        }
        success=1;
        r=t;
        break;
      }
      else
      {
          t=p;
          p=p->next;
      }
  }
  if(success)
  {
      while(q)
      {
          a=q->address;
          b=q->length;
          c=a+b;
        if(c==d)
        {
      news=(node *)malloc(sizeof(node));
      news->address=c;
      news->length=work->length;
      news->flag=1;
      printf("\ndistributing is successful!\n");
       strcpy(news->name, work->name); 
      news->next=q->next;
      q->next=news;
      print(freehead);
      print(distributedhead);
      return;
         }
       q=q->next;
      }
  }
  else{
     printf("\ndistributing is not successful!\n");
      print(freehead);
      print(distributedhead);
  }
} 


void main() 
 { 

    struct table *dtable,*ftable,*work; 
    char workn[10]; 
    int a; 
    printf("\nThe distributed table is:\n"); 
    dtable=creat();
//  print(dtable);
        /*dtable 输入已分配情况表*/ 
    printf("\nThe free table is:\n"); 
    ftable=creat();          
//  print(ftable);/*ftable 输入未分配情况表*/ 
     while(1)
     {
         work=(node *)malloc(sizeof(node));
         printf("\nThe length of worked job is:");
         scanf("%d",&work->length);
         printf("The name of worked is:");
         scanf("%s",&workn);
         strcpy(work->name, workn); 
         distribute(ftable,dtable,work);
     } 
 } 

你可能感兴趣的:(计算机操作系统)