操作系統(tǒng)源代碼復(fù)習(xí)過程_第1頁
操作系統(tǒng)源代碼復(fù)習(xí)過程_第2頁
操作系統(tǒng)源代碼復(fù)習(xí)過程_第3頁
操作系統(tǒng)源代碼復(fù)習(xí)過程_第4頁
已閱讀5頁,還剩17頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、操作系統(tǒng)源代碼精品文檔#include<stdio.h>#include<time.h>#include<stdlib.h>int memoryStartAddress = -1;int memorySize = -1;struct jobListint id;/* 作業(yè) ID */int size;/* 作業(yè)大?。ㄐ枰拇鎯臻g大小)*/int status;/* 作業(yè)狀態(tài) 0 : new job ,1 : in the memory , 2 : finished . */struct jobList *next;/* 作業(yè)鏈表指針 */;struct f

2、reeListint startAddress;/* 分區(qū)起始地址 */int size;/* 分區(qū)大小 */struct freeList *next;/* 分區(qū)鏈表指針 */;struct usedListint startAddress;/* 分區(qū)起始地址 */int jobID;/* 分區(qū)中存放作業(yè)ID */struct usedList *next;/* 分區(qū)鏈表指針 */;void errorMessage(void)/*出現(xiàn)嚴(yán)重錯誤時顯示信息并結(jié)束程序*/printf("ntError !a");printf("nPress any key to e

3、xit !");收集于網(wǎng)絡(luò),如有侵權(quán)請聯(lián)系管理員刪除精品文檔getch();exit(1);void openFile(FILE *fp,char *filename,char *mode)/*以要求的方式打開文件 */if(*fp = fopen(filename,mode) = NULL)printf("nCan't open %s in mode %s.",filename,mode); errorMessage();void makeFreeNode(struct freeList *empty,int startAddress,int size)

4、/* 根據(jù)參數(shù) startAddress、size創(chuàng)建空閑節(jié)點,由empty 指針返回 */if(*empty = malloc(sizeof(struct freeList) = NULL)printf("nNot enough to allocate for the free node ."); errorMessage();(*empty)->startAddress = startAddress;(*empty)->size = size;(*empty)->next = NULL;void iniMemory(void)/* 初始化存儲空間起始地

5、址、大小*/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 !"); scanf("%s",MS);memorySize = atoi(MS);char selectFitMethod(void)/*選擇適應(yīng)算法 */收集于網(wǎng)絡(luò),如

6、有侵權(quán)請聯(lián)系管理員刪除精品文檔FILE *fp;char fitMethod;doprintf("nnPlease input a char as fallow to select the fit method !n1(Best fit) n2(Worst fit) n3(First fit) n4(Last fit)n");fitMethod = getche();while(fitMethod < '1' | fitMethod > '4');openFile(&fp,"d:result.cl",&

7、quot;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;cas

8、e '4': fprintf(fp,"nnnntLast fit");fprintf(fp,"n*");break;fclose(fp);return fitMethod;void inputJob(void)/* 從鍵盤輸入作業(yè)到D 盤的 JOB 文件 */int /*id,size, */status = 0,jobnum = 0;收集于網(wǎng)絡(luò),如有侵權(quán)請聯(lián)系管理員刪除精品文檔FILE *fp;char id10,size10;openFile(&fp,"d:job.cl","w");fp

9、rintf(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,"

10、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)/*從 JOB 文件中讀出作業(yè)并創(chuàng)建作業(yè)

11、鏈表*/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();收集于

12、網(wǎng)絡(luò),如有侵權(quán)請聯(lián)系管理員刪除精品文檔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)printf("nNot enough to allocate for the job ."); fclose(fp);errorMessage();p -> next = rear -> next;rea

13、r -> 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)/* 更新作業(yè)鏈表中作業(yè)的狀態(tài)*/FILE *fp;struct jobList *p;openFile(&fp,"d:job.cl","w");fprintf(fp,&qu

14、ot;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)/* 空閑分區(qū)隊列顯示 */FILE *fp;struct freeList *p = empty -> next;收集于網(wǎng)絡(luò),如有侵權(quán)請聯(lián)系管理員刪除精品文檔int count = 0;ope

15、nFile(&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

16、",+count,p -> size,p ->startAddress);printf("n%dt%dt%d",count,p -> size,p -> startAddress);fclose(fp);return 1;elsefprintf(fp,"nThe memory was used out !");printf("nThe memory was used out !");fclose(fp);return 0;void getJobInfo(struct jobList *jobs,int

17、 id,int *size,int *status)/*獲取作業(yè)的信息 */struct jobList *p = jobs->next;while(p && p->id != id)p = p->next;if(p = NULL)printf("nCan't find the job which id is : %d .",id); errorMessage();else*size = p -> size;收集于網(wǎng)絡(luò),如有侵權(quán)請聯(lián)系管理員刪除精品文檔*status = p -> status;void updateJo

18、bStatus(struct jobList *jobs,int id,int status)struct jobList *p = (*jobs)->next;while(p && p->id != id)p = p->next;if(p = NULL)printf("nCan't find the job which id is : %d .",id); errorMessage();elsep -> status = status;int showUsedList(struct jobList *jobs,struct

19、usedList *used)/* 作業(yè)占用鏈表顯示 */FILE *fp;struct usedList *p = used -> next;int count = 0,size,status;openFile(&fp,"d:result.cl","a");fprintf(fp,"nnNow show the used list.");printf("nnNow show the used list.");if(p)fprintf(fp,"nnumbertjobIDtsizetstartA

20、ddress");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",count,p->jobID,size,p->startAddress)

21、;fclose(fp);return 1;收集于網(wǎng)絡(luò),如有侵權(quán)請聯(lián)系管理員刪除精品文檔elsefprintf(fp,"nNo job in the memory ! You should input some jobsto it.");printf("nNo job in the memory ! You should input some jobs toit.");fclose(fp);return 0;int showJobList(struct jobList *jobs)/* 顯示作業(yè)鏈表 */struct jobList *p;p = jobs

22、->next;if(p = NULL)printf("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,

23、struct freeList *empty,struct usedList *used)int size,status;struct usedList *p;int address = memoryStartAddress;/* 全局變量,初始化時分配存儲空間始址*/if(*empty)->next = NULL)/*空閑分區(qū)鏈表為空,提示并返回*/收集于網(wǎng)絡(luò),如有侵權(quán)請聯(lián)系管理員刪除精品文檔printf("nThe memory was used out at all.nMay be you should finish some jobs first or press an

24、y key to try again !");getch();return;for(p = (*used) -> next;p;p = p-> next)/* 循環(huán)的修改占用分區(qū)的始址*/p -> startAddress = address;getJobInfo(jobs,p -> jobID,&size,&status);/* 由作業(yè) ID 獲得作業(yè)大小 */address += size;(*empty)->next->startAddress = address;/* 修改空閑分區(qū)的首節(jié)點始址、大小*/(*empty) -&

25、gt; next -> size = memorySize - (address - memoryStartAddress);(*empty) -> next -> next = NULL;/* 刪除首節(jié)點后的所有節(jié)點*/void order(struct freeList *empty,int bySize,int inc)struct freeList *p,*q,*temp;int startAddress,size;for(p = (*empty) -> next;p;p = p -> next)/* 按 bySize 和 inc 兩個參數(shù)尋找合適的節(jié)點,

26、用temp指向它 */for(temp = q = p;q;q = q -> next)switch(bySize)case 0 : switch(inc)case 0:if(q->size < temp->size)temp = q;break;收集于網(wǎng)絡(luò),如有侵權(quán)請聯(lián)系管理員刪除精品文檔default:if(q->size > temp->size)temp = q;break; break;default: switch(inc)case 0:if(q->startAddress < temp->startAddress)tem

27、p = q;break;default:if(q->startAddress > temp->startAddress)temp = q;break; break;/* 交換節(jié)點的成員值*/if(temp != p)startAddress = p->startAddress;size = p->size;p->startAddress = temp->startAddress;p->size = temp->size;temp->startAddress = startAddress;temp->size = size;int

28、 allocate(struct freeList *empty,int size)/* 為作業(yè)分配存儲空間、狀態(tài)必須為0*/struct freeList *p,*prep;int startAddress = -1;p = (*empty) -> next;while(p && p->size < size)p = p -> next;if(p != NULL)if(p -> size > size)startAddress = p -> startAddress;收集于網(wǎng)絡(luò),如有侵權(quán)請聯(lián)系管理員刪除精品文檔p -> star

29、tAddress += size;p -> size -= size;elsestartAddress = p -> startAddress;prep = *empty;while(prep -> next != p)prep = prep -> next;prep -> next = p -> next;free(p);else printf("nMay be you should move the fragment together ."); /* Unsuccessful ! */return startAddress;void

30、insertUsedNode(struct usedList *used,int id,int startAddress) /*插入釋放的空間到used鏈表中(作業(yè)號為id,startAddress由函數(shù) 13返回) */struct usedList *q,*r,*prer;if(q = malloc(sizeof(struct usedList) = NULL)printf("nNot enough to allocate for the used node ."); errorMessage();q -> startAddress = startAddress;

31、q -> jobID = id;prer = *used;r = (*used) -> next;while(r && r->startAddress < startAddress)prer = r;r = r -> next;q -> next = prer -> next;收集于網(wǎng)絡(luò),如有侵權(quán)請聯(lián)系管理員刪除精品文檔prer -> next = q;int finishJob(struct usedList *used,int id,int *startAddress)/* 結(jié)束一個作業(yè)號為id 的作業(yè),釋放存儲空間(由*st

32、artAddress返回空間的起始地址) */struct usedList *p,*prep;prep = *used;p = prep -> next;while(p && p -> jobID != id)prep = p;p = p -> next;if(p = NULL)printf("nThe job which id is : %d is not in the memory !",id); return 0;else*startAddress = p->startAddress;prep -> next = p -

33、> next;free(p);return 1;void insertFreeNode(struct freeList *empty,int startAddress,int size)/* 插入回收的空節(jié)點分區(qū),處理回收分區(qū)與空閑分區(qū)的四種鄰接關(guān)系。 */struct freeList *p,*q,*r;for(p = *empty;p -> next;p = p -> next) ; /* 處理鏈表尾部的鄰接情況 */if(p = *empty | p -> startAddress + p -> size < startAddress)收集于網(wǎng)絡(luò),如有

34、侵權(quán)請聯(lián)系管理員刪除精品文檔/* 與尾部不相鄰 */makeFreeNode(&r,startAddress,size);/* 通過 r 指針返回創(chuàng)建的空閑節(jié)點*/r -> next = p -> next;/* 插入獨立的空閑節(jié)點*/p -> next = r;return ;if(p -> startAddress + p -> size = startAddress) /* 與尾部上鄰 */p -> size += size;/* 合并尾部節(jié)點 */return ;q = (*empty) -> next;/* 處理鏈表首節(jié)點的鄰接情況*

35、/if(startAddress + size = q -> startAddress)/*與首節(jié)點下鄰 */q -> startAddress = startAddress;/*合并首節(jié)點 */q -> size += size;else if(startAddress + size < q -> startAddress)/* 與首節(jié)點不相鄰*/makeFreeNode(&r,startAddress,size);r -> next = (*empty) -> next;(*empty) -> next = r;else/* 處理鏈表

36、中間的鄰接情況*/while(q -> next && q->startAddress < startAddress)收集于網(wǎng)絡(luò),如有侵權(quán)請聯(lián)系管理員刪除精品文檔p = q;q = q -> next;if(p->startAddress+p->size = startAddress &&q->startAddress = startAddress+size) /*上下鄰,合并節(jié)點*/p->size+= size+q->size;p->next =q->next;free(q);/* 刪除多余節(jié)點

37、 */else if(p -> startAddress + p -> size = startAddress && q -> startAddress != startAddress + size)/* 上鄰,增加節(jié)點的大小 */p -> size += size;else if(p -> startAddress + p -> size != startAddress &&q -> startAddress = startAddress + size)/*下鄰 */q -> startAddress = sta

38、rtAddress; /*修改節(jié)點起始地址 */q -> size += size;/* 修改節(jié)點的大小*/else/* 上下不相鄰 */makeFreeNode(&r,startAddress,size);r -> next = p -> next;p -> next = r;收集于網(wǎng)絡(luò),如有侵權(quán)請聯(lián)系管理員刪除精品文檔void main(void)char fitMethod;FILE *fp;struct jobList *jobs;struct freeList *empty;struct usedList *used;if(used = malloc(

39、sizeof(struct usedList) = NULL)printf("nNot enough to allocate for the used node."); errorMessage();used -> next = NULL;remove("d:result.cl");makeFreeNode(&empty,0,0);while(1)char ch,step;int id,size,startAddress,status;struct jobList *q;printf("n1-Initializiation.n2-

40、Put job into memory(allocate memory).n3-Finish job(reuse memory).n4-Show current free list.n5-Show current memory used by jobs.n6-Move fragment together.n7-Exit.");printf("nPlease select a digit to continue.n");step = getche();printf("n");switch(step)case '1':openFil

41、e(&fp,"d:result.cl","a");fprintf(fp,"nntInitializiation:)");used -> next = NULL;empty->next = NULL;iniMemory();收集于網(wǎng)絡(luò),如有侵權(quán)請聯(lián)系管理員刪除精品文檔makeFreeNode(&(empty->next),memoryStartAddress,memorySize);fprintf(fp,"nnnDo you want to use your job filedirectly

42、?nDefault is 'N' . Y/N : "); printf("nnnDo you want to use your job filedirectly ?nDefault is 'N' . Y/N : n");ch = getche();fprintf(fp,"n%c",ch);fclose(fp);if(ch != 'Y'&& ch != 'y')inputJob();makeJobList(&jobs);if(ch = 'Y'|

43、 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)prin

44、tf("nnBad memory allocated !a");break;收集于網(wǎng)絡(luò),如有侵權(quán)請聯(lián)系管理員刪除精品文檔openFile(&fp,"d:result.cl","a");fprintf(fp,"nntPut job into memory(allocatememory) .");fprintf(fp,"nnnDo you want to allocate for jobfrom keyboard ?nDefault is 'N' . Y/N : "); pr

45、intf("nnnDo you want to allocate for job fromkeyboard ?nDefault is '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':

46、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(&

47、jobs,q->id,1);收集于網(wǎng)絡(luò),如有侵權(quán)請聯(lián)系管理員刪除精品文檔updateJobFile(jobs);elseshowJobList(jobs);openFile(&fp,"d:result.cl","a");fprintf(fp,"nPlease input a job id fromabove .");printf("nPlease input a job id fromabove .");scanf("%d",&id);fprintf(fp,"%d

48、n",id);getJobInfo(jobs,id,&size,&status);switch(status)case 0: printf("nOk !The job'sstatus is correct !");fprintf(fp,"nOk !The job'sstatus is correct !");fclose(fp);break;case 1: printf("nThe job was in thememory !");fprintf(fp,"nThe job was i

49、nthe memory !");fclose(fp);goto label;case 2: printf("nThe job wasfinished !");fprintf(fp,"nThe job wasfinished !");fclose(fp);goto label;default:printf("nUnexpected jobstatus .Please check you job file.");fprintf(fp,"nUnexpectedjob status .Please check you jo

50、b file.");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;收集于網(wǎng)絡(luò),如有侵權(quán)請聯(lián)系管理員刪除精品文檔startAddress = allocate(&empty,size);if(startAddress != -1)insertUsedNode(&used,id,startAddress);updateJobStatus(&jobs,id,1);updateJobFile(jobs);label : ;break;case '3':if(memoryStartAddress < 0 | memorySize < 1)printf("nnBad memory allocated !a");break;doint i;openFil

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論