操作系统源代码

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

#includestdio.h#includetime.h#includestdlib.hintmemoryStartAddress=-1;intmemorySize=-1;structjobList{intid;/*作业ID*/intsize;/*作业大小(需要的存储空间大小)*/intstatus;/*作业状态0:newjob,1:inthememory,2:finished.*/structjobList*next;/*作业链表指针*/};structfreeList{intstartAddress;/*分区起始地址*/intsize;/*分区大小*/structfreeList*next;/*分区链表指针*/};structusedList{intstartAddress;/*分区起始地址*/intjobID;/*分区中存放作业ID*/structusedList*next;/*分区链表指针*/};voiderrorMessage(void)/*出现严重错误时显示信息并结束程序*/{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**********************************************);break;case'2':fprintf(fp,\n\n\n\n\tWorstfit);fprintf(fp,\n**********************************************);break;case'3':fprintf(fp,\n\n\n\n\tFirstfit);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;}else{fprintf(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);retu

1 / 19
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功