



版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
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è)大?。ㄐ枰拇鎯?chǔ)空間大小)*/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)重錯(cuò)誤時(shí)顯示信息并結(jié)束程序*/printf("ntError !a");printf("nPress any key to e
3、xit !");收集于網(wǎng)絡(luò),如有侵權(quán)請(qǐng)聯(lián)系管理員刪除精品文檔getch();exit(1);void openFile(FILE *fp,char *filename,char *mode)/*以要求的方式打開(kāi)文件 */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é)點(diǎn),由empty 指針?lè)祷?*/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)/* 初始化存儲(chǔ)空間起始地
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)請(qǐng)聯(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)/* 從鍵盤(pán)輸入作業(yè)到D 盤(pán)的 JOB 文件 */int /*id,size, */status = 0,jobnum = 0;收集于網(wǎng)絡(luò),如有侵權(quán)請(qǐng)聯(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)請(qǐng)聯(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ū)隊(duì)列顯示 */FILE *fp;struct freeList *p = empty -> next;收集于網(wǎng)絡(luò),如有侵權(quán)請(qǐng)聯(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)請(qǐng)聯(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)請(qǐng)聯(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;/* 全局變量,初始化時(shí)分配存儲(chǔ)空間始址*/if(*empty)->next = NULL)/*空閑分區(qū)鏈表為空,提示并返回*/收集于網(wǎng)絡(luò),如有侵權(quán)請(qǐng)聯(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é)點(diǎn)始址、大小*/(*empty) -&
25、gt; next -> size = memorySize - (address - memoryStartAddress);(*empty) -> next -> next = NULL;/* 刪除首節(jié)點(diǎn)后的所有節(jié)點(diǎn)*/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 兩個(gè)參數(shù)尋找合適的節(jié)點(diǎn),
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)請(qǐng)聯(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é)點(diǎn)的成員值*/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è)分配存儲(chǔ)空間、狀態(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)請(qǐng)聯(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è)號(hào)為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)請(qǐng)聯(lián)系管理員刪除精品文檔prer -> next = q;int finishJob(struct usedList *used,int id,int *startAddress)/* 結(jié)束一個(gè)作業(yè)號(hào)為id 的作業(yè),釋放存儲(chǔ)空間(由*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é)點(diǎn)分區(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)請(qǐng)聯(lián)系管理員刪除精品文檔/* 與尾部不相鄰 */makeFreeNode(&r,startAddress,size);/* 通過(guò) r 指針?lè)祷貏?chuàng)建的空閑節(jié)點(diǎn)*/r -> next = p -> next;/* 插入獨(dú)立的空閑節(jié)點(diǎn)*/p -> next = r;return ;if(p -> startAddress + p -> size = startAddress) /* 與尾部上鄰 */p -> size += size;/* 合并尾部節(jié)點(diǎn) */return ;q = (*empty) -> next;/* 處理鏈表首節(jié)點(diǎn)的鄰接情況*
35、/if(startAddress + size = q -> startAddress)/*與首節(jié)點(diǎn)下鄰 */q -> startAddress = startAddress;/*合并首節(jié)點(diǎn) */q -> size += size;else if(startAddress + size < q -> startAddress)/* 與首節(jié)點(diǎn)不相鄰*/makeFreeNode(&r,startAddress,size);r -> next = (*empty) -> next;(*empty) -> next = r;else/* 處理鏈表
36、中間的鄰接情況*/while(q -> next && q->startAddress < startAddress)收集于網(wǎng)絡(luò),如有侵權(quán)請(qǐng)聯(lián)系管理員刪除精品文檔p = q;q = q -> next;if(p->startAddress+p->size = startAddress &&q->startAddress = startAddress+size) /*上下鄰,合并節(jié)點(diǎn)*/p->size+= size+q->size;p->next =q->next;free(q);/* 刪除多余節(jié)點(diǎn)
37、 */else if(p -> startAddress + p -> size = startAddress && q -> startAddress != startAddress + size)/* 上鄰,增加節(jié)點(diǎn)的大小 */p -> size += size;else if(p -> startAddress + p -> size != startAddress &&q -> startAddress = startAddress + size)/*下鄰 */q -> startAddress = sta
38、rtAddress; /*修改節(jié)點(diǎn)起始地址 */q -> size += size;/* 修改節(jié)點(diǎn)的大小*/else/* 上下不相鄰 */makeFreeNode(&r,startAddress,size);r -> next = p -> next;p -> next = r;收集于網(wǎng)絡(luò),如有侵權(quán)請(qǐng)聯(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)請(qǐng)聯(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)請(qǐng)聯(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)請(qǐng)聯(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)請(qǐng)聯(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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年度春季四川省國(guó)有資產(chǎn)投資管理有限責(zé)任公司招聘8人筆試參考題庫(kù)附帶答案詳解
- 黃岡師范學(xué)院《動(dòng)畫(huà)設(shè)計(jì)與制作》2023-2024學(xué)年第二學(xué)期期末試卷
- 邢臺(tái)應(yīng)用技術(shù)職業(yè)學(xué)院《計(jì)算機(jī)通信與網(wǎng)絡(luò)實(shí)驗(yàn)》2023-2024學(xué)年第二學(xué)期期末試卷
- 麗江文化旅游學(xué)院《冶金熱力學(xué)參數(shù)測(cè)定與分析》2023-2024學(xué)年第二學(xué)期期末試卷
- 河南師范大學(xué)《無(wú)機(jī)非金屬材料工藝學(xué)》2023-2024學(xué)年第二學(xué)期期末試卷
- 上海外國(guó)語(yǔ)大學(xué)賢達(dá)經(jīng)濟(jì)人文學(xué)院《網(wǎng)絡(luò)傳播與法規(guī)》2023-2024學(xué)年第二學(xué)期期末試卷
- 湖北工程學(xué)院《針織物設(shè)計(jì)與試織》2023-2024學(xué)年第二學(xué)期期末試卷
- 廣東南方職業(yè)學(xué)院《環(huán)境工程CAD實(shí)驗(yàn)》2023-2024學(xué)年第二學(xué)期期末試卷
- 北京北大方正軟件職業(yè)技術(shù)學(xué)院《建筑工程制圖與識(shí)圖》2023-2024學(xué)年第二學(xué)期期末試卷
- 重慶五一職業(yè)技術(shù)學(xué)院《圖案與字體設(shè)計(jì)》2023-2024學(xué)年第二學(xué)期期末試卷
- DBJ-T 13-195-2022 燒結(jié)煤矸石實(shí)心磚和多孔磚(砌塊) 應(yīng)用技術(shù)標(biāo)準(zhǔn)
- FZ/T 21009-2015短毛條
- 最新臭氧氧化技術(shù)專業(yè)知識(shí)講座課件
- 電力拖動(dòng)自動(dòng)控制系統(tǒng)-運(yùn)動(dòng)控制系統(tǒng)(第5版)習(xí)題答案
- 心血管疾病康復(fù)治療課件
- 海運(yùn)提單填制練習(xí)
- 幼兒園童話劇“拔蘿卜”劇本
- 各種面試方法詳解
- 常用H型鋼理論重量表格
- 中學(xué)自主招生考試物理試題
- 四川大學(xué)-劉龍飛-畢業(yè)答辯PPT模板
評(píng)論
0/150
提交評(píng)論