段頁式虛擬存儲(chǔ)管理_第1頁
段頁式虛擬存儲(chǔ)管理_第2頁
段頁式虛擬存儲(chǔ)管理_第3頁
段頁式虛擬存儲(chǔ)管理_第4頁
段頁式虛擬存儲(chǔ)管理_第5頁
已閱讀5頁,還剩8頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、word#include <stdio.h>#include <time.h>#include <stdlib.h>#define Processcount 10/假設(shè)進(jìn)程數(shù)為10#define AssumeBlock100/假設(shè)內(nèi)存塊大小有100塊#define Segnumber 4/假設(shè)每個(gè)進(jìn)程最大段數(shù)是4#define Keepmemory 8/駐留集大小#define SegLength 16/段長最長16/*頁表相關(guān)信息*/ typedef struct int pageNumber; int frameNumber; int pageFlag;

2、 int accesstime; int runcount; Page;/段表相關(guān)信息*typedef struct int segmentNumber; int segmentlength; /段長即每一個(gè)段的頁數(shù) int startAddress; Page pagenumSegLength; int segFlag; int pagereal; Segment;/*進(jìn)程相關(guān)信息*/ typedef struct Segment segmentSegnumber; int isInmemory; int segtotalnumber; int inpage;/載入內(nèi)存中的頁數(shù) int se

3、greal; Process;int blocksize;/塊大小int blocknumbers;/塊數(shù)量int processnumber;/進(jìn)程數(shù)量int AvailableBlock;/內(nèi)存中可用塊數(shù)量int offset;/頁內(nèi)偏移量Process processProcesscount;/進(jìn)相關(guān)信息程int memoryAssumeBlock;/內(nèi)存塊相關(guān)信息int occupied;/已被占用的內(nèi)存塊數(shù)量void Applyspace() printf("*內(nèi)存空間申請(qǐng)*n");printf("輸入進(jìn)程數(shù)目:");scanf("%

4、d",&processnumber);while(processnumber<=0|processnumber>Processcount)printf("警告:進(jìn)程數(shù)目不能大于10且不能小于0,請(qǐng)重新輸入:");scanf("%d",&processnumber);for(int i=0;i<processnumber;i+)printf("n");printf("%d號(hào)進(jìn)程有幾個(gè)段:",i);scanf("%d",&processi.segt

5、otalnumber);while(processi.segtotalnumber<=0|processi.segtotalnumber>4)printf("警告:進(jìn)程每個(gè)段的數(shù)目不能大于4且不能小于0,請(qǐng)重新輸入:");scanf("%d",&processi.segtotalnumber);for(int j=0;j<processi.segtotalnumber;j+)printf("%d號(hào)段需要的段長是多少:",j);scanf("%d",&processi.segment

6、j.segmentlength);while(processi.segmentj.segmentlength<0|processi.segmentj.segmentlength>16)printf("警告:進(jìn)程每個(gè)段的段長不能大于16且不能小于0,請(qǐng)重新輸入:");scanf("%d",&processi.segmentj.segmentlength);for(int k=0;k<processi.segmentj.segmentlength;k+)processi.segmentj.pagenumk.accesstime=0;

7、processi.segmentj.pagenumk.runcount=0;processi.segmentj.pagenumk.frameNumber=-1;processi.segment0.startAddress=0;int address=processi.segment0.startAddress;for(int c=0;c<processi.segtotalnumber;c+)processi.segmentc+1.startAddress=address+processi.segmentc.segmentlength*1024;void InitMemory()int i

8、;int ran1,ran2,ran3,ran4;printf("*內(nèi)存初始化*n");printf("請(qǐng)輸入內(nèi)存塊數(shù)正整數(shù)形式:n");scanf("%d",&blocknumbers);while(blocknumbers<=0|blocknumbers>AssumeBlock)printf("警告:內(nèi)存輸入無效,請(qǐng)重新輸入!n");scanf("%d",&blocknumbers);AvailableBlock=blocknumbers;printf("

9、;請(qǐng)輸入內(nèi)存中塊大小正整數(shù)形式n");scanf("%d",&blocksize);while(blocksize<=0|blocksize>20)printf("警告:塊大小輸入無效,請(qǐng)重新輸入!n");scanf("%d",&blocksize);/*初始時(shí)內(nèi)存中各塊均初始化為空*/for(i=0;i<blocknumbers;i+)memoryi=0;srand(unsigned)time(NULL);ran1=rand()%64;ran2=rand()%64;ran3=rand()%

10、64;ran4=rand()%64;memoryran1=1;memoryran2=1;memoryran3=1;memoryran4=1;printf("已被占用的塊有:%d,%d,%d,%dn",ran1,ran2,ran3,ran4);void printfAvailable(int j)int i=0;AvailableBlock=blocknumbers-occupied;if(AvailableBlock!=0)printf("對(duì)于%d號(hào)進(jìn)程有%d塊可用",j,AvailableBlock);printf("n");int

11、 Judge(int use,int block1)if(use>=block1)printf("內(nèi)存已滿n");return 2;else return 3;int Judgekeepmemory(int i,int inpage1)if(inpage1>=Keepmemory)printf("%d號(hào)進(jìn)程的駐留集已滿n",i);return 2;else return 3;void printTable(int j)printf("*%d號(hào)進(jìn)程的段表信息如下*n",j);printf(" 段號(hào) 在內(nèi)存中 段長

12、基地址 n");for(int m=0;m<processj.segtotalnumber;m+)printf(" %d %d %d %dn",m,processj.segmentm.segFlag,processj.segmentm.segmentlength,processj.segmentm.startAddress);for(int k=0;k<processj.segtotalnumber;k+)printf("n");printf("n");printf("*%d號(hào)進(jìn)程%d號(hào)段的頁表信息如下

13、*n",j,k);for(int m=0;m<processj.segmentk.segmentlength;m+)printf("頁號(hào) 塊號(hào) 載入時(shí)間 n");printf(" %d%d %d n",m,processj.segmentk.pagenumm.frameNumber,processj.segmentk.pagenumm.runcount);printf("n");void Allocate()occupied=0;int mem;int i=0;int page1,seg;printf("*內(nèi)

14、存分配*n");while(i<processnumber)mem=0;printf("n");printfAvailable(i);processi.inpage=0;processi.segreal=0;for( seg=0;seg<processi.segtotalnumber;seg+)processi.segmentseg.pagereal=0;for( page1=0;page1<processi.segmentseg.segmentlength;page1+)while(memorymem=1)/查找內(nèi)存中的空閑塊mem+;/*判斷

15、駐留集是否滿*/int judge1=Judgekeepmemory(i,processi.inpage);/*判斷內(nèi)存是否已滿*/int judge2=Judge(occupied,blocknumbers);if(judge1=2)seg=processi.segtotalnumber;page1=processi.segmentseg.segmentlength;else if(judge2=3)processi.isInmemory=1;processi.inpage+;occupied+;processi.segmentseg.pagenumpage1.frameNumber=mem

16、;processi.segmentseg.pagenumpage1.pageFlag=1;processi.segmentseg.segFlag=1;processi.segmentseg.pagereal+;memorymem=1;mem+;for(int p1=0;p1<=processi.segtotalnumber;p1+)for(int p=0;p<=processi.segmentp1.segmentlength;p+)if(processi.segmentp1.pagenump.pageFlag=1)processi.segmentp1.pagenump.runcou

17、nt+;processi.segmentp1.pagenump.accesstime+;elseprocessi.segmentp1.pagenump.frameNumber=-1;elsei=processnumber;/內(nèi)存已滿時(shí)跳出循環(huán),不再進(jìn)行內(nèi)存分配/end for(int page1=0;page1<processi.segmentseg.segmentlength;page1+)if(processi.segmentseg.segFlag=1)processi.segreal+;/end for(int seg=0;seg<processi.segtotalnumbe

18、r;seg+)i+;for(int j=0;j<processnumber;j+) printf("*%d號(hào)進(jìn)程的段表信息如下*n",j);printf(" 段號(hào) 在內(nèi)存中 段長 基地址 n");for(int m=0;m<processj.segtotalnumber;m+)printf(" %d %d %d %dn",m,processj.segmentm.segFlag,processj.segmentm.segmentlength,processj.segmentm.startAddress);for(int k=

19、0;k<processj.segtotalnumber;k+)printf("n");printf("n");printf("*%d號(hào)進(jìn)程%d號(hào)段的頁表信息如下*n",j,k);for(int m=0;m<processj.segmentk.segmentlength;m+)printf("頁號(hào) 塊號(hào) 載入時(shí)間n");printf(" %d%d %dn",m,processj.segmentk.pagenumm.frameNumber,processj.segmentk.pagenu

20、mm.runcount);printf("n");void transform(int processnumber7,int segmentnumber7, int pagenumber7,int seglong,int pagelong)processprocessnumber7.segmentsegmentnumber7.pagenumpagenumber7.frameNumber=processprocessnumber7.segmentseglong.pagenumpagelong.frameNumber;processprocessnumber7.segments

21、egmentnumber7.pagenumpagenumber7.pageFlag=1;processprocessnumber7.segmentsegmentnumber7.segFlag=1;processprocessnumber7.segmentseglong.pagenumpagelong.frameNumber=-1;processprocessnumber7.segmentseglong.pagenumpagelong.pageFlag=0;processprocessnumber7.segmentseglong.pagenumpagelong.accesstime=0;proc

22、essprocessnumber7.segmentseglong.pagenumpagelong.runcount=0;for(int p1=0;p1<=processprocessnumber7.segtotalnumber;p1+)for(int p=0;p<=processprocessnumber7.segmentp1.segmentlength;p+)if(processprocessnumber7.segmentp1.pagenump.pageFlag=1)processprocessnumber7.segmentp1.pagenump.runcount+;proces

23、sprocessnumber7.segmentp1.pagenump.accesstime+;LRU(int processnumber7,int segmentnumber7,int pagenumber7)int pagelong=0,seglong=0;int maxtime=process0.segment0.pagenum0.accesstime;for(int i=0;i<processprocessnumber7.inpage;i+)for(int j=0;j<processprocessnumber7.segtotalnumber;j+)for(int k=0;k&

24、lt;processprocessnumber7.segmentj.segmentlength;k+)if(processprocessnumber7.segmentj.pagenumk.pageFlag=1)if(maxtime<processprocessnumber7.segmentj.pagenumk.accesstime)maxtime=processprocessnumber7.segmentj.pagenumk.accesstime;seglong=j;pagelong=k;transform(processnumber7,segmentnumber7, pagenumbe

25、r7,seglong,pagelong);return 1;void FIFO(int processnumber6,int segmentnumber6,int pagenumber6)int seglong=0,pagelong=0;int longest=processprocessnumber6.segment0.pagenum0.runcount;for(int i=0;i<processprocessnumber6.inpage;i+)for(int j=0;j<processprocessnumber6.segtotalnumber;j+)for(int k=0;k&

26、lt;processprocessnumber6.segmentj.segmentlength;k+)if(longest<processprocessnumber6.segmentj.pagenumk.runcount)longest=processprocessnumber6.segmentj.pagenumk.runcount;seglong=j;pagelong=k;transform(processnumber6,segmentnumber6, pagenumber6,seglong,pagelong);/*地址映射*/void printPhysical(int proces

27、snumber5,int segmentnumber5,int pagenumber5,int offset1)int physicalAddress=processprocessnumber5.segmentsegmentnumber5.pagenumpagenumber5.frameNumber*1024;printf("該邏輯地址所對(duì)應(yīng)的物理地址是%d n", physicalAddress+offset1);/*頁面置換*/void replace(int processnumber6,int segmentnumber6,int pagenumber6)int o

28、ffset1,flag;int seg1=0;printf("%d號(hào)進(jìn)程的%d號(hào)段中的%d號(hào)頁將被裝入內(nèi)存n",processnumber6,segmentnumber6,pagenumber6);printf("輸入1表使用LRU策略,輸入其他表示使用FIFO策略n");scanf("%d",&flag);if(flag=1)LRU(processnumber6,segmentnumber6,pagenumber6 );printf("%d號(hào)進(jìn)程的%d段%d頁已被裝入內(nèi)存,請(qǐng)輸入其業(yè)內(nèi)偏移量n",proc

29、essnumber6,segmentnumber6,pagenumber6);scanf("%d",&offset1);printPhysical(processnumber6,segmentnumber6,pagenumber6,offset1);elseFIFO(processnumber6,segmentnumber6,pagenumber6);printf("%d號(hào)進(jìn)程的%d段%d頁已被裝入內(nèi)存,請(qǐng)輸入其業(yè)內(nèi)偏移量n",processnumber6,segmentnumber6,pagenumber6);scanf("%d&qu

30、ot;,&offset1);printPhysical(processnumber6,segmentnumber6,pagenumber6,offset1);printTable(processnumber6);void replaceseg(int processnumber4,int segmentnumber4,int seglength)int offset1,flag;int seg1=0;for(int p=0;p<=processprocessnumber4.segmentsegmentnumber4.pagereal;p+)printf("%d號(hào)進(jìn)程的%

31、d號(hào)段中的%d號(hào)頁將被裝入內(nèi)存n",processnumber4,segmentnumber4,p);printf("輸入1表使用LRU策略,輸入其他表示使用FIFO策略n");scanf("%d",&flag);if(flag=1)LRU(processnumber4,segmentnumber4,p );printf("%d號(hào)進(jìn)程的%d段%d頁已被裝入內(nèi)存,請(qǐng)輸入其業(yè)內(nèi)偏移量n",processnumber4,segmentnumber4,p);scanf("%d",&offset1)

32、;printPhysical(processnumber4,segmentnumber4,p,offset1);elseFIFO(processnumber4,segmentnumber4,p);printf("%d號(hào)進(jìn)程的%d段%d頁已被裝入內(nèi)存,請(qǐng)輸入其業(yè)內(nèi)偏移量n",processnumber4,segmentnumber4,p);scanf("%d",&offset1);printPhysical(processnumber4,segmentnumber4,p,offset1);/*進(jìn)程缺段缺頁判斷*/void Locate()int p

33、rocessnumber4,segmentnumber4,pagenumber4;printf("請(qǐng)輸入你想要訪問的進(jìn)程號(hào)n");scanf("%d",&processnumber4);if(processnumber4<=processnumber)if(processprocessnumber4.isInmemory!=1)printf("該進(jìn)程不在內(nèi)存中n");elseprintf("請(qǐng)輸入你想要訪問的段號(hào)n");scanf("%d",&segmentnumber4)

34、;if(segmentnumber4>processprocessnumber4.segtotalnumber)printf("段地址越界,訪問失敗n");else if(processprocessnumber4.segmentsegmentnumber4.segFlag!=1)printf("該段不在內(nèi)存中,將進(jìn)行缺段處理n");replaceseg(processnumber4,segmentnumber4,processprocessnumber4.segmentsegmentnumber4.segmentlength);elseprint

35、f("請(qǐng)輸入你想要訪問的頁號(hào)n");scanf("%d",&pagenumber4);if(pagenumber4>processprocessnumber4.segmentsegmentnumber4.segmentlength)printf("頁地址越界,訪問失敗n");elseif(processprocessnumber4.segmentsegmentnumber4.pagenumpagenumber4.pageFlag!=1)printf("該頁不在內(nèi)存中,將進(jìn)行缺頁處理n");replac

36、e(processnumber4,segmentnumber4,pagenumber4);elseprintf("請(qǐng)輸入頁內(nèi)偏移量n");scanf("%d",&offset);processprocessnumber4.segmentsegmentnumber4.pagenumpagenumber4.accesstime=1;processprocessnumber4.segmentsegmentnumber4.pagenumpagenumber4.runcount+;printPhysical(processnumber4,segmentnumber4,pagenumber4,offset);for(int p1=0;p1<=processprocessnumber4.segreal;p1+)for(int p=0;p<=

溫馨提示

  • 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)論