操作系统课程设计代码

  今天我完成操作系统课程设计,模拟可变分区管理,代码如下: #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #define MAPSIZE 100 int N=0; int job[10][2]; struct map //存储资源表结构 { int m_addr;//存储空闲首地址 int m_size;//存储空闲分区大小 }; struct map map[MAPSIZE]; //存储资源表结构体数组 int BF_malloc(struct map *mp,int size) { register int a,s; register struct map *bp,*bpp; for(bp=mp;bp->m_size;bp++) { if(bp->m_size>=size)//SIZE是指作业大小 { //最佳适应 a=bp->m_addr; s=bp->m_size; for(bpp=bp;bpp->m_size;bpp++) { if(bpp->m_size>=size&&bpp->m_sizem_addr; s=bpp->m_size; bp=bpp; } } bp->m_addr+=size; if((bp->m_size-=size)==0) do { bp++; (bp-1)->m_addr=bp->m_addr; } while((bp-1)->m_size=bp->m_size); return(a); } } return -1; } int WF_malloc(struct map *mp,int size)//WF存储分配函数 { register int a,s; register struct map *bp,*bpp; for (bp=mp;bp->m_size;bp++) { if(bp->m_size>=size)//从分区中找出m_size最大的分区进行分配 { a=bp->m_addr; s=bp->m_size; for(bpp=bp;bpp->m_size;bpp++) { //最坏适应 if(bpp->m_size>s) { a=bpp->m_addr; s=bpp->m_size; bp=bpp; } } bp->m_addr+=size; if((bp->m_size-=size)==0) do { bp++; (bp-1)->m_addr=bp->m_addr; } while((bp-1)->m_size=bp->m_size); return(a); } } return(-1); } void mfree(struct map *mp,int aa,int size)//存储释放函数 { register struct map *bp; register int t; register int a; a=aa; for(bp=mp;bp->m_addr<=a&&bp->m_size!=0;bp++); if(bp>mp&&(bp-1)->m_addr+(bp-1)->m_size==a) {//与前合并 (bp-1)->m_size+=size; if(a+size==bp->m_addr) {//前后合并 (bp-1)->m_size+=bp->m_size; while (bp->m_size) { bp++; (bp-1)->m_addr=bp->m_addr; (bp-1)->m_size=bp->m_size; } } } else { if(a+size==bp->m_addr&&bp->m_size) {//与后合并 bp->m_addr-=size; bp->m_size+=size; } else if (size) do {//无合并 t=bp->m_addr; bp->m_addr=a; a=t; t=bp->m_size; bp->m_size=size; bp++; } while (size=t); } } void dele(int job[10][2],int a,int s) //从作业内存中将已回收内存删除 { for(int i=0;im_addr=addr;//将首地址付给节点 bp->m_size=size;//将大小付给节点 (++bp)->m_size=0; //表尾 } void show_map()//显示空闲内存 { int i=0; struct map *bp; bp=map; printf("Current memory map.../n"); printf("Address Size/n"); while(bp->m_size!=0) { printf("<%d/t/t%d>/n",bp->m_addr,bp->m_size); bp++; } } void moveFragment(struct map *mp) //碎片拼接 { int sum=0; struct map *bp; bp=mp; sum=0; int addr=bp->m_addr; for(;bp->m_size;bp++) sum=sum+bp->m_size; mp->m_addr=addr; mp->m_size=sum; (++mp)->m_size=0;//尾表 } void main() { int a,s; int job[10][2]; char c; int i,j=0; printf("--------------可变分区存储管理--------------/n1--------------初始化/n2--------------内存分配/n3--------------显示作业内存分配/n4--------------显示空闲内存/n5--------------内存回收/n6--------------碎片拼节/n7--------------退出/n"); do { printf( "please select a choice:"); scanf("%d",&i); switch(i) { case 1: init(); break;N=0; case 2: printf("please input ,b for BF,w for WF:"); cin>>c; printf("please input size:"); scanf("%d",&s); if(c=='b') a=BF_malloc(map,s); else a=WF_malloc(map,s); if (a==-1) printf("request cant't be satisfied/n"); else printf("alloc memory at address:%d,size:%d/n",a,s); N++; job[j][0]=a; job[j][1]=s; j++; break; case 3: printf("Show the jobs list:/n"); printf("Address Size/n"); for(j=0;j
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值