西安郵電大學(xué)操作系統(tǒng)內(nèi)存管理實(shí)驗(yàn)報(bào)告含源碼16500字_第1頁
西安郵電大學(xué)操作系統(tǒng)內(nèi)存管理實(shí)驗(yàn)報(bào)告含源碼16500字_第2頁
西安郵電大學(xué)操作系統(tǒng)內(nèi)存管理實(shí)驗(yàn)報(bào)告含源碼16500字_第3頁
西安郵電大學(xué)操作系統(tǒng)內(nèi)存管理實(shí)驗(yàn)報(bào)告含源碼16500字_第4頁
西安郵電大學(xué)操作系統(tǒng)內(nèi)存管理實(shí)驗(yàn)報(bào)告含源碼16500字_第5頁
已閱讀5頁,還剩34頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

西安郵電大學(xué)操作系統(tǒng)內(nèi)存管理實(shí)驗(yàn)報(bào)告含源碼16500字

西安郵電大學(xué)(計(jì)算機(jī)學(xué)院)課內(nèi)實(shí)驗(yàn)報(bào)告實(shí)驗(yàn)名稱:內(nèi)存管理專業(yè)名稱:班級(jí):1201班學(xué)生姓名:學(xué)號(hào)(8指導(dǎo)教師:實(shí)驗(yàn)日期:20xx年11月25日一.實(shí)驗(yàn)?zāi)康募皩?shí)驗(yàn)環(huán)境(一)、實(shí)驗(yàn)環(huán)境1.硬件(1)主機(jī):PentiumIII以上;(2)內(nèi)存:128MB以上;(3)顯示器:VGA或更高;(4)硬盤空間:至少100MB以上剩余空間。2.軟件Ubuntu下gcc編譯器、gdb調(diào)試工具。(二)、實(shí)驗(yàn)?zāi)康?1)、掌握內(nèi)存分配FF,BF,WF策略及實(shí)現(xiàn)的思路;(2)、掌握內(nèi)存回收過程及實(shí)現(xiàn)思路;(3)、參考本程序思路,實(shí)現(xiàn)內(nèi)存的申請(qǐng)、釋放的管理程序,調(diào)試運(yùn)行,總結(jié)程序設(shè)計(jì)中出現(xiàn)的問題并找出原因。二、實(shí)驗(yàn)內(nèi)容(1)補(bǔ)充完整FF,BF,WF等算法的代碼;(2)掌握內(nèi)存回收過程及實(shí)現(xiàn)思路;(3)實(shí)現(xiàn)內(nèi)存的申請(qǐng)和釋放。三.方案設(shè)計(jì)(一)、實(shí)現(xiàn)功能1-Setmemorysize(default=1024)2-Selectmemoryallocationalgorithm3-Newprocess4-Terminateaprocess5-Displaymemoryusage0-Exit(二)、關(guān)鍵算法思想設(shè)計(jì)與分析首次適應(yīng)算法(FirstFit):從空閑分區(qū)表的第一個(gè)表目起查找該表,把最先能夠滿足要求的空閑區(qū)分配給作業(yè),這種方法目的在于減少查找時(shí)間。為適應(yīng)這種算法,空閑分區(qū)表(空閑區(qū)鏈)中的空閑分區(qū)要按地址由低到高進(jìn)行排序。該算法優(yōu)先使用低址部分空閑區(qū),在低址空間造成許多小的空閑區(qū),在高地址空間保留大的空閑區(qū)。最佳適應(yīng)算法(BestFit):它從全部空閑區(qū)中找出能滿足作業(yè)要求的、且大小最小的空閑分區(qū),這種方法能使碎片盡量小。為適應(yīng)此算法,空閑分區(qū)表(空閑區(qū)鏈)中的空閑分區(qū)要按從小到大進(jìn)行排序,自表頭開始查找到第一個(gè)滿足要求的自由分區(qū)分配。該算法保留大的空閑區(qū),但造成許多小的空閑區(qū)。最差適應(yīng)算法(WorstFit):它從全部空閑區(qū)中找出能滿足作業(yè)要求的、且大小最大的空閑分區(qū),從而使鏈表中的結(jié)點(diǎn)大小趨于均勻,適用于請(qǐng)求分配的內(nèi)存大小范圍較窄的系統(tǒng)。為適應(yīng)此算法,空閑分區(qū)表(空閑區(qū)鏈)中的空閑分區(qū)要按大小從大到小進(jìn)行排序,自表頭開始查找到第一個(gè)滿足要求的自由分區(qū)分配。該算法保留小的空閑區(qū),盡量減少小的碎片產(chǎn)生。四.測(cè)試數(shù)據(jù)及運(yùn)行結(jié)果設(shè)置內(nèi)存大小選擇算法創(chuàng)建進(jìn)程選擇殺死進(jìn)程查看內(nèi)存以及進(jìn)程五.總結(jié)這次實(shí)驗(yàn)剛開始的時(shí)候不知道整個(gè)實(shí)驗(yàn)的思路,還好老師在課堂上大概講解了一下,并且給出了大部分代碼,剩下的工作就是填寫部分代碼,這樣實(shí)驗(yàn)就簡單多了。通過本次的內(nèi)存實(shí)驗(yàn)我了解到了內(nèi)存的管理模型的知識(shí),在內(nèi)存緊縮合并回收部分還遇到了一些問題,最終通過查資料解決了問題,雖然對(duì)內(nèi)存的管理掌握得不是很熟練,但這激勵(lì)了我下來后看書,努力學(xué)習(xí)不懂的知識(shí),通過讓我對(duì)其有了更加深入的了解,讓我認(rèn)識(shí)到了,操作系統(tǒng)是一項(xiàng)真正實(shí)用,而且很有意義的學(xué)科,增加了我對(duì)操作系統(tǒng)的興趣,也為以后的學(xué)習(xí)打下理論基礎(chǔ)。六、源代碼#include<stdio.h>#include<malloc.h>#include<unistd.h>#include<stdlib.h>#definePROCESS_NAME_LEN32//進(jìn)程名長度#defineMIN_SLICE10//最小碎片的大小#defineDEFAULT_MEM_SIZE1024//內(nèi)存大小#defineDEFAULT_MEM_START0//起始位置/*內(nèi)存分配算法*/#defineMA_FF1#defineMA_BF2#defineMA_WF3/*描述每一個(gè)空閑塊的數(shù)據(jù)結(jié)構(gòu)*/structfree_block_type{intsize;//空閑塊大小intstart_addr;//空閑塊起始地址structfree_block_type*next;//指向下一個(gè)空閑塊};/*指向內(nèi)存中空閑塊鏈表的首指針*/structfree_block_type*free_block=NULL;/*每個(gè)進(jìn)程分配到的內(nèi)存塊的描述*/structallocated_block{intpid;//進(jìn)程標(biāo)識(shí)符intsize;//進(jìn)程大小intstart_addr;//進(jìn)程分配到的內(nèi)存塊的起始地址charprocess_name[PROCESS_NAME_LEN];//進(jìn)程名structallocated_block*next;//指向下一個(gè)進(jìn)程控制塊};/*進(jìn)程分配內(nèi)存塊鏈表的首指針*/structallocated_block*allocated_block_head=NULL;intfree_block_count=0;//空閑塊個(gè)數(shù)intmem_size=DEFAULT_MEM_SIZE;//內(nèi)存大小intcurrent_free_mem_size=0;//當(dāng)前空閑內(nèi)存大小intma_algorithm=MA_FF;//當(dāng)前分配算法staticintpid=0;//初始PIDintflag=0;//設(shè)置內(nèi)存大小標(biāo)志,表示內(nèi)存大小是否設(shè)置/*函數(shù)聲明*/structfree_block_type*init_free_block(intmem_size);voiddisplay_menu();intset_mem_size();voidset_algorithm();voidrearrange(intalgorithm);intrearrange_WF();intrearrange_BF();intrearrange_FF();intnew_process();intallocate_mem(structallocated_block*ab);voidkill_process();intfree_mem(structallocated_block*ab);intdispose(structallocated_block*free_ab);intdisplay_mem_usage();structallocated_block*find_process(intpid);intdo_exit();intallocate_FF(structallocated_block*ab);intallocate_BF(structallocated_block*ab);intallocate_WF(structallocated_block*ab);intallocate(structfree_block_type*pre,structfree_block_type*allocate_free_nlock,structallocated_block*ab);intmem_retrench(structallocated_block*ab);//通過內(nèi)存緊縮技術(shù)給新進(jìn)程分配內(nèi)存空間intmem_retrench(structallocated_block*ab){structallocated_block*allocated_work,*allocated_pre=allocated_block_head;structfree_block_type*free_work,*free_pre=free_block->next;if(allocated_pre==NULL)return-1;allocated_pre->start_addr=0;allocated_work=allocated_pre->next;while(allocated_work!=NULL){allocated_work->start_addr=allocated_pre->start_addr+allocated_pre->size;allocated_pre=allocated_work;allocated_work=allocated_work->next;}free_block->start_addr=allocated_pre->start_addr+allocated_pre->size;free_block->size=current_free_mem_size;free_block->next=NULL;free_work=free_pre;while(free_pre!=NULL){free(free_pre);free_pre=free_work;if(free_pre!=NULL)free_work=free_work->next;}allocate(NULL,free_block,ab);return1;}//給新進(jìn)程分配內(nèi)存空間intallocate(structfree_block_type*pre,structfree_block_type*allocate_free_block,structallocated_block*ab){structallocated_block*p=allocated_block_head;ab->start_addr=allocate_free_block->start_addr;if(allocate_free_block->size-ab->size<MIN_SLICE){ab->size=allocate_free_block->size;if(pre!=NULL){pre->next=allocate_free_block;}else{free_block=allocate_free_block->next;}free(allocate_free_block);}else{allocate_free_block->start_addr+=ab->size;allocate_free_block->size-=ab->size;}if(p==NULL){allocated_block_head=ab;}else{while(p->next!=NULL)p=p->next;p->next=ab;}current_free_mem_size-=ab->size;if(current_free_mem_size==0)free_block=NULL;return0;}//按照最壞適應(yīng)算法給新進(jìn)程分配內(nèi)存空間intallocate_WF(structallocated_block*ab){intret;structfree_block_type*wf=free_block;if(wf==NULL)return-1;if(wf->size>=ab->size)allocate(NULL,wf,ab);elseif(current_free_mem_size>=ab->size)ret=mem_retrench(ab);elseret=-2;rearrange_WF();returnret;}//按照最佳適應(yīng)算法給新進(jìn)程分配內(nèi)存空間intallocate_BF(structallocated_block*ab){intret;structfree_block_type*pre=NULL,*bf=free_block;if(bf==NULL)return-1;while(bf!=NULL){if(bf->size>=ab->size){ret=allocate(pre,bf,ab);break;}pre=bf;pre=pre->next;}if(bf==NULL&¤t_free_mem_size>ab->size)ret=mem_retrench(ab);elseret=-2;rearrange_BF();returnret;}//按照首次適應(yīng)算法給新進(jìn)程分配內(nèi)存空間intallocate_FF(structallocated_block*ab){intret;structfree_block_type*pre=NULL,*ff=free_block;if(ff==NULL)return-1;while(ff!=NULL){if(ff->size>=ab->size){ret=allocate(pre,ff,ab);break;}pre=ff;pre=pre->next;}if(ff==NULL&¤t_free_mem_size>ab->size)ret=mem_retrench(ab);elseret=-2;rearrange_FF();returnret;}//分配內(nèi)存模塊intallocate_mem(structallocated_block*ab){intret;structfree_block_type*fbt,*pre;intrequest_size=ab->size;fbt=pre=free_block;switch(ma_algorithm){caseMA_FF:ret=allocate_FF(ab);break;caseMA_BF:ret=allocate_BF(ab);break;caseMA_WF:ret=allocate_WF(ab);break;default:break;}returnret;}//創(chuàng)建一個(gè)新的進(jìn)程。intnew_process(){structallocated_block*ab;intsize;intret;ab=(structallocated_block*)malloc(sizeof(structallocated_block));if(!ab)exit(-5);ab->next=NULL;pid++;sprintf(ab->process_name,"PROCESS-%02d",pid);//sprintf()函數(shù)將格式化的數(shù)據(jù)寫入某字符串中ab->pid=pid;printf("Memoryfor%s:",ab->process_name);for(;;){scanf("%d",&size);getchar();if(size>0){ab->size=size;break;}elseprintf("Thesizehavetogreaterthanzero!Pleaseinputagain!");}ret=allocate_mem(ab);//從空閑區(qū)分配內(nèi)存,ret==1表示分配okif((ret==1)&&(allocated_block_head==NULL))//如果此時(shí)allocated_block_head尚未賦值,則賦值{//進(jìn)程分配鏈表為空allocated_block_head=ab;return1;}elseif(ret==1)//分配成功,將該已分配塊的描述插入已分配鏈表{ab->next=allocated_block_head;//頭插法allocated_block_head=ab;return2;}elseif(ret==-1)//分配不成功{printf("Allocationfail\n");free(ab);return-1;}return3;}//退出程序并釋放內(nèi)存空間。intdo_exit(){structallocated_block*allocated_ab,*allocated_pre;structfree_block_type*free_ab,*free_pre;free_pre=free_block;allocated_pre=allocated_block_head;if(free_pre!=NULL){free_ab=free_pre->next;while(free_ab!=NULL){free(free_pre);free_pre=free_ab;free_ab=free_ab->next;}}if(allocated_pre!=NULL){allocated_ab=allocated_pre->next;while(allocated_ab!=NULL){free(allocated_pre);allocated_pre=allocated_ab;allocated_ab=allocated_ab->next;}}allocated_ab=allocated_ab->next;return0;}//在進(jìn)程分配鏈表中尋找指定進(jìn)程。structallocated_block*find_process(intpid){structallocated_block*ab=allocated_block_head;if(ab==NULL){printf("Here??????111111111\n");returnNULL;}while(ab->pid!=pid&&ab->next!=NULL)ab=ab->next;if(ab->next==NULL&&ab->pid!=pid){printf("Here??????2222222\n");returnNULL;}returnab;}//顯示當(dāng)前內(nèi)存的使用情況,包括空閑區(qū)的情況和已經(jīng)分配的情況。intdisplay_mem_usage(){structfree_block_type*fbt=free_block;structallocated_block*ab=allocated_block_head;printf("----------------------------------------------------------\n");//顯示空閑區(qū)printf("FreeMemory:\n");printf("%20s%20s\n","start_addr","size");while(fbt!=NULL){printf("%20d%20d\n",fbt->start_addr,fbt->size);fbt=fbt->next;}//顯示已分配區(qū)printf("\nUsedMemory:\n");printf("%10s%20s%10s%10s\n","PID","ProcessName","start_addr","size");while(ab!=NULL){printf("%10d%20s%10d%10d\n",ab->pid,ab->process_name,ab->start_addr,ab->size);ab=ab->next;}printf("----------------------------------------------------------\n");return1;}//釋放ab數(shù)據(jù)結(jié)構(gòu)節(jié)點(diǎn)。intdispose(structallocated_block*free_ab){structallocated_block*pre,*ab;if(free_block==NULL)return-1;if(free_ab==allocated_block_head)//如果要釋放第一個(gè)節(jié)點(diǎn){allocated_block_head=allocated_block_head->next;free(free_ab);}else{pre=allocated_block_head;ab=allocated_block_head->next;//找到free_abwhile(ab!=free_ab){pre=ab;ab=ab->next;}pre->next=ab->next;free(ab);}return1;}/*將ab所表示的已分配區(qū)歸還,并進(jìn)行可能的合并*/intfree_mem(structallocated_block*ab){intalgorithm=ma_algorithm;structfree_block_type*fbt,*pre,*work;fbt=(structfree_block_type*)malloc(sizeof(structfree_block_type));if(!fbt)return-1;pre=free_block;fbt->start_addr=ab->start_addr;fbt->size=ab->size;fbt->next=NULL;if(pre!=NULL){while(pre->next!=NULL)pre=pre->next;pre->next=fbt;}else{free_block=fbt;}rearrange_FF();pre=free_block;work=pre->next;while(work!=NULL){if(pre->start_addr+pre->size==work->start_addr){pre->size+=work->size;free(work);work=pre->next;}else{pre=work;work=work->next;}}current_free_mem_size+=ab->size;return1;}//刪除進(jìn)程,歸還分配的存儲(chǔ)空間,并刪除描述該進(jìn)程內(nèi)存分配的節(jié)點(diǎn)。voidkill_process(){structallocated_block*ab;intpid;printf("KillProcess,pid=");scanf("%d",&pid);getchar();ab=find_process(pid);if(ab!=NULL){free_mem(ab);/*釋放ab所表示的分配區(qū)*/dispose(ab);/*釋放ab數(shù)據(jù)結(jié)構(gòu)節(jié)點(diǎn)*/}}//按FF算法重新整理內(nèi)存空閑塊鏈表,按空閑塊首地址排序。intrearrange_FF(){structfree_block_type*head=free_block;structfree_block_type*forehand,*pre,*rear;inti;if(head==NULL)return-1;//冒泡排序for(i=0;i<free_block_count-1;i++){forehand=head;pre=forehand->next;rear=pre->next;while(pre->next!=NULL){if(forehand==head&&forehand->start_addr>=pre->start_addr)//比較空閑鏈表中第一個(gè)空閑塊與第二個(gè)空閑塊的開始地址的大小{head->next=pre->next;pre->next=head;head=pre;forehand=head->next;pre=forehand->next;rear=pre->next;}elseif(pre->start_addr>=rear->start_addr)//比較鏈表中其他相鄰兩節(jié)點(diǎn)的開始地址的大小{pre->next=rear->next;forehand->next=rear;rear->next=pre;forehand=rear;rear=pre->next;}else{forehand=pre;pre=rear;rear=rear->next;}}}return0;}//按BF算法重新整理內(nèi)存空閑塊鏈表,按空閑塊大小從小到大排序。intrearrange_BF(){structfree_block_type*head=free_block;structfree_block_type*forehand,*pre,*rear;inti;if(head==NULL)return-1;//冒泡排序for(i=0;i<free_block_count-1;i++){forehand=head;pre=forehand->next;rear=pre->next;while(pre->next!=NULL){if(forehand==head&&forehand->size<=pre->size)//比較空閑鏈表中第一個(gè)空閑塊與第二個(gè)空閑塊的空間的大小{head->next=pre->next;pre->next=head;head=pre;forehand=head->next;pre=forehand->next;rear=pre->next;}elseif(pre->size<=rear->size)//比較鏈表中其他相鄰兩節(jié)點(diǎn)的空間的大小{pre->next=rear->next;forehand->next=rear;rear->next=pre;forehand=rear;rear=pre->next;}else{forehand=pre;pre=rear;rear=rear->next;}}}return0;}//按WF算法重新整理內(nèi)存空閑塊鏈表,按空閑塊大小從大到小排序。intrearrange_WF(){structfree_block_type*head=free_block;structfree_block_type*forehand,*pre,*rear;inti;if(head==NULL)return-1;//冒泡排序for(i=0;i<free_block_count-1;i++){forehand=head;pre=forehand->next;rear=pre->next;while(pre->next!=NULL){if(forehand==head&&forehand->size>=pre->size)//比較空閑鏈表中第一個(gè)空閑塊與第二個(gè)空閑塊空間的大小{head->next=pre->next;pre->next=head;head=pre;forehand=head->next;pre=forehand->next;rear=pre->next;}elseif(pre->size>=rear->size)//比較鏈表中其他相鄰兩節(jié)點(diǎn)的空間的大小{pre->next=rear->next;forehand->next=rear;rear->next=pre;forehand=rear;rear=pre->next;}else{forehand=pre;pre=rear;rear=rear->next;}}}return0;}//按指定的算法整理內(nèi)存空閑塊鏈表。voidrearrange(intalgorithm){switch(algorithm){caseMA_FF:rearrange_FF();break;caseMA_BF:rearrange_BF();break;caseMA_WF:rearrange_WF();break;}}//設(shè)置當(dāng)前的分配算法voidset_algorithm(){intalgorithm;//system("clear");printf("\t1-FirstFit\n");//首次適應(yīng)算法printf("\t2-BestFit\n");//最佳適應(yīng)算法printf("\t3-WorstFit\n");//最壞適應(yīng)算法printf("\nPleasechoose(1~3):");for(;;){scanf("%d",&algorithm);getchar();if(algorithm>=1&&algorithm<=3){ma_algorithm=algorithm;break;}else{printf("\nCannotinput%d,Pleaseinput1~3:",algorithm);}}//按指定算法重新排列空閑區(qū)鏈表rearrange(ma_algorithm);}//設(shè)置內(nèi)存的大小intset_mem_size(){intsize;if(flag!=0)//防止重復(fù)設(shè)置{printf("Cannotsetmemorysizeagain\n");return0;}printf("Totalmemorysize=");for(;;){scanf("%d",&size);getchar();if(size>0){current_fre

溫馨提示

  • 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ì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論