操作系统源代码_第1页
操作系统源代码_第2页
操作系统源代码_第3页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

#include<stdio.h>#include<time.h>#include<stdlib.h>intmemoryStartAddress=-1;intmemorySize=-1;structjobListintid;/*作业ID*/intid;intsize;/*作业大小(需要的存储空间大小)*/intstatus;/*作业状态0:newjob,1:inthememory,2:finished.*/structjobList*next;};structjobList*next;};structfreeList/*作业链表指针*/{intstartAddress;{intstartAddress;/*分区起始地址*/intsize;structfreeList*next;intsize;structfreeList*next;};structusedList/*分区大小*/

/*分区链表指针*/{{intstartAddress;intjobID;structusedList*next;};voiderrorMessage(void)/*分区起始地址*//*分区中存放作业ID*//*分区链表指针*/*//*出现严重错误时显示信息并结束程序{*/printf("\n\tError!\a");printf("\nPressanykeytoexit!");getch();exit(1);}voidopenFile(FILE**fp,char*filename,char*mode)/*以要求的方式打开文件*/{if((*fp=fopen(filename,mode))==NULL){printf("\nCan'topen%sinmode%s.",filename,mode);errorMessage();}voidmakeFreeNode(structfreeList**empty,intstartAddress,intsize)/*根据参数startAddress、size创建空闲节点,由empty指针返回*/{if((*empty=malloc(sizeof(structfreeList)))==NULL){printf("\nNotenoughtoallocateforthefreenode.");errorMessage();}(*empty)->startAddress=startAddress;(*empty)->size=size;(*empty)->next=NULL;}voidiniMemory(void)/*初始化存储空间起始地址、大小*/{charMSA[10],MS[10];printf("\nPleaseinputthestartaddressofthememory!");scanf("%s",MSA);memoryStartAddress=atoi(MSA);printf("\nPleaseinputthesizeofthememory!");scanf("%s",MS);memorySize=atoi(MS);}charselectFitMethod(void) /*选择适应算法*/{FILE*fp;charfitMethod;do{printf("\n\nPleaseinputacharasfallowtoselectthefitmethod!\\n1(Bestfit)\\n2(Worstfit)\\n3(Firstfit)\\n4(Lastfit)\n");fitMethod=getche();}while(fitMethod<'1'||fitMethod>'4');openFile(&fp,"d:\\result.cl","a");switch(fitMethod){case'1':fprintf(fp,"\n\n\n\n\tBestfit");

fprintf(fp,"\n**********************************************");fprintf(fp,"\n**********************************************");break;case'2':fprintf(fp,"\n\n\n\n\tWorstfit");fprintf(fp,"\n**********************************************");fprintf(fp,"\n**********************************************");break;case'3':fprintf(fp,"\n\n\n\n\tFirstfit");fprintf(fp,"\n**********************************************");fprintf(fp,"\n**********************************************");break;case'4':fprintf(fp,"\n\n\n\n\tLastfit");fprintf(fp,"\n**********************************************");break;}fclose(fp);returnfitMethod;}voidinputJob(void)/*从键盘输入作业到D盘的JOB文件*/{int/*id,size,*/status=0,jobnum=0;FILE*fp;charid[10],size[10];openFile(&fp,"d:\\job.cl","w");fprintf(fp,"job_ID\tsize\tstatus");printf("\n\n\n\nPleaseinputthejobsasfallow!\\nEnteraintegersmallerthan1toquit.\njob_ID\tsize\n");do{/*scanf("%d%d",&id,&size);*/scanf("%s\t%s",id,size);if(atoi(id)>0&&atoi(size)>0){fprintf(fp,"\n%s\t%s\t%d",id,size,status);/*fprintf(fp,"\n%d\t%d\t%d",id,size,status);*/jobnum++;}elsebreak;}while(1);if(jobnum)printf("\nFinishedtoinputthejobs!");else{printf("\nNojobwasgiven.");errorMessage();}fclose(fp);}intmakeJobList(structjobList**jobs)/*从JOB文件中读出作业并创建作业链表*/{charjobID[10],size[10],status[10];structjobList*rear;FILE*fp;openFile(&fp,"d:\\job.cl","r");fscanf(fp,"%s%s%s",jobID,size,status);if((*jobs=malloc(sizeof(structjobList)))==NULL){printf("\nNotenoughtoallocateforthejob.");fclose(fp);errorMessage();}rear=*jobs;(*jobs)->next=NULL;while(!feof(fp)){structjobList*p;fscanf(fp,"%s%s%s",jobID,size,status);if((p=malloc(sizeof(structjobList)))==NULL){printf("\nNotenoughtoallocateforthejob.");fclose(fp);errorMessage();}p->next=rear->next;rear->next=p;rear=rear->next;rear->id=atoi(jobID);{{rear->size=atoi(size);rear->status=atoi(status);}fclose(fp);return0;}intupdateJobFile(structjobList*jobs) /*更新作业链表中作业的状态*/{FILE*fp;structjobList*p;openFile(&fp,"d:\\job.cl","w");fprintf(fp,"job_ID\tsize\tstatus");for(p=jobs->next;p;p=p->next)fprintf(fp,"\n%d\t%d\t%d",p->id,p->size,p->status);fclose(fp);return0;}intshowFreeList(structfreeList*empty) /*空闲分区队列显示*/{FILE*fp;structfreeList*p=empty->next;intcount=0;openFile(&fp,"d:\\result.cl","a");fprintf(fp,"\n\nNowshowthefreelist...");printf("\n\nNowshowthefreelist...");if(p){fprintf(fp,"\nnumber\tsize\tstartAddress");printf("\nnumber\tsize\tstartAddress");for(;p;p=p->next){fprintf(fp,"\n%d\t%d\t%d",++count,p->size,p->startAddress);printf("\n%d\t%d\t%d",count,p->size,p->startAddress);}fclose(fp);return1;}elsefprintf(fp,"\nThememorywasusedout!");printf("\nThememorywasusedout!");fclose(fp);return0;}}voidgetJobInfo(structjobList*jobs,intid,int*size,int*status)/*获取作业的信息*/{structjobList*p=jobs->next;while(p&&p->id!=id)p=p->next;if(p==NULL){printf("\nCan'tfindthejobwhichidis:%d.",id);errorMessage();}else{*size=p->size;*status=p->status;}}voidupdateJobStatus(structjobList**jobs,intid,intstatus){structjobList*p=(*jobs)->next;while(p&&p->id!=id)p=p->next;if(p==NULL){printf("\nCan'tfindthejobwhichidis:%d.",id);errorMessage();}elsep->status=status;}intshowUsedList(structjobList*jobs,structusedList*used)/*作业占用链表显示*/{FILE*fp;structusedList*p=used->next;intcount=0,size,status;openFile(&fp,"d:\\result.cl","a");fprintf(fp,"\n\nNowshowtheusedlist...");printf("\n\nNowshowtheusedlist...");if(p){fprintf(fp,"\nnumber\tjobID\tsize\tstartAddress");printf("\nnumber\tjobID\tsize\tstartAddress");for(;p;p=p->next){getJobInfo(jobs,p->jobID,&size,&status);fprintf(fp,"\n%d\t%d\t%d\t%d",++count,p->jobID,size,p->startAddress);printf("\n%d\t%d\t%d\t%d",count,p->jobID,size,p->startAddress);}fclose(fp);return1;}else{fprintf(fp,"\nNojobinthememory!Youshouldinputsomejobstoit.");printf("\nNojobinthememory!Youshouldinputsomejobstoit.");fclose(fp);return0;}}intshowJobList(structjobList*jobs)/*显示作业链表*/{structjobList*p;p=jobs->next;if(p==NULL){printf("\nNojobinthelist!Tryagainnexttime.");return0;}printf("\n\nThejoblistisasfallow:\njob_ID\tsize\tstatus");while(p){printf("\n%d\t%d\t%d",p->id,p->size,p->status);p=p->next;}{{return1;}voidmoveFragment(structjobList*jobs,structfreeList**empty,structusedList**used){intsize,status;structusedList*p;intaddress=memoryStartAddress;/*全局变量,初始化时分配存储空间始址*/if((*empty)->next==NULL) /*空闲分区链表为空,提示并返回*/{printf("\nThememorywasusedoutatall.\nMaybeyoushouldfinishsomejobsfirstorpressanykeytotryagain!");getch();return;}for(p=(*used)->next;p;p=p->next)/*循环的修改占用分区的始址*/{p->startAddress=address;getJobInfo(jobs,p->jobID,&size,&status);/*由作业ID获得作业大小*/address+=size;}(*empty)->next->startAddress=address;/*修改空闲分区的首节点始址、大小*/(*empty)->next->size=memorySize-(address-memoryStartAddress);(*empty)->next->next=NULL;/*删除首节点后的所有节点*/}voidorder(structfreeList**empty,intbySize,intinc){structfreeList*p,*q,*temp;intstartAddress,size;for(p=(*empty)->next;p;p=p->next){/*按bySize和inc两个参数寻找合适的节点,用temp指向它*/for(temp=q=p;q;q=q->next)switch(bySize){{{{case0:switch(inc){case0:if(q->size<temp->size)temp=q;break;default:if(q->size>temp->size)temp=q;break;}break;default:switch(inc){case0:if(q->startAddress<temp->startAddress)temp=q;break;default:if(q->startAddress>temp->startAddress)temp=q;break;}break;}}/*交换节点的成员值*/if(temp!=p){startAddress=p->startAddress;size=p->size;p->startAddress=temp->startAddress;p->size=temp->size;temp->startAddress=startAddress;temp->size=size;}}}intallocate(structfreeList**empty,intsize)/*为作业分配存储空间、状态必须为0*/{structfreeList*p,*prep;intstartAddress=-1;p=(*empty)->next;while(p&&p->size<size)p=p->next;if(p!=NULL)if(p->size>size){startAddress=p->startAddress;p->startAddress+=size;p->size-=size;}else{startAddress=p->startAddress;prep=*empty;while(prep->next!=p)prep=prep->next;prep->next=p->next;free(p);}}elseprintf("\nMaybeyoushouldmovethefragmenttogether.");/*Unsuccessful!*/returnstartAddress;}voidinsertUsedNode(structusedList**used,intid,intstartAddress) /*插入释放的空间到used链表中(作业号为 id,startAddress由函数13返回)*/{structusedList*q,*r,*prer;if((q=malloc(sizeof(structusedList)))==NULL){printf("\nNotenoughtoallocatefortheusednode.");errorMessage();}q->startAddress=startAddress;q->jobID=id;prer=*used;r=(*used)->next;while(r&&r->startAddress<startAddress){prer=r;r=r->next;}q->next=prer->next;prer->next=q;}intfinishJob(structusedList**used,intid,int*startAddress)/*结束一个作业号为id的作业,释放存储空间(由*startAddress返回空间的起始地址)*/{structusedList*p,*prep;prep=*used;p=prep->next;while(p&&p->jobID!=id){prep=p;p=p->next;}if(p==NULL){printf("\nThejobwhichidis:%disnotinthememory!",id);return0;}else{*startAddress=p->startAddress;prep->next=p->next;free(p);return1;}}voidinsertFreeNode(structfreeList**empty,intstartAddress,intsize)/*插入回收的空节点分区,处理回收分区与空闲分区的四种邻接关系。 */{structfreeList*p,*q,*r;for(p=*empty;p->next;p=p->next);/*处理链表尾部的邻接情况*/if(p==*empty||p->startAddress+p->size<startAddress)/*与尾部不相邻*/{makeFreeNode(&r,startAddress,size);/*通过r指针返回创建的空闲节点*/r->next=p->next; /*插入独立的空闲节点*/p->next=r;return;}if(p->startAddress+p->size==startAddress)/*与尾部上邻*/{{/*/*合并尾部节点*/p->size+=size;return;}q=(*empty)->next; /*处理链表首节点的邻接情况*/if(startAddress+size==q->startAddress)/*与首节点下邻*/{q->startAddress=startAddress; /*合并首节点*/q->size+=size;}elseif(startAddress+size<q->startAddress)/*与首节点不相邻*/{makeFreeNode(&r,startAddress,size);r->next=(*empty)->next;(*empty)->next=r;}else{/*处理链表中间的邻接情况*/while(q->next&&q->startAddress<startAddress){p=q;q=q->next;}if(p->startAddress+p->size==startAddress&&\q->startAddress==startAddress+size)/*上下邻,合并节点*/{p->size+=size+q->size;p->next=q->next;free(q);/*删除多余节点*/}elseif(p->startAddress+p->size==startAddress&&\q->startAddress!=startAddress+size)/*上邻,增加节点的大小*/{p->size+=size;}elseif(p->startAddress+p->size!=startAddress&&\q->startAddress==startAddress+size) /*下邻*/q->startAddress=startAddress;/*修改节点起始地址*/q->size+=size; /*修改节点的大小*/}else{/*上下不相邻*/makeFreeNode(&r,startAddress,size);r->next=p->next;p->next=r;}}}voidmain(void){charfitMethod;FILE*fp;structjobList*jobs;structfreeList*empty;structusedList*used;if((used=malloc(sizeof(structusedList)))==NULL){printf("\nNotenoughtoallocatefortheusednode.");errorMessage();}used->next=NULL;remove("d:\\result.cl");makeFreeNode(&empty,0,0);while(1){charch,step;intid,size,startAddress,status;structjobList*q;printf("\n1 Initializiation.\\n2 Putjobintomemory(allocatememory).\\n3 Finishjob(reusememory).\\n4 Showcurrentfreelist.\\n5 Showcurrentmemoryusedbyjobs.\\n6 Movefragmenttogether.\\n7 Exit.");printf("\nPleaseselectadigittocontinue.\n");step=getche();printf("\n");switch(step){case'1':openFile(&fp,"d:\\result.cl","a");fprintf(fp,"\n\n\tInitializiation:)");used->next=NULL;empty->next=NULL;iniMemory();makeFreeNode(&(empty->next),memoryStartAddress,memorySize);fprintf(fp,"\n\n\nDoyouwanttouseyourjobfiledirectly?\\nDefaultis\'N\'.Y/N:");printf("\n\n\nDoyouwanttouseyourjobfiledirectly?\\nDefaultis\'N\'.Y/N:\n");ch=getche();fprintf(fp,"\n%c",ch);fclose(fp);if(ch!='Y'&&ch!='y'){inputJob();}makeJobList(&jobs);if(ch=='Y'||ch=='y'){for(q=jobs->next;q;q=q->next){if(q->status==1){startAddress=allocate(&empty,q->size);if(startAddress!=-1){insertUsedNode(&used,q->id,startAddress);}}}}fitMethod=selectFitMethod();break;case'2':if(memoryStartAddress<0||memorySize<1){printf("\n\nBadmemoryallocated!\a");break;}openFile(&fp,"d:\\result.cl","a");fprintf(fp,"\n\n\tPutjobintomemory(allocatememory)...");fprintf(fp,"\n\n\nDoyouwanttoallocateforjobfromkeyboard?\\nDefaultis\'N\'.Y/N:");printf("\n\n\nDoyouwanttoallocateforjobfromkeyboard?\\nDefaultis\'N\'.Y/N:\n");ch=getche();fprintf(fp,"\n%c",ch);fclose(fp);if(ch!='Y'&&ch!='y'){for(q=jobs->next;q;q=q->next){if(q->status==0){switch(fitMethod){case'1':order(&empty,0,0);break;case'2':order(&empty,0,1);break;case'3':order(&empty,1,0);break;case'4':order(&empty,1,1);break;}startAddress=allocate(&empty,q->size);if(startAddress!=-1){insertUsedNode(&used,q->id,startAddress);updateJobStatus(&jobs,q->id,1);}}}updateJobFile(jobs);}else{showJobList(jobs);openFile(&fp,"d:\\result.cl","a");fprintf(fp,"\nPleaseinputajobidfromabove.");printf("\nPleaseinputajobidfromabove.");scanf("%d",&id);fprintf(fp,"%d\n",id);getJobInfo(jobs,id,&size,&status);switch(status){case0:printf("\nOk!Thejob'sstatusiscorrect!");fprintf(fp,"\nOk!Thejob'sstatusiscorrect!");fclose(fp);break;case1:printf("\nThejobwasinthememory!");fprintf(fp,"\nThejobwasinthememory!");fclose(fp);gotolabel;case2:printf("\nThejobwasfinished!");fprintf(fp,"\nThejobwasfinished!");fclose(fp);gotolabel;default:printf("\nUnexpectedjobstatus.Pleasecheckyoujobfile.");fprintf(fp,"\nUnexpectedjobstatus.Pleasecheckyoujobfile.");fclose(fp);errorMessage();}switch(fitMethod){case'1':order(&empty,0,0);break;case'2':order(&empty,0,1);break;case'3':order(&empty,1,0);break;case'4':order(&empty,1,1);break;}startAddress=allocate(&empty,size);if(startAddress!=-1){insertUsedNode(&used,id,startAddress);updateJobStatus(&jobs,id,1);updateJobFile(jobs);}

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论