Nachos實(shí)驗(yàn)10設(shè)計(jì)并實(shí)現(xiàn)具有二級(jí)索引的文件系統(tǒng)_第1頁(yè)
Nachos實(shí)驗(yàn)10設(shè)計(jì)并實(shí)現(xiàn)具有二級(jí)索引的文件系統(tǒng)_第2頁(yè)
Nachos實(shí)驗(yàn)10設(shè)計(jì)并實(shí)現(xiàn)具有二級(jí)索引的文件系統(tǒng)_第3頁(yè)
Nachos實(shí)驗(yàn)10設(shè)計(jì)并實(shí)現(xiàn)具有二級(jí)索引的文件系統(tǒng)_第4頁(yè)
Nachos實(shí)驗(yàn)10設(shè)計(jì)并實(shí)現(xiàn)具有二級(jí)索引的文件系統(tǒng)_第5頁(yè)
已閱讀5頁(yè),還剩8頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、實(shí)驗(yàn)?zāi)康淖畲竽艽嫒?NumDirect * SectorSize 的大小的文件,本次試Nachos系統(tǒng)原有的文件系統(tǒng)只支持單級(jí)索引, 驗(yàn)的目的: 理解文件系統(tǒng)的組織結(jié)構(gòu) 擴(kuò)展原有的文件系統(tǒng),設(shè)計(jì)并實(shí)現(xiàn)具有二級(jí)索引的文件系統(tǒng)。實(shí)驗(yàn)環(huán)境linux操作系統(tǒng),Nachos操作系統(tǒng)實(shí)驗(yàn)分析已知在文件頭的定義中描述了:#define NumDirect (SectorSize - 2 * sizeof(int) / sizeof(int) 為了說(shuō)明方便,經(jīng)過(guò)實(shí)際計(jì)算,NumDirect = 30.二級(jí)索引的文件系統(tǒng)的filehdr首先,通過(guò)觀察 Nachos原有的filehdr (即上圖左邊的部分),可知

2、 Nachos的單級(jí)索引的文件系統(tǒng)最大 只支持存取29個(gè)扇區(qū)大小的文件。為了擴(kuò)展二級(jí)索引,取數(shù)組的最后一個(gè)dataSectors29作為存取新的dataSectors 數(shù)組塊的索弓I,定義 dataSectors0 - dataSectors28存取數(shù)據(jù)所在的塊號(hào),dataSectors29=-1表示無(wú)二級(jí)索引塊,為正值表示二級(jí)索引dataSectors2所在的索引塊。當(dāng)文件超過(guò)原dataSectors數(shù)組所能能夠存取的大小 28的時(shí)候,通過(guò)bitmap為文件頭的dataSectors2分配空間,返回的 Sector號(hào) 存在 dataSectors29中。fileSys每次讀取filehdr的

3、時(shí)候,仍然只讀取原filehdr,如果想要訪問(wèn)和修改dataSectors2中的內(nèi)容,則在filehdr中先通過(guò)dataSectors29獲取到dataSectors2 的扇區(qū)號(hào),通過(guò)調(diào)用synchDisk ->ReadSector(dataSectorslastIndex, (char *)dataSectors2) ,讀入 dataSectors2 的內(nèi)容,然后再進(jìn)行 dataSectors數(shù)組29-62號(hào)所對(duì)應(yīng)的數(shù)據(jù)塊的讀取。因?yàn)楸敬螌?shí)驗(yàn)是在實(shí)驗(yàn) 5的基礎(chǔ)上進(jìn)行更改的,即支持文件的擴(kuò)展,這就要求不僅要有讀取dataSectors2數(shù)組的方法,還要可以重新寫(xiě)入dataSectors2

4、的方法。實(shí)現(xiàn)方法也就是首先如果需要訪問(wèn)dataSectors2 ,那么首先調(diào)用 synchDisk -> ReadSector (dataSectorslastIndex, (char *)dataSectors2) ,讀入 dataSectors2的內(nèi)容,然后進(jìn)行各種應(yīng)用程序的讀寫(xiě)操作,最后調(diào)用synchDisk -> WriteSector(dataSectorslastIndex, (char *)dataSectors2) ,將更改后的結(jié)果寫(xiě)回。由分析可知,文件系統(tǒng)的二級(jí)索引功能的擴(kuò)展只針對(duì)filehdr,所有的修改的都只在filehdr.cc中進(jìn)行,連頭文件filehdr

5、.h也不涉及。關(guān)鍵源代碼及注釋filehdr.cc在頭文件中添加 dataSectors2 的大小定義:#define NumDirect2 (SectorSize / sizeof(int)更改 MaxFileSize :#define MaxFileSize (NumDirect + NumDirect2)* SectorSize)轉(zhuǎn)向filehdr.cc進(jìn)行說(shuō)明,所有函數(shù)體中綠色部分均為本次實(shí)驗(yàn)的注釋(建議看Allocate和Deallocate就好了,其他的原理類(lèi)似,appSectors涉及實(shí)驗(yàn)5的部分):/ filehdr.cc省略無(wú)數(shù)責(zé)任聲明/ LiZhen 17/11/09/ E

6、xtends the file system to double the max file size that/ Nachos can store#include "copyright.h"#include "system.h"#include "filehdr.h"Allocate/ FileHeader:Allocate/ Initialize a fresh file header for a newly created file./ Allocate data blocks for the file out of the ma

7、p of free disk blocks./ Return FALSE if there are not enough free blocks to accomodate/ the new file./ "freeMap" is the bit map of free disk sectors/ "fileSize" is the bit map of free disk sectors/ LiZhen 17/11/09/ Extends the file system to double the max file size that / Nachos

8、 can store/ Current max file size of Nachos file system is/ (NumDirect + NumDirect2) * SectorSize/ If the dataSectorsNumDirect - 1 = -1, no secondary index./ otherwise, use the dataSecotrsNumDirect - 1 to stroe the/ pointer to the secondary index block - dataSectors2口./boolFileHeader:Allocate(BitMap

9、 *freeMap, int fileSize)numBytes = fileSize;numSectors = divRoundUp(fileSize, SectorSize);if (freeMap->NumClear() < numSectors)return FALSE; / not enough spaceelse if(NumDirect + NumDirect2 <= numSectors)return FALSE;not enough pointer space/First figure out the current length of dataSector

10、s /dataSectors array index ranges from 0 to lastIndex-1 int lastIndex=NumDirect-1;/If do not need the secondary index, /do not change the original code except /assign dataSectorslastIndex = -1 if(numSectors < lastindex)for (int i = 0; i < numSectors; i+)dataSectorsi = freeMap->Find();dataSe

11、ctorslastIndex = -1;/If the numSectors excends the rage of dataSectors, /first handle the first 0-lastIndex-1 as before./Then, ask bitmap to allocate a new sector to stroe /the Secondary index block - dataSectors2./At last, write back the secondary index block into the sector.elsefor (int i = 0; i &

12、lt; lastIndex; i+)dataSectorsi = freeMap->Find();dataSectorslastIndex = freeMap->Find();int dataSectors2NumDirect2;secondary index blockfor (int i = 0; i < numSectors - NumDirect; i+)dataSectors2i = freeMap->Find();synchDisk->WriteSector(dataSectorslastIndex, (char *)dataSectors2);ret

13、urn TRUE;AppSectors/FileHeader:AppSectors/LiZhen 10/31/2009 In lab5/ add appFileSize more bytes in the current file/ refresh the bitmap to show the allocating changes./LiZhen 17/11/09/ In lab10/ Handle the reading and writing of the dataSectors2/ if needed./boolFileHeader:AppSectors(BitMap *freeMap,

14、 int appFileSize) if(appFileSize <= 0)return false;int restFileSize = SectorSize * numSectors - numBytes;/ printf("the moreFileSize is %dn",moreFileSize);/ printf("the appFileSize is %dn",appFileSize);if(restFileSize >= appFileSize)numBytes += appFileSize;return true;elsein

15、t moreFileSize = appFileSize - restFileSize;if(freeMap->NumClear()< divRoundUp(moreFileSize, SectorSize)return FALSE;else if(NumDirect + NumDirect2 <= numSectors + divRoundUp(moreFileSize, SectorSize) return FALSE;int i = numSectors;numBytes += appFileSize;numSectors += divRoundUp(moreFileS

16、ize, SectorSize);/* In lab10*/First figure out the current length of dataSectors/dataSectors array index ranges from 0 to lastIndex-1int lastIndex = NumDirect-1;/* If before appending, there is no secondary index*/if(dataSectorslastIndex = -1)/If after being appended, new dataSecoters do NOT need th

17、e secondary index,/do not change the original code.if(numSectors < lastindex)for( ; i < numSectors; i+) dataSectorsi = freeMap -> Find();/If after being appended, new dataSecoters DO need the secondary index, /first handle the first 0-lastIndex-1 as before./Then, ask bitmap to allocate a ne

18、w sector to stroe/the Secondary index block - dataSectors2./At last, write back the secondary index block into the sector.elsefor( ; i< lastindex ; i+)dataSectorsi= freeMap ->Find();dataSectorslastIndex = freeMap ->Find();int dataSectors2NumDirect2;for ( ; i < numSectors ; i+)dataSectors

19、2i - lastindex = freeMap->Find();synchDisk->WriteSector(dataSectorslastIndex, (char *)dataSectors2);/* If before appending, there is already a secondary index*/First read the dataSectors2 from the Disk./Then, append the file size./At last, write back the secondary index block into the sector,e

20、lseint dataSectors2NumDirect2;synchDisk->ReadSector(dataSectorslastIndex, (char *)dataSectors2);for( ; i < numSectors; i+)dataSectors2i-lastIndex = freeMap -> Find();synchDisk->WriteSector(dataSectorslastIndex, (char *)dataSectors2);return TRUE;Deallocate/ FileHeader:Deallocate/ De-alloc

21、ate all the space allocated for data blocks for this file./ "freeMap" is the bit map of free disk sectors/ LiZhen 17/11/09/ Deallocate the dataSectors2 if needed./voidFileHeader:Deallocate(BitMap *freeMap)int lastIndex = NumDirect - 1;/ If there is no secondary index,/ handle it as origina

22、l.if(dataSectorslastIndex=-1)for (int i = 0; i < numSectors; i+)ASSERT(freeMap->Test(int) dataSectorsi); / ought to be marked!freeMap->Clear(int) dataSectorsi);/ If there is a secondary index,/ first read in the dataSectors2 from the Disk./ Then, deallocate the data blocks for this file./ A

23、t last, deallocate the block that dataSector2 locates.elseint i=0;for ( ; i < lastindex; i+)ASSERT(freeMap->Test(int) dataSectorsi); / ought to be marked!freeMap->Clear(int) dataSectorsi);int dataSectors2NumDirect2;synchDisk->ReadSector(dataSectorslastIndex, (char *)dataSectors2);freeMap

24、->Clear(int) dataSectorslastIndex);for( ; i < numSectors; i+)freeMap->Clear(int) dataSectors2i-lastIndex);ByteToSectors/ FileHeader:ByteToSector/ Return which disk sector is storing a particular byte within the file./ This is essentially a translation from a virtual address (the/ offset in

25、the file) to a physical address (the sector where the/ data at the offset is stored)./ "offset" is the location within the file of the byte in question / LiZhen 17/11/09/ Handle the reading and writing of the dataSectors2 / if needed./intFileHeader:ByteToSector(int offset) int lastindex =

26、NumDirect - 1;if(offset / SectorSize < lastindex)return(dataSectorsoffset / SectorSize);elseint dataSectors2NumDirect2;synchDisk->ReadSector(dataSectorslastIndex, (char *)dataSectors2);return (dataSectors2offset / SectorSize - lastindex);Print/ FileHeader:Print/ Print the contents of the file

27、header, and the contents of all the data blocks pointed to by the file header./ LiZhen 17/11/09/ Handle the reading and writing of the dataSectors2/ if needed./voidFileHeader:Print()int i, j, k;int lastindex = NumDirect - 1;char *data = new charSectorSize;/ If there is no secondary index,/ handle it

28、 as original.if(dataSectorslastIndex = -1)printf("FileHeader contents. File size: %d. File blocks:n", numBytes);for (i = 0; i < numSectors; i+)printf("%d ", dataSectorsi);printf("nFile contents:n");for (i = k = 0; i < numSectors; i+) synchDisk->ReadSector(dataS

29、ectorsi, data);for (j = 0; (j < SectorSize) && (k < numBytes); j+, k+) if (''040' <= dataj && dataj <= '176')/ isprint(dataj)printf("%c", dataj);elseprintf("%x", (unsigned char)dataj);printf("n");/ If there is a secondary

30、index,/ first read in the dataSectors2 from the Disk./ Then, deallocate the data blocks for this file./ At last, deallocate the block that dataSector2 locates.elseint dataSectors2NumDirect2;synchDisk->ReadSector(dataSectorslastIndex, (char *)dataSectors2);printf("FileHeader contents. File si

31、ze: %d. File blocks:n", numBytes);for (i = 0; i < lastIndex; i+)printf("%d ", dataSectorsi);for(; i < numSectors; i+)printf("%d ", dataSectors2i - lastIndex);printf("nFile contents:n");for (i = k = 0; i < lastIndex; i+) synchDisk->ReadSector(dataSectors

32、i, data);for (j = 0; (j < SectorSize) && (k < numBytes); j+, k+) if ('040' <= dataj && dataj <= '176')/ isprint(dataj)printf("%c", dataj);elseprintf("%x", (unsigned char)dataj);printf("n");for( ; i < numSectors; i+) synchDi

33、sk->ReadSector(dataSectors2i - lastindex, data);for (j = 0; (j < SectorSize) && (k < numBytes); j+, k+) if (''040' <= dataj && dataj <= '176')/ isprint(dataj)printf("%c", dataj);elseprintf("%x", (unsigned char)dataj);printf("

34、;n");delete data;沒(méi)有進(jìn)行任何更改的函數(shù)void FileHeader:FetchFrom(int sector)void FileHeader:WriteBack(int sector) int FileHeader:FileLength()調(diào)試記錄編譯好Nachos之后,在終端敲入一下命令:studentlocalhost lab10$ ./nachos D得到運(yùn)行結(jié)果(請(qǐng)一掃而過(guò)):Bit map file header:FileHeader contents. File size: 128. File blocks:2File contents:0000000

35、000000000000Directory file header:FileHeader contents. File size: 200. File blocks: 3 4File contents:0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

36、0Bitmap set:0,1,2, 3, 4,Directory contents:No threads ready or runnable, and no pending interrupts.Assuming the program completed.Machine halting!Ticks: total 5400, idle 5060, system 340, user 0Disk I/O: reads 10, writes 0Console I/O: reads 0, writes 0Paging: faults 0Network I/O: packets received 0,

37、 sent 0Cleaning up.然后在終端中敲入如下代碼,將 big文件復(fù)制到Nachos中:studentlocalhost lab10$ ./nachos -cp test/big big并執(zhí)行append命令13次左右:studentlocalhost lab10$ ./nachos -ap test/big big最后顯示結(jié)果(關(guān)鍵部分加粗顯示,其余部分一掃而過(guò)):studentlocalhost lab10$ ./nachos -DBit map file header:FileHeader contents. File size: 128. File blocks:2File

38、 contents:0000000000000000000Directory file header:FileHeader contents. File size: 200. File blocks:3 4File contents:00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000Bitmap set:0,1,2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,

39、 24, 25, 26, 27, 28, 29, 30,31,32, 33, 34, 35, 36, 37, 38, 39, 40, 41,42, 43, 44,Directory contents:Name: big, Sector: 5FileHeader contents. File size: 4760. File blocks:6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 36 37 38 39 40 4142 43 44File contents:big file big file big file big file big file a

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 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ì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論