操作系统源代码.pdf
《操作系统源代码.pdf》由会员分享,可在线阅读,更多相关《操作系统源代码.pdf(23页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、 操作系统源代码 Prepared on 21 November 2021#include#include#include int memoryStartAddress=-1;int memorySize=-1;struct jobList int id;/*作业 ID*/int size;/*作业大小(需要的存储空间大小)*/int status;/*作业状态 0:new job,1:in the memory,2:finished.*/struct jobList*next;/*作业链表指针*/;struct freeList int startAddress;/*分区起始地址*/int
2、size;/*分区大小*/struct freeList*next;/*分区链表指针*/;struct usedList int startAddress;/*分区起始地址*/int jobID;/*分区中存放作业 ID*/struct usedList*next;/*分区链表指针*/;void errorMessage(void)/*出现严重错误时显示信息并结束程序*/printf(ntError!a);printf(nPress any key to exit!);getch();exit(1);void openFile(FILE*fp,char*filename,char*mode)/
3、*以要求的方式打开文件*/if(*fp=fopen(filename,mode)=NULL)printf(nCant open%s in mode%s.,filename,mode);errorMessage();void makeFreeNode(struct freeList*empty,int startAddress,int size)/*根据参数 startAddress、size 创建空闲节点,由 empty 指针返回*/if(*empty=malloc(sizeof(struct freeList)=NULL)printf(nNot enough to allocate for
4、the free node.);errorMessage();(*empty)-startAddress=startAddress;(*empty)-size=size;(*empty)-next=NULL;void iniMemory(void)/*初始化存储空间起始地址、大小*/char MSA10,MS10;printf(nPlease input the start address of the memory!);scanf(%s,MSA);memoryStartAddress=atoi(MSA);printf(nPlease input the size of the memory!
5、);scanf(%s,MS);memorySize=atoi(MS);char selectFitMethod(void)/*选择适应算法*/FILE*fp;char fitMethod;do printf(nnPlease input a char as fallow to select the fit method!n 1(Best fit)n 2(Worst fit)n 3(First fit)n 4(Last fit)n);fitMethod=getche();while(fitMethod 4);openFile(&fp,d:,a);switch(fitMethod)case 1:f
6、printf(fp,nnnntBest fit);fprintf(fp,n*);break;case 2:fprintf(fp,nnnntWorst fit);fprintf(fp,n*);break;case 3:fprintf(fp,nnnntFirst fit);fprintf(fp,n*);break;case 4:fprintf(fp,nnnntLast fit);fprintf(fp,n*);break;fclose(fp);return fitMethod;void inputJob(void)/*从键盘输入作业到 D 盘的 JOB 文件*/int /*id,size,*/sta
7、tus=0,jobnum=0;FILE*fp;char id10,size10;openFile(&fp,d:,w);fprintf(fp,job_IDtsizetstatus);printf(nnnnPlease input the jobs as fallow!nEnter a integer smaller than 1 to quit.njob_IDtsizen);do /*scanf(%d%d,&id,&size);*/scanf(%st%s,id,size);if(atoi(id)0&atoi(size)0)fprintf(fp,n%st%st%d,id,size,status);
8、/*fprintf(fp,n%dt%dt%d,id,size,status);*/jobnum+;else break;while(1);if(jobnum)printf(nFinished to input the jobs!);else printf(nNo job was given.);errorMessage();fclose(fp);int makeJobList(struct jobList*jobs)/*从 JOB 文件中读出作业并创建作业链表*/char jobID10,size10,status10;struct jobList*rear;FILE*fp;openFile(
9、&fp,d:,r);fscanf(fp,%s%s%s,jobID,size,status);if(*jobs=malloc(sizeof(struct jobList)=NULL)printf(nNot enough to allocate for the job.);fclose(fp);errorMessage();rear=*jobs;(*jobs)-next=NULL;while(!feof(fp)struct jobList*p;fscanf(fp,%s%s%s,jobID,size,status);if(p=malloc(sizeof(struct jobList)=NULL)pr
10、intf(nNot enough to allocate for the job.);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);return 0;int updateJobFile(struct jobList*jobs)/*更新作业链表中作业的状态*/FILE*fp;struct jobList*p;openFile(&fp,d:,w);fpr
11、intf(fp,job_IDtsizetstatus);for(p=jobs-next;p;p=p-next)fprintf(fp,n%dt%dt%d,p-id,p-size,p-status);fclose(fp);return 0;int showFreeList(struct freeList*empty)/*空闲分区队列显示*/FILE*fp;struct freeList*p=empty-next;int count=0;openFile(&fp,d:,a);fprintf(fp,nnNow show the free list.);printf(nnNow show the fre
12、e list.);if(p)fprintf(fp,nnumbertsizetstartAddress);printf(nnumbertsizetstartAddress);for(;p;p=p-next)fprintf(fp,n%dt%dt%d,+count,p-size,p-startAddress);printf(n%dt%dt%d,count,p-size,p-startAddress);fclose(fp);return 1;else fprintf(fp,nThe memory was used out!);printf(nThe memory was used out!);fclo
13、se(fp);return 0;void getJobInfo(struct jobList*jobs,int id,int*size,int*status)/*获取作业的信息*/struct jobList*p=jobs-next;while(p&p-id!=id)p=p-next;if(p=NULL)printf(nCant find the job which id is:%d.,id);errorMessage();else *size=p-size;*status=p-status;void updateJobStatus(struct jobList*jobs,int id,int
14、 status)struct jobList*p=(*jobs)-next;while(p&p-id!=id)p=p-next;if(p=NULL)printf(nCant find the job which id is:%d.,id);errorMessage();else p-status=status;int showUsedList(struct jobList*jobs,struct usedList*used)/*作业占用链表显示*/FILE*fp;struct usedList*p=used-next;int count=0,size,status;openFile(&fp,d
15、:,a);fprintf(fp,nnNow show the used list.);printf(nnNow show the used list.);if(p)fprintf(fp,nnumbertjobIDtsizetstartAddress);printf(nnumbertjobIDtsizetstartAddress);for(;p;p=p-next)getJobInfo(jobs,p-jobID,&size,&status);fprintf(fp,n%dt%dt%dt%d,+count,p-jobID,size,p-startAddress);printf(n%dt%dt%dt%d
16、,count,p-jobID,size,p-startAddress);fclose(fp);return 1;else fprintf(fp,nNo job in the memory!You should input some jobs to it.);printf(nNo job in the memory!You should input some jobs to it.);fclose(fp);return 0;int showJobList(struct jobList*jobs)/*显示作业链表*/struct jobList*p;p=jobs-next;if(p=NULL)pr
17、intf(nNo job in the list!Try again next time.);return 0;printf(nnThe job list is as fallow:njob_IDtsizetstatus);while(p)printf(n%dt%dt%d,p-id,p-size,p-status);p=p-next;return 1;void moveFragment(struct jobList*jobs,struct freeList*empty,struct usedList*used)int size,status;struct usedList*p;int addr
18、ess=memoryStartAddress;/*全局变量,初始化时分配存储空间始址*/if(*empty)-next=NULL)/*空闲分区链表为空,提示并返回*/printf(nThe memory was used out at all.nMay be you should finish some jobs first or press any key to try again!);getch();return;for(p=(*used)-next;p;p=p-next)/*循环的修改占用分区的始址*/p-startAddress=address;getJobInfo(jobs,p-jo
19、bID,&size,&status);/*由作业 ID 获得作业大小*/address+=size;(*empty)-next-startAddress=address;/*修改空闲分区的首节点始址、大小*/(*empty)-next-size=memorySize-(address-memoryStartAddress);(*empty)-next-next=NULL;/*删除首节点后的所有节点*/void order(struct freeList*empty,int bySize,int inc)struct freeList*p,*q,*temp;int startAddress,si
20、ze;for(p=(*empty)-next;p;p=p-next)/*按 bySize 和 inc 两个参数寻找合适的节点,用 temp 指向它*/for(temp=q=p;q;q=q-next)switch(bySize)case 0:switch(inc)case 0:if(q-size size)temp=q;break;default:if(q-size temp-size)temp=q;break;break;default:switch(inc)case 0:if(q-startAddress startAddress)temp=q;break;default:if(q-star
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 操作系统 源代码
限制150内