Nachos文件系统目录管理的解读

Nachos文件系统目录管理的解读

首先在实现目录树中对目录项结构做了修改,导致所占数据增多,按照原来的解读,所创建的文件个数也该相应减少(受限于目录项个数),但是却没有,为什么?

问题

当我对目录结构进行修改,改成多叉树时,会增加目录表项的占用空间,如果说目录表内容只有2个扇区的话,通过计算按理说创建五个目录表项就会出现问题,但是缺没有,这是为什么呢?

原因

首先 当format 初始化文件系统的时候

//----------------------------------------------------------------------
// FileSystem::FileSystem
// 	Initialize the file system.  If format = TRUE, the disk has
//	nothing on it, and we need to initialize the disk to contain
//	an empty directory, and a bitmap of free sectors (with almost but
//	not all of the sectors marked as free).  
//
//	If format = FALSE, we just have to open the files
//	representing the bitmap and the directory.
//
//	"format" -- should we initialize the disk?
//----------------------------------------------------------------------

FileSystem::FileSystem(bool format)
{ 
    DEBUG('f', "Initializing the file system.\n");
    if (format) {
        BitMap *freeMap = new BitMap(NumSectors);
        Directory *directory = new Directory(NumDirEntries);
        FileHeader *mapHdr = new FileHeader;
        FileHeader *dirHdr = new FileHeader;

        DEBUG('f', "Formatting the file system.\n");

        // First, allocate space for FileHeaders for the directory and bitmap
        // (make sure no one else grabs these!)
        freeMap->Mark(FreeMapSector);	    
        freeMap->Mark(DirectorySector);

        // Second, allocate space for the data blocks containing the contents
        // of the directory and bitmap files.  There better be enough space!

        ASSERT(mapHdr->Allocate(freeMap, FreeMapFileSize));
        ASSERT(dirHdr->Allocate(freeMap, DirectoryFileSize));

        // Flush the bitmap and directory FileHeaders back to disk
        // We need to do this before we can "Open" the file, since open
        // reads the file header off of disk (and currently the disk has garbage
        // on it!).

        DEBUG('f', "Writing headers back to disk.\n");
	    mapHdr->WriteBack(FreeMapSector);    
	    dirHdr->WriteBack(DirectorySector);

        // OK to open the bitmap and directory files now
        // The file system operations assume these two files are left open
        // while Nachos is running.

        freeMapFile = new OpenFile(FreeMapSector);
        directoryFile = new OpenFile(DirectorySector);
     
        // Once we have the files "open", we can write the initial version
        // of each file back to disk.  The directory at this point is completely
        // empty; but the bitmap has been changed to reflect the fact that
        // sectors on the disk have been allocated for the file headers and
        // to hold the file data for the directory and bitmap.

        DEBUG('f', "Writing bitmap and directory back to disk.\n");
	    freeMap->WriteBack(freeMapFile);	 // flush changes to disk
	    directory->WriteBack(directoryFile);

        if (DebugIsEnabled('f')) {
            freeMap->Print();
            directory->Print();

            delete freeMap; 
            delete directory; 
            delete mapHdr; 
            delete dirHdr;
        }
    } else {
        // if we are not formatting the disk, just open the files representing
        // the bitmap and directory; these are left open while Nachos is running
        freeMapFile = new OpenFile(FreeMapSector);
        directoryFile = new OpenFile(DirectorySector);
    }
}

可以看出首先

Directory *directory = new Directory(NumDirEntries);

#define NumDirEntries 		20

所以目录表里面新建了 20个目录表项

之后标记的是 Bitmap 和 Directory的文件头,分别是0 / 1 sector

freeMap->Mark(FreeMapSector);	    
freeMap->Mark(DirectorySector);

再往后

ASSERT(mapHdr->Allocate(freeMap, FreeMapFileSize));
ASSERT(dirHdr->Allocate(freeMap, DirectoryFileSize));

问题来了 Allocate的 DirectoryFileSize 是多少呢

#define DirectoryFileSize 	(sizeof(DirectoryEntry) * NumDirEntries)

所以Nachos分配的目录表的大小并不是固定的3、4扇区,而是根据具体的表项个数以及表项大小进行动态分配的

所以 我改成了 20个表项,就目录表而言,必须要能放开20个表项的,但是至于往后磁盘的其他空间,他并不关心.

在我插入五个文件之后

./nachos -D

我的目录项

class DirectoryEntry {
  public:
    bool inUse;				// Is this directory entry in use?
    int sector;				// Location on disk to find the 
					//   FileHeader for this file 
    char name[FileNameMaxLen + 1];	// Text name for file, with +1 for 
					// the trailing '\0'
    int parent;       // father dir 's index
    int Child_Num;    // dir's child num
    int children[MaxDirChildNUm];    // dir's children
};
#define MaxDirChildNUm    10 // the number of child that a dir can have

Entry Size = 1 + 4 + 10 + 8 + 10*4 bytes = 63bytes

而20个Entry 则20 * 63= 1260 bytes

1260 / 128 = 10 (向上取整),故最后目录表要占10个扇区,3 ~ 13

image-20220427122511555

所以其他文件的数据都是从0x704开始写,最高地址为0x20004 即文件数据块能占 32*32 - 13 个扇区

1260 / 128 = 10 (向上取整),故最后目录表要占10个扇区,3 ~ 13

[外链图片转存中…(img-rfOU0D9g-1651035093466)]

所以其他文件的数据都是从0x704开始写,最高地址为0x20004 即文件数据块能占 32*32 - 13 个扇区

但是受限于设置的目录表个数,因此最多还是20个文件。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
山东大学的操作系统课程中,学生通常会进行课设项目,其中一项是实现一个nachos操作系统nachos是一个开源的教学操作系统,旨在帮助学生理解操作系统的原理以及实现方式。它提供了一个轻量级的操作系统框架,可以在模拟的硬件上运行。通过进行nachos操作系统课设,学生能够深入学习操作系统的内部机制和实现细节。 在山东大学的课设中,学生通常需要从头开始实现一个简单的nachos操作系统。他们需要理解操作系统的基本原理,如进程管理、内存管理文件系统和设备管理等。然后,他们可以利用nachos提供的框架,根据自己的设计思路逐步实现这些功能。 在实现过程中,学生会面临许多挑战和困难。他们需要处理进程调度、内存分配、文件系统的设计和实现,以及对设备的管理等。他们需要通过深入研究和不断的试验来解决这些问题,从而加深对操作系统的理解。 这个课设对于学生来说是一次非常有价值的实践和学习机会。通过亲手实现一个操作系统,他们可以更好地理解操作系统的工作原理,并掌握操作系统的设计和实现技巧。同时,这也是一个锻炼他们团队合作和问题解决能力的过程。 总之,山东大学的nachos操作系统课设对于学生来说是一次难得的学习机会。通过实践和探索,他们可以更深入地理解操作系统,提升自己的技能和能力。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tototototorres

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值