Skip to content

Commit

Permalink
finish init of buffer manager
Browse files Browse the repository at this point in the history
  • Loading branch information
Mionger committed Feb 20, 2020
1 parent d7cb76f commit 0b66524
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 36 deletions.
1 change: 1 addition & 0 deletions FileSystem/src/Buf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Buf::Buf()

this->b_dev = -1;
this->b_wcount = 0;
this->b_addr = NULL;
this->b_blkno = 0;
}

Expand Down
49 changes: 49 additions & 0 deletions FileSystem/src/BufferManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "BufferManager.h"
#include<iostream>
using namespace std;

BufferManager::BufferManager()
{
Initialize();
}

BufferManager::~BufferManager()
{

}

/* 缓存控制块队列的初始化。将缓存控制块中b_addr指向相应缓冲区首地址。 */
void BufferManager::Initialize()
{
// cout << "Starting initialize buffer manager..." << endl;

/* 初始化自由队列和NODEV队列 */
this->bFreeList.b_forw = &(this->bFreeList);
this->bFreeList.b_back = &(this->bFreeList);
this->bFreeList.av_forw = &(this->bFreeList);
this->bFreeList.av_back = &(this->bFreeList);

Buf *buf_ptr;
for (int i = 0; i < this->NBUF; i++)
{
buf_ptr = &(this->m_Buf[i]);

/* 缓冲区定位 */
buf_ptr->b_addr = this->Buffer[i];
// Buffer[i][0] = 'a' + i;

Buf *temp = this->bFreeList.b_forw;
/* 初始化自由队列 */
buf_ptr->b_forw = temp;
temp->b_back = buf_ptr;
this->bFreeList.b_forw = buf_ptr;
buf_ptr->b_back = &(this->bFreeList);
/* 初始化NODEV队列 */
buf_ptr->av_back = temp;
temp->av_forw = buf_ptr;
this->bFreeList.av_back = buf_ptr;
buf_ptr->av_forw = &(this->bFreeList);

// cout << "Insert one buffer succeed!" << endl;
}
}
42 changes: 7 additions & 35 deletions FileSystem/src/BufferManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,23 @@
class BufferManager
{
public:
/* 静态常属性 */
static const int NBUF = 15; /* 缓存控制块、缓冲区的数量 */
static const int BUFFER_SIZE = 512; /* 缓冲区大小。 以字节为单位 */

/* 方法 */
Buf bFreeList; /* 自由缓存队列控制块 */

public:
/* 构造函数 */
BufferManager();
/* 析构函数 */
~BufferManager();

void Initialize(); /* 缓存控制块队列的初始化。将缓存控制块中b_addr指向相应缓冲区首地址。*/

Buf* GetBlk(short dev, int blkno); /* 申请一块缓存,用于读写设备dev上的字符块blkno。*/
void Brelse(Buf* bp); /* 释放缓存控制块buf */
void IOWait(Buf* bp); /* 同步方式I/O,等待I/O操作结束 */
void IODone(Buf* bp); /* I/O操作结束善后处理 */

Buf* Bread(short dev, int blkno); /* 读一个磁盘块。dev为主、次设备号,blkno为目标磁盘块逻辑块号。 */
Buf* Breada(short adev, int blkno, int rablkno); /* 读一个磁盘块,带有预读方式。
* adev为主、次设备号。blkno为目标磁盘块逻辑块号,同步方式读blkno。
* rablkno为预读磁盘块逻辑块号,异步方式读rablkno。 */
void Bwrite(Buf* bp); /* 写一个磁盘块 */
void Bdwrite(Buf* bp); /* 延迟写磁盘块 */
void Bawrite(Buf* bp); /* 异步写磁盘块 */

void ClrBuf(Buf* bp); /* 清空缓冲区内容 */
void Bflush(short dev); /* 将dev指定设备队列中延迟写的缓存全部输出到磁盘 */
bool Swap(int blkno, unsigned long addr, int count, enum Buf::BufFlag flag);
/* Swap I/O 用于进程图像在内存和盘交换区之间传输
* blkno: 交换区中盘块号;addr: 进程图像(传送部分)内存起始地址;
* count: 进行传输字节数,byte为单位;传输方向flag: 内存->交换区 or 交换区->内存。 */
Buf& GetSwapBuf(); /* 获取进程图像传送请求块Buf对象引用 */
Buf& GetBFreeList(); /* 获取自由缓存队列控制块Buf对象引用 */

private:
void GetError(Buf* bp); /* 获取I/O操作中发生的错误信息 */
void NotAvail(Buf* bp); /* 从自由队列中摘下指定的缓存控制块buf */
Buf* InCore(short adev, int blkno); /* 检查指定字符块是否已在缓存中 */

private:
Buf bFreeList; /* 自由缓存队列控制块 */
Buf SwBuf; /* 进程图像传送请求块 */
Buf m_Buf[NBUF]; /* 缓存控制块数组 */
Buf m_Buf[NBUF]; /* 缓存控制块数组 */
unsigned char Buffer[NBUF][BUFFER_SIZE]; /* 缓冲区数组 */

void Initialize(); /* 缓存控制块队列的初始化。将缓存控制块中b_addr指向相应缓冲区首地址。 */

void BlkInsert(); /* 将一个缓存块插入到自由队列队尾 */
};

#endif
14 changes: 13 additions & 1 deletion FileSystem/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
#include <iostream>
#include "UserInterface.h"
#include "BufferManager.h"

int main()
{
BufferManager os_BufferManager;
// cout << "Initialized buffer manager..." << endl;
// Buf *buf_ptr = os_BufferManager.bFreeList.b_back;
// while (buf_ptr != &(os_BufferManager.bFreeList))
// {
// cout << char(buf_ptr->b_addr[0]) << endl;
// buf_ptr = buf_ptr->b_back;
// }

string user_name = "root";
UserInterface UI;
UI.GetCmd(user_name);
// UI.GetCmd(user_name);


return 0;
}

0 comments on commit 0b66524

Please sign in to comment.