山东大学操作系统课程设计源码分析 filesys(2)

本文分析了操作系统课程设计中的文件系统,重点讲解了位图BitMap、文件头FileHeader及打开文件OpenFile的源码实现。位图用于管理磁盘扇区的空闲状况;文件头存储文件元数据,如大小和扇区分配;打开文件是内存中的文件表示,提供读写操作。文件系统的操作涉及位图的分配与回收、文件头的扇区管理和OpenFile的读写功能。

一、写在前面

        上一节我们分析了nachos文件系统底层的裸磁盘对象Disk和线程安全磁盘对象SynchDisk,在本节我们沿着文件系统的结构继续向上进行分析,介绍FileHeader、OpenFile与BitMap相关文件.

二、源码分析

        1、位图对象:BitMap

        磁盘的最小读写单元为扇区,而BitMap就是用于表示磁盘扇区空闲状况的.BitMap本身也是一个文件,位置在0号扇区.


class BitMap {
  public:
    BitMap(int nitems);		// Initialize a bitmap, with "nitems" bits
				// initially, all bits are cleared.
    ~BitMap();			// De-allocate bitmap
    
    void Mark(int which);   	// Set the "nth" bit
    void Clear(int which);  	// Clear the "nth" bit
    bool Test(int which);   	// Is the "nth" bit set?
    int Find();            	// Return the # of a clear bit, and as a side
				// effect, set the bit. 
				// If no bits are clear, return -1.
    int NumClear();		// Return the number of clear bits

    void Print();		// Print contents of bitmap
    
    // These aren't needed until FILESYS, when we will need to read and 
    // write the bitmap to a file
    void FetchFrom(OpenFile *file); 	// fetch contents from disk 
    void WriteBack(OpenFile *file); 	// write contents to disk

  private:
    int numBits;			// number of bits in the bitmap
    int numWords;			// number of words of bitmap storage
					// (rounded up if numBits is not a
					//  multiple of the number of bits in
					//  a word)
    unsigned int *map;			// bit storage
};

        BitMap的数据成员有两个整型值和一个无符号整型指针.numBits代表BitMap中有多少个扇区的使用信息,numWords为将位图BitMap中的位使用整型表示后有多少值(32位机器中int为32位).map为表示位图数组的指针,BitMap使用无符号整型数组表示位图,因此map中的一项表示了32个扇区的信息.

        下面对BitMap的成员函数进行简要介绍.

  1. Mark:对某一位进行标记,即设置该位所表示扇区的使用情况
  2. Clear:清空某一位
  3. Test:测试某一位是否已经被使用
  4. Find:返回一个空闲扇区的序号,若不存在则返回-1
  5. NumClear:返回BitMap中空闲扇区的数量
  6. Print:打印位图的使用信息
  7. FetchFrom:取得位图文件
  8. WriteBack:写回位图文件
BitMap::BitMap(int nitems) 
{ 
    numBits = nitems;
    numWords = divRoundUp(numBits, BitsInWord);
    map = new unsigned int[numWords];
    for (int i = 0; i < numBits; i++) 
        Clear(i);
}

BitMap::~BitMap()
{ 
    delete map;
}

void 
BitMap::Clear(int which) 
{
    ASSERT(which >= 0 && which < numBits);
    map[which / BitsInWord] &= ~(1 << (which % BitsInWord));
}

bool 
BitMap::Test(int which)
{
    ASSERT(which >= 0 && which < numBits);
    
    if (map[which / BitsInWord] & (1 << (which % BitsInWord)))
	return TRUE;
    else
	return FALSE;
}
int 
BitMap::NumClear() 
{
    int count = 0;

    for (int i = 0; i < numBits; i++)
	if (!Test(i)) count++;
    return count;
}

int 
BitMap::Find() 
{
    for (int i = 0; i < numBits; i++)
	if (!Test(i)) {
	    Mark(i);
	    return i;
	}
    return -1;
}

        BitMap的构造函数接收一个整型值nitems,表示位图的大小.首先设置位图位数numBits,然后计算并设置能够表示所有位的整型值数目numWords,即numBits/32上取整.接着建立一个numWords大小的数组用于表示位图.建立位图之后,调用Clear函数将整型数组每个单元的每一位都置为0.析构函数使用delete释放动态建立的整型数组.

        Clear函数接收一个位编号,首先断言这个编号没有越界,然后找到数组中表示这个位的整型值,对这个整型值进

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值