《操作系统源代码》由会员分享,可在线阅读,更多相关《操作系统源代码(19页珍藏版)》请在金锄头文库上搜索。
1、#include#include#includeint memoryStartAddress = -1;int memorySize = -1;struct jobListint id; /* 作业ID */int size; /* 作业大小(需要的存储空间大小) */int status; /* 作业状态 0 : new job ,1 : in the memory , 2 : finished . */struct jobList *next; /* 作业链表指针 */;struct freeListint startAddress; /* 分区起始地址 */int size; /* 分区
2、大小 */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,c
3、har *mode) /*以要求的方式打开文件*/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 eno
4、ugh to allocate for 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(nPleas
5、e input the size of the memory !);scanf(%s,MS);memorySize = atoi(MS);char selectFitMethod(void) /*选择适应算法*/FILE *fp;char fitMethod;doprintf(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)
6、; openFile(&fp,d:result.cl,a);switch(fitMethod)case 1: fprintf(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 fitMe
7、thod;void inputJob(void) /*从键盘输入作业到D盘的JOB文件*/int /*id,size, */status = 0,jobnum = 0;FILE *fp;char id10,size10;openFile(&fp,d:job.cl,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
8、(%st%s,id,size);if(atoi(id) 0 & atoi(size) 0)fprintf(fp,n%st%st%d,id,size,status);/*fprintf(fp,n%dt%dt%d,id,size,status); */jobnum+;elsebreak;while(1);if(jobnum)printf(nFinished to input the jobs !);elseprintf(nNo job was given .);errorMessage();fclose(fp);int makeJobList(struct jobList *jobs) /*从JO
9、B文件中读出作业并创建作业链表*/char jobID10,size10,status10;struct jobList *rear;FILE *fp;openFile(&fp,d:job.cl,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(!feo
10、f(fp)struct jobList *p;fscanf(fp,%s%s%s,jobID,size,status);if(p = malloc(sizeof(struct jobList) = NULL)printf(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 = at
11、oi(status);fclose(fp);return 0;int updateJobFile(struct jobList *jobs) /*更新作业链表中作业的状态*/FILE *fp;struct jobList *p;openFile(&fp,d:job.cl,w);fprintf(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:result.cl,a);fprintf(fp,nnNow show the free list.);printf(nnNow show the free list.);if(p)fprintf(fp,nnumbertsizetstartAddress);printf(nnumbertsizetstartAddress);for(;p;p = p - next)fprintf(fp,n%dt%dt%d,+co