版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、#include<stdio.h>#include<time.h>#include<stdlib.h>int memoryStartAddress = -1; int memorySize = -1;struct jobList int id;int size; int status;/* 作業(yè)狀態(tài)/*/*作業(yè) ID */ 作業(yè)大小(需要的存儲(chǔ)空間大?。?/struct jobList *next;struct freeListint startAddress; /* int size;/*struct freeList *next;struct usedL
2、istint startAddress;int jobID;0 : new job ,1 : in the memory , 2 : finished . */ 作業(yè)鏈表指針 */*/*/*/*分區(qū)起始地址 */分區(qū)大小 */分區(qū)鏈表指針 */分區(qū)起始地址 */ 分區(qū)中存放作業(yè) ID */分區(qū)鏈表指針 */struct usedList *next;void errorMessage(void) /* printf("ntError !a"); printf("nPress any key to exit !"); getch(); exit(1);vo
3、id openFile(FILE *fp,char *filename,char *mode)/* 以要求的方式打開文件/*出現(xiàn)嚴(yán)重錯(cuò)誤時(shí)顯示信息并結(jié)束程序*/*/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)/* 根據(jù)參數(shù) startAddress 、 size 創(chuàng)建空閑節(jié)
4、點(diǎn),由 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) /* char MSA10,MS10; printf("nPl
5、ease 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)FILE *fp; char fitMethod;do printf("nnPlease meth
6、od !/*初始化存儲(chǔ)空間起始地址、選擇適應(yīng)算法input a char as fallow大小 */*/to select the fitnn1 (Best fit) 2 (Worst fit) nn3 (First fit) 4 (Last fit)n"); fitMethod = getche();while(fitMethod < '1' | fitMethod > '4');openFile(&fp,"d:result.cl","a"); switch(fitMethod)case
7、'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,"nnnn
8、tLast fit");fprintf(fp,"n*");break; fclose(fp); return fitMethod;從鍵盤輸入作業(yè)到 D盤的JOB文件*/void inputJob(void) /*int /*id,size, */status = 0,jobnum = 0;FILE *fp; char id10,size10; openFile(&fp,"d:job.cl","w"); fprintf(fp,"job_IDtsizetstatus"); printf("n
9、nnnPlease input the jobs as fallow !nEnter a integer smaller than 1 to quit .njob_IDtsizen"); doscanf("%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);/* fprintf(fp,"n%dt
10、%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 文件中讀出作業(yè)并創(chuàng)建作業(yè)鏈表 */char jobID10,size10,status10; struct jobL
11、ist *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(
12、!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; rear -> next = p;rear = rear -> next; rear -> id =
13、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:job.cl","w"); fprintf(fp,"job_IDtsizetstatus"); for(p = jobs -> next;p;p =
14、 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 th
15、e 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",+count,p startAddress);更新作業(yè)鏈表中作業(yè)的狀態(tài)空閑分區(qū)隊(duì)列顯示 */->size,p->printf(&
16、quot;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 size = p -> size; *status = p -> status;void updateJobS
17、tatus(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(); elsejobs,int id,int *size,int *status)/* 獲取作業(yè)的信息 */struct jobList *p = jo
18、bs->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/* 作業(yè)占用鏈表顯示 */FILE *fp;struct usedList *p = used -> next;int count = 0,size,status; openFile(&fp,"d:result.cl","a&quo
19、t;); 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); fpr
20、intf(fp,"n%dt%dt%dt%d",+count,p->jobID,size,p-> startAddress);printf("n%dt%dt%dt%d",count,p->jobID,size,p-> startAddress); fclose(fp); return 1; elsefprintf(fp,"nNo job in the memory ! You should input some jobs to it.");printf("nNo job in the memory !
21、You should input some jobs to it.");fclose(fp);return 0;/*顯示作業(yè)鏈表 */int showJobList(struct jobList *jobs) struct jobList *p; p = jobs->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")
22、; 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 address = memoryStartAddress;/* 全局變量,初始化時(shí)分配存儲(chǔ)空間始址 */if(*empty)->next = NULL
23、) /* 空閑分區(qū)鏈表為空,提示并返回 */ printf("nThememory was used out at all.nMay be you should finish somejobs first or press any 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,&a
24、mp;status);/*由作業(yè) ID 獲得作業(yè)大小 */ address += size; (*empty)->next->startAddress = address;/* 修改空閑分區(qū)的首節(jié)點(diǎn)始址、大小 */(*empty) -> next -> size = memorySize - (address - memoryStartAddress); (*empty) -> next -> next = NULL;/* 刪除首節(jié)點(diǎn)后的所有節(jié)點(diǎn) */ void order(struct freeList *empty,int bySize,int inc)
25、struct freeList *p,*q,*temp;int startAddress,size;for(p = (*empty) -> next;p;p = p -> next) /* 按 bySize 和 inc 兩個(gè)參數(shù)尋找合適的節(jié)點(diǎn),用 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;default:if(q->size > temp-&
26、gt;size)temp = q;break; break;default: switch(inc)case 0:if(q->startAddress < temp->startAddress) temp = q;break;default:if(q->startAddresstemp->startAddress)temp = q;break;break;/*交換節(jié)點(diǎn)的成員值 */if(temp != p)startAddress = p->startAddress;size = p->size;p->startAddress = temp-&g
27、t;startAddress; p->size = temp->size; temp->startAddress = startAddress; temp->size = size;int 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 -> nex
28、t;if(p != NULL)if(p -> size > size) startAddress = p -> startAddress; p -> startAddress += size;p -> size -= size; elsestartAddress = p -> startAddress; prep = *empty;while(prep -> next != p) elseprep = prep -> next; prep -> next = p -> next; free(p);printf("nMay b
29、e you should move the fragment together ."); /* Unsuccessful ! */ return startAddress;void 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("
30、;nNot enough to allocate for the used node ."); errorMessage();q -> startAddress = startAddress; q -> jobID = id;prer = *used;r = (*used) -> next;while(r && r->startAddress < startAddress)prer = r; r = r -> next; q -> next = prer -> next;prer -> next = q;int f
31、inishJob(struct usedList for(p = *empty;p -> next;p = p -> next) ;/* 處理鏈表尾部的鄰接情況 */if(p = *empty | p -> startAddress + p -> size < startAddress)used,int id,int startAddress = p->startAddress; prep -> next = p -> next; free(p);return 1;void/* 插入回收的空節(jié)點(diǎn)分區(qū),處理回收分區(qū)與空閑分區(qū)的四種鄰接關(guān)系。 str
32、uct freeList *p,*q,*r;startAddress)/* 結(jié)束一個(gè)作業(yè)號(hào)為 id 的作業(yè), 釋放存儲(chǔ)空間 (由 *startAddress 返回空間的 起始地址) */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)
33、; return 0;insertFreeNode(struct freeList *empty,int startAddress,intsize)*/ else/*與尾部不相鄰 */*makeFreeNode(&r,startAddress,size);通過 r 指針返回創(chuàng)建的空閑節(jié)點(diǎn) */r -> next = p -> next;/*p -> next = r;return ;插入獨(dú)立的空閑節(jié)點(diǎn) */if(p -> startAddress + p -> size = startAddress) /* 與尾部上鄰 */p -> size +=
34、size; return ;/*合并尾部節(jié)點(diǎn) */q = (*empty) -> next;if(startAddress + size = q -> startAddress) /*/*處理鏈表首節(jié)點(diǎn)的鄰接情況與首節(jié)點(diǎn)下鄰*/*/q -> startAddress = startAddress; q -> size += size;/*合并首節(jié)點(diǎn) */else if(startAddress + size < q -> startAddress) /* 與首節(jié)點(diǎn)不相鄰 */makeFreeNode(&r,startAddress,size); r
35、-> next = (*empty) -> next;(*empty) -> next = r; else處理鏈表中間的鄰接情況 */ while(q -> next && q->startAddress < startAddress)/*p = q;q = q -> next;if(p->startAddress+p->size = startAddress && q->startAddress = startAddress+size) /*上下鄰,合并節(jié)點(diǎn)*/p->size+= size+q-
36、>size; p->next =q->next; free(q);/*刪除多余節(jié)點(diǎn) */* 上鄰,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 = sta
37、rtAddress + size)/*下鄰 */q -> startAddress = startAddress; /* q -> size += size; /*修改節(jié)點(diǎn)起始地址 */修改節(jié)點(diǎn)的大小 */ else/* makeFreeNode(&r,startAddress,size); r -> next = p -> next;p -> next = r; void main(void)char fitMethod;FILE *fp; struct jobList *jobs; struct freeList *empty; struct used
38、List *used; if(used = malloc(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 jobL
39、ist *q;printf("n1 n2- n3-n4n5n6Initializiation.Put job into memory(allocate memory). Finish job(reuse memory). Show current free list. Show current memory used by jobs. Move fragment together.n7Exit.");printf("nPlease select a digit to continue.n"); step = getche();printf("n
40、"); switch(step) case '1':openFile(&fp,"d:result.cl","a"); fprintf(fp,"nntInitializiation :)"); used -> next = NULL; empty->next = NULL; iniMemory();makeFreeNode(&(empty->next),memoryStartAddress,memorySize);fprintf(fp,"nnnDo you want
41、to use your job filedirectly ?nDefault is 'N' . Y/N : ");printf("nnnDo you want to use your job file directly ? nDefault is 'N' . Y/N : n");ch = getche();fprintf(fp,"n%c",ch);fclose(fp);if(ch != 'Y'&& ch != 'y')inputJob();makeJobList(&
42、amp;jobs);if(ch = 'Y'| 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
43、 < 0 | memorySize < 1) printf("nnBad memory allocated !a"); break;openFile(&fp,"d:result.cl","a");memory(allocatefprintf(fp,"nntPut job intomemory) .");fprintf(fp,"nnnDo you want to allocate for job fromkeyboard ?nDefault is 'N' . Y/N :
44、");printf("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)c
45、ase '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;startAddress = allocate(&empty,q->size); if(startAddress != -1)insertUsedNode(&used,q->id,startAddre
46、ss);updateJobStatus(&jobs,q->id,1);updateJobFile(jobs); elseshowJobList(jobs); openFile(&fp,"d:result.cl","a"); fprintf(fp,"nPlease input a job id from above ."); printf("nPlease input a job id from above ."); scanf("%d",&id);fprintf(f
47、p,"%dn",id); getJobInfo(jobs,id,&size,&status);switch(status)case 0: printf("nOk!The job's statusiscorrect !");fprintf(fp,"nOk!The job's statusiscorrect !");fclose(fp);break;case 1: printf("nThe job was in the memory !"); fprintf(fp,"nThe
48、job was memory !");fclose(fp);goto label;case 2: printf("nThe job was finished !"); fprintf(fp,"nThe job finished !");fclose(fp);goto label;default:printf("nUnexpected job status .Pleaseinthewascheck you job file.");fprintf(fp,"nUnexpected job status .Pleasech
49、eck you job 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; 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;openFile(&fp,"d
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 江蘇省淮安市2024-2025學(xué)年高一第一學(xué)期期末調(diào)研測(cè)試數(shù)學(xué)試題(含答案)
- 2025年度幕墻工程勞務(wù)分包合同風(fēng)險(xiǎn)評(píng)估與防控措施3篇
- 2024版:專業(yè)技術(shù)人員聘用協(xié)議3篇
- 2024食用油產(chǎn)品退貨及售后服務(wù)合同3篇
- 2025年度個(gè)人二手房交易稅費(fèi)減免專項(xiàng)合同3篇
- 2024版造林綠化承包合同
- 福建省南平市舊縣中學(xué)高二數(shù)學(xué)理聯(lián)考試卷含解析
- 福建省南平市建州高級(jí)中學(xué)2021年高一地理下學(xué)期期末試卷含解析
- 2024行政事業(yè)單位內(nèi)部控制規(guī)范與內(nèi)部控制風(fēng)險(xiǎn)防范合同3篇
- 2024蔬菜買賣合同
- 基于Internet的銀行競(jìng)爭(zhēng)情報(bào)收集系統(tǒng)的研究與實(shí)現(xiàn)的中期報(bào)告
- 醫(yī)院對(duì)賬平臺(tái)技術(shù)方案
- 住院醫(yī)師規(guī)范化培訓(xùn)年度眼科學(xué)習(xí)總結(jié)
- 醫(yī)療事故處理?xiàng)l例【精美醫(yī)學(xué)課件】
- 2024年首都機(jī)場(chǎng)集團(tuán)公司招聘筆試參考題庫含答案解析
- 自動(dòng)化電氣控制方案
- 加油站涉恐風(fēng)險(xiǎn)評(píng)估報(bào)告
- 2 汽車維修檔案管理制度范文精簡(jiǎn)處理
- 工貿(mào)企業(yè)重大事故隱患判定標(biāo)準(zhǔn)培訓(xùn)PPT
- 2023年外交學(xué)院招考聘用筆試題庫含答案解析
- 農(nóng)學(xué)技能高考【種植類】復(fù)習(xí)題庫大全-2、《植物生產(chǎn)與環(huán)境》-上(單選多選題)
評(píng)論
0/150
提交評(píng)論