内存管理模拟实验 - PowerPoint PPT Presentation

phelan-vaughn
slide1 n.
Skip this Video
Loading SlideShow in 5 Seconds..
内存管理模拟实验 PowerPoint Presentation
Download Presentation
内存管理模拟实验

play fullscreen
1 / 19
Download Presentation
内存管理模拟实验
99 Views
Download Presentation

内存管理模拟实验

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. 内存管理模拟实验 • 要求:设计一个内存管理模拟系统并调试运行。 • 例:可变分区管理:存储分配算法采用首次适配算法。用外碎片的处理用“拼接”和“搬迁”技术。 12014/11/6

  2. 主要数据结构 • #define NULL 0 • #include "dos.h" • #include "mem.h" • #include "string.h" • #include "stdio.h" • #define total 5000//整个内存的大小 • #define setaddress 2000//用户区开始地址 • #define min 100//碎片界 • #define max 10//最大作业数 22014/11/6

  3. 主存分配表MAT typedef struct mat { char name[10]; int address; int length; struct mat *next; struct mat *back; }jobptr; jobptr *jobp; 32014/11/6

  4. 自由块链 typedef struct freearea { int address; int size; struct freearea *next; struct freearea *back; }freeptr; freeptr *freep; 42014/11/6

  5. 有关变量及模块 int totalfree;//总自由空间 int jobnumber; //作业数 void initiation(); //初始化代码 int ffallocation(); //分配模块 void showyou(); //显示当前内存状态 void ffcollection(); // 回收 void coalesce(); // 碎片处理 void menu(); //主菜单程序 52014/11/6

  6. 初始化 • void initiation() • { freep=(freeptr *)malloc(sizeof(freeptr)); • freep->size=total; • freep->address=setaddress; • freep->next=NULL; • freep->back=NULL; • totalfree=total; • jobp=NULL; • jobnumber=0; • } 62014/11/6

  7. int ffallocation(jl,jn) int jl;//作业长度 char jn[10]; //作业名 { int ja; freeptr *fp; jobptr *jp,*jp1,*jp2; ja=-1; if (totalfree<jl) { printf("can't allocation "); return; } ja=0; fp=freep; while(fp!=NULL) if (fp->size<jl) fp=fp->next; else {jobnumber++; totalfree=totalfree-jl; jp2=(jobptr *)malloc(sizeof(jobptr)); strcpy(jp2->name,jn); jp2->length=jl; jp2->address=fp->address; ja=jp2->address; if (jobp==NULL) { jp2->next=NULL; jp2->back=NULL; jobp=jp2; }

  8. else { jp=jobp; while ((jp != NULL) && (jp2->address<jp->address)) { jp1=jp; jp=jp->next; } jp2->next=jp; if (jp==NULL) { jp2->back=jp1; jp1->next=jp2; } else { jp2->back=jp->back; if (jp->back!=NULL) jp1->next=jp2; else jobp=jp2; jp->back=jp2; } } if (fp->size-jl<min) { if (fp->next!=NULL) fp->next->back=fp->back; if (fp->back!=NULL) fp->back->next=fp->next; else freep=fp->next; } else { fp->size=fp->size-jl; fp->address=fp->address+jl; } return (ja); } }

  9. 92014/11/6

  10. void showyou() {jobptr *jp; system("cls"); if (jobnumber<0) printf("no job\n"); else { printf(" name length address\n"); jp=jobp; while (jp!=NULL) { printf("%11s%11d%13d\n",jp->name,jp->length,jp->address); jp=jp->next; } } printf("the total left is %d byte\n",totalfree); }

  11. void ffcollection(jn) char jn[10]; { freeptr *fp,*fp1,*fp2; jobptr *jp; int f; jp=jobp; f=0; while ( (jp!=NULL) && strcmp(jp->name,jn) ) jp=jp->next; if (jp!=NULL) { jobnumber--; totalfree=totalfree+jp->length; if (freep==NULL) { freep=(freeptr *)malloc(sizeof(freeptr)); freep->address=jp->address; freep->size=jp->length; freep->next=NULL; freep->back=NULL; }

  12. else { fp=freep; while ( (fp!=NULL) && (fp->address<jp->address) ) { fp1=fp; fp=fp->next; } if (fp!=NULL) { if ((fp->next!=NULL) && (fp->next->address==jp->address+jp->length)) f=f+1; if ((fp->back!=NULL) && (jp->address==fp->address+fp->size)) f=f+2; } else if ( jp->address==fp1->address+fp1->size ) f=f+2;

  13. switch(f) { case 0:{ fp2=(freeptr *)malloc(sizeof(freeptr)); fp2->address=jp->address; fp2->size=jp->length; fp2->next=fp; if (fp!=NULL) { fp2->back=fp->back; if (fp->back!=NULL) fp1->next=fp2; else freep=fp2; fp->back =fp2; } else { fp2->back=fp1; fp1->next=fp2; } break; }

  14. case 1:{ fp->size=fp->size+jp->length; fp->address=jp->address; break; } case 2: { fp1->size=fp->size+jp->length;break; } case 3:{ fp->size=fp1->size+jp->length+fp->size; fp1->next=fp->next; if(fp->next!=NULL) { fp->next->back=fp2; free(fp); } break; } } } if (jp==jobp) jobp=jp->next; if (jp->next!=NULL) jp->next->back=jp->back; if (jp->back!=NULL) jp->back->next=jp->next; free(jp); } if (!(strcmp(jp->name,jn))) printf("cant't find %s,so can't collect %s",jn,jn); }

  15. void coalesce() { freeptr *fp,*fp1; jobptr *jp;int bottom; if (jobnumber>0) { jp=jobp; bottom=total+setaddress; while (jp!=NULL) { jp->address=bottom-jp->length; bottom=bottom-jp->length; jp=jp->next; } fp=freep; while (fp!=NULL) { fp1=fp; fp=fp->next; free(fp1); } freep=(freeptr *)malloc(sizeof(freeptr)); freep->size=totalfree; freep->address=setaddress; freep->next=NULL; freep->back=NULL; } }

  16. void menu() { char name[10];int length,address; int select=0; while (select!=4) { system("cls"); printf("\n"); printf(" ************ memory management program **********\n"); printf("\n"); printf("\n"); printf(" you can select one of the following:\n" ); printf("____________________________________________________\n"); printf(" (1) require to be allocate.\n"); printf(" (2) require to collecte the size.\n"); printf(" (3) check the memory.\n"); printf(" (4) quit.\n"); printf("____________________________________________________\n"); printf("\n"); printf("\n"); printf(" which would you select ? please inpute 1,2,3,4.\n");

  17. scanf("%d",&select); while ( (select!=1) && (select!=2) && (select!=3) && (select!=4) ) scanf("%d",&select); switch(select) { case 1: { if ( jobnumber>max ) printf("the job is too many.\n"); else { printf("enter you job name:\n"); scanf("%s",name); printf("enter you job length:\n"); scanf("%d",&length); address=ffallocation(length,name); switch(address) { case -1: printf("the momory is full.\n"); break; case 0: { coalesce(); ffallocation(length,name); showyou();break; } default: showyou();break; } } getch();break; }

  18. case 2: { printf("\nenter the name of the job want to collect the size:\n"); scanf("%s",name); ffcollection(name); showyou(); getch();break; } case 3: { showyou(); getch();break; } case 4: exit(1);break; } } }

  19. main() { initiation(); menu(); getch(); } 192014/11/6