最佳分配算法模拟

 
这个程序是模拟固定分区分配的最佳分配算法的,首先你来根据提示输入分区个数(用户区)和各分区大小(不要太大,小于100即可,为的是整个一屏能显示出来),回车就会显示出内存分配情况,最上边的一块为系统区。然后,你可以申请空间若成功分配则会给出分配后的内存示意图(填过色的就是分配的区),并且可以连续分配。源码如下: optiAlloc.rar
 
/*****************************
Wandy Wang 2005-4-10
*****************************/
/*固定分区分配的最佳分配算法模拟*/
 
#i nclude<stdio.h>
#i nclude<graphics.h>
#i nclude<string.h>
#i nclude<stdlib.h>
 
/*定义分区表结构*/
struct areaTable
{
 int start;
 int length;
 int state;
};
 
/*****************/
/*手动分区表*/
 
int initAT(struct areaTable* at,int n)
{
 int i;/*for loop*/
 printf("Please Allocate The Memory By Yourselt:/n");
 
 for(i=0;i<n;i++)
 {
     printf("Input the %d-area length:",i);
     scanf("%d",&at[i].length);
 
     if(i==0) at[i].start=40;
     else at[i].start=at[i-1].start+at[i-1].length;
     at[i].state=0;
 }
 return 0;
}
 
/*****************/
/*根据最佳分配算法分配*/
int optiAlloc(struct areaTable* at,int n,int ask)
{
   int difference=1000;
   int areaNo=-1;
   int i;
 
   for(i=0;i<n;i++)
   {
       if(at[i].state==0&&at[i].length>=ask&&((at[i].length-ask)<difference))
       {
       difference=at[i].length-ask;
       areaNo=i;
       }
   }
   return areaNo;
}
 
/*****************/
/*根据分区表画图*/
int drawAT(struct areaTable* at,int n)
{
 
 int gdriver,gmode;
 int i=0;
 int left,right;
 char str10[10];
 char str40[40];
 
 /*设定图形模式*/
 detectgraph(&gdriver,&gmode);
 initgraph(&gdriver,&gmode,"c://tc");
 
 /*画图*/
 
    left = getmaxx() / 2 - 50;
    right = getmaxx() / 2 + 50;
 
    setcolor(2);
    setfillstyle(SOLID_FILL,6);
    rectangle(left,50,right,50+at[0].start);
    floodfill(left+2,50+2,2);
    outtextxy(right+5,50,"system area len=50");
 
    setcolor(2);
    setfillstyle(SOLID_FILL,4);
     for(i=0;i<n;i++)
    {
    rectangle(left,50+at[i].start,right,50+at[i].start+at[i].length);       if(at[i].state==1) floodfill(left+2,50+at[i].start+2,2);
    if(at[i].state!=0) floodfill(left+2,50+at[i].start+2,2);
 
    itoa(i,str10,10);
    strcpy(str40,str10);
 
    strcat(str40," len=");
 
    itoa(at[i].length,str10,10);
    strcat(str40,str10);
 
    strcat(str40,at[i].state?" FULL":" FREE");
 
    outtextxy(right+5,50+at[i].start,str40);
    }
 
 /*暂停屏幕并关闭图形模式*/
 getch();
 clrscr();
 closegraph();
 return 0;
}
 
/*****************/
void main()
{
 struct areaTable* at;
 int ask;
 int n;
 int allocNo=-1;
 char ch=' ';
 
 clrscr();
 printf("Input how many area you allocate:");
 scanf("%d",&n);
 at=(struct areaTable*)malloc(n*sizeof(struct areaTable));
 
 initAT(at,n);
 drawAT(at,n);
 
 do{
     printf("/nInput the mem length you want:");
     scanf("%d",&ask);
     allocNo = optiAlloc(at,n,ask);
     if(allocNo!=-1)
     {
       printf("Allocate Successfully!/n");
       at[allocNo].state=1;
       drawAT(at,n);
     }
     else
     {
      printf("Allocate Failed");
     }
     printf("/ndo you want contimue to allocate(Y or N):");
     ch=getch();
     putch(ch);
 }while(ch=='Y'||ch=='y');
 
 free(at);
 getch();
}
 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值