Nachos试验10设计并实现具有二级索引的文件系统文档良心出品_第1页
Nachos试验10设计并实现具有二级索引的文件系统文档良心出品_第2页
Nachos试验10设计并实现具有二级索引的文件系统文档良心出品_第3页
Nachos试验10设计并实现具有二级索引的文件系统文档良心出品_第4页
Nachos试验10设计并实现具有二级索引的文件系统文档良心出品_第5页
免费预览已结束,剩余7页可下载查看

付费下载

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、实验目的最大能存取 NumDirect * SectorSize 的大小的文件,本次试Nachos系统原有的文件系统只支持单级索引, 验的目的: 理解文件系统的组织结构 扩展原有的文件系统,设计并实现具有二级索引的文件系统.实验环境linux操作系统,Nachos操作系统实验分析在文件头的定义中描述了:#define NumDirect (SectorSize - 2 * sizeof(int) / sizeof(int) 为了说明方便,经过实际计算,NumDirect = 30.二级索引的文件系统的filehdr首先,通过观察 Nachos原有的filehdr (即上图左边的局部),可知 N

2、achos的单级索引的文件系统最大 只支持存取29个扇区大小的文件.为了扩展二级索引,取数组的最后一个dataSectors29作为存取新的dataSectors 数组块的索弓I,定义 dataSectors0 - dataSectors28存取数据所在的块号,dataSectors29=-1表示无二级索引块,为正值表示二级索引dataSectors2所在的索引块.当文件超过原dataSectors数组所能能够存取的大小 28的时候,通过bitmap为文件头的dataSectors2分配空间,返回的 Sector号 存在 dataSectors29中.fileSys每次读取filehdr的时候

3、,仍然只读取原filehdr,如果想要访问和修改dataSectors2中的内容,那么在filehdr中先通过dataSectors29获取到dataSectors2 的扇区号,通过调用synchDisk ->ReadSector(dataSectorslastIndex, (char *)dataSectors2) ,读入 dataSectors2 的内容,然后再进行 dataSectors数组29-62号所对应的数据块的读取.由于本次实验是在实验 5的根底上进行更改的,即支持文件的扩展,这就要求不仅要有读取dataSectors2数组的方法,还要可以重新写入dataSectors2的

4、方法.实现方法也就是首先如果需要访问dataSectors2 ,那么首先调用 synchDisk -> ReadSector (dataSectorslastIndex, (char *)dataSectors2) ,读入 dataSectors2的内容,然后进行各种应用程序的读写操作,最后调用synchDisk -> WriteSector(dataSectorslastIndex, (char *)dataSectors2) ,将更改后的结果写回.由分析可知,文件系统的二级索引功能的扩展只针对filehdr ,所有的修改的都只在filehdr.cc中进行,连头文件filehdr

5、.h也不涉及.关键源代码及注释filehdr.cc在头文件中添加 dataSectors2 的大小定义:#define NumDirect2 (SectorSize / sizeof(int)更改 MaxFileSize :#define MaxFileSize (NumDirect + NumDirect2)* SectorSize)转向filehdr.cc进行说明,所有函数体中绿色局部均为本次实验的注释(建议看Allocate和Deallocate就好了,其他的原理类似,appSectors涉及实验5的局部):/ filehdr.cc省略无数责任声明/ 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 dataSect

10、ors /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();dat

11、aSectorslastIndex = -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;

12、 i < 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

13、);return TRUE;AppSectors/FileHeader:AppSectors/LiZhen 10/31/2021/ 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 *fr

14、eeMap, 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;

15、elseint 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(mor

16、eFileSize, 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 n

17、eed the 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 allocat

18、e a new sector to stroe/the Secondary index block - dataSectors2./At last, write back the secondary index block into the sector.else(for( ; i< lastindex ; i+)dataSectorsi= freeMap ->Find();dataSectorslastindex = freeMap ->Find();int dataSectors2NumDirect2;for ( ; i < numSectors ; i+)data

19、Sectors2i - 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

20、sector,else(int dataSectors2NumDirect2;synchDisk->ReadSector(dataSectorslastindex, (char *)dataSectors2);for( ; i < numSectors; i+)dataSectors2i-lastindex = freeMap -> Find();synchDisk->WriteSector(dataSectorslastindex, (char *)dataSectors2);)return TRUE;)Deallocate/ FileHeader:Deallocat

21、e/ De-allocate 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

22、 as original.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 t

23、his file./ At 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 *)dataSec

24、tors2);freeMap->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 (

25、the/ offset in 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) (

26、int lastindex = NumDirect - 1;if(offset / SectorSize < lastindex)return(dataSectorsoffset / SectorSize);else(int dataSectors2NumDirect2;synchDisk->ReadSector(dataSectorslastIndex, (char *)dataSectors2);return (dataSectors2offset / SectorSize - lastIndex);Print/ FileHeader:Print/ Print the cont

27、ents of the file 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 secondar

28、y index,/ handle it 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-

29、>ReadSector(dataSectorsi, 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 t

30、here is a secondary 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("FileHead

31、er contents. File size: %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->Re

32、adSector(dataSectorsi, 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); for( ; i < numSectors; i+) synchDis

33、k->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("n&qu

34、ot;);)delete data;)没有进行任何更改的函数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:0000000000

35、000000000Directory file header:FileHeader contents. File size: 200. File blocks: 3 4File contents:00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000Bi

36、tmap 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, s

37、ent 0Cleaning up.然后在终端中敲入如下代码,将 big文件复制到Nachos中:studentlocalhost lab10$ ./nachos -cp test/big big并执行append命令13次左右:studentlocalhost lab10$ ./nachos -ap test/big big最后显示结果关键局部加粗显示,其余局部一扫而过:studentlocalhost lab10$ ./nachos -DBit map file header:FileHeader contents. File size: 128. File blocks:2File con

38、tents: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, 24

39、, 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 abig

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论