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

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、實驗目的最大能存取 NumDirect * SectorSize 的大小的文件,本次試Nachos系統(tǒng)原有的文件系統(tǒng)只支持單級索引, 驗的目的: 理解文件系統(tǒng)的組織結構 擴展原有的文件系統(tǒng),設計并實現(xiàn)具有二級索引的文件系統(tǒng)。實驗環(huán)境linux操作系統(tǒng),Nachos操作系統(tǒng)實驗分析已知在文件頭的定義中描述了:#define NumDirect (SectorSize - 2 * sizeof(int) / sizeof(int) 為了說明方便,經過實際計算,NumDirect = 30.二級索引的文件系統(tǒng)的filehdr首先,通過觀察 Nachos原有的filehdr (即上圖左邊的部分),可知

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

3、時候,仍然只讀取原filehdr,如果想要訪問和修改dataSectors2中的內容,則在filehdr中先通過dataSectors29獲取到dataSectors2 的扇區(qū)號,通過調用synchDisk ->ReadSector(dataSectorslastIndex, (char *)dataSectors2) ,讀入 dataSectors2 的內容,然后再進行 dataSectors數(shù)組29-62號所對應的數(shù)據(jù)塊的讀取。因為本次實驗是在實驗 5的基礎上進行更改的,即支持文件的擴展,這就要求不僅要有讀取dataSectors2數(shù)組的方法,還要可以重新寫入dataSectors2

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

5、.h也不涉及。關鍵源代碼及注釋filehdr.cc在頭文件中添加 dataSectors2 的大小定義:#define NumDirect2 (SectorSize / sizeof(int)更改 MaxFileSize :#define MaxFileSize (NumDirect + NumDirect2)* SectorSize)轉向filehdr.cc進行說明,所有函數(shù)體中綠色部分均為本次實驗的注釋(建議看Allocate和Deallocate就好了,其他的原理類似,appSectors涉及實驗5的部分):/ filehdr.cc省略無數(shù)責任聲明/ 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;沒有進行任何更改的函數(shù)void FileHeader:FetchFrom(int sector)void FileHeader:WriteBack(int sector) int FileHeader:FileLength()調試記錄編譯好Nachos之后,在終端敲入一下命令:studentlocalhost lab10$ ./nachos D得到運行結果(請一掃而過):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文件復制到Nachos中:studentlocalhost lab10$ ./nachos -cp test/big big并執(zhí)行append命令13次左右:studentlocalhost lab10$ ./nachos -ap test/big big最后顯示結果(關鍵部分加粗顯示,其余部分一掃而過):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. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論