x01.os.13: 文件系统

时间:2023-03-09 02:23:54
x01.os.13: 文件系统

停了两天电,忽然得空闲。找来破吉他,已然不成弦。

          丁丁当当敲,敲到电来到。为把时间捡,熬夜三四点。

从我的置顶随笔 x01.Lab.Download 中下载 x01.os.12.tar.gz, 解压后由终端进入 os 目录,输入 bochs  命令即可见到如下界面:

x01.os.13: 文件系统

注意下面的四行,分别是 open,write, read, unlink 文件。调用代码如下:

 void TestA() {
int fd, n;
char path[] = "/test";
char bufw[] = "hello";
const int Len = ;
char bufr[Len]; fd = open(path , O_CREAT | O_RDWR);
Print("fd: %d\n", fd); n = write(fd, bufw, StrLength(bufw));
Print("write ok!\n"); close(fd); fd = open(path, O_RDWR);
n = read(fd, bufr, Len);
bufr[n] = ;
Print("read: %s\n", bufr); close(fd); if (unlink(path) == )
Print("unlink file: %s", path); // Spin("TestA");
while () {
MilliDelay();
}
}

TestA

按 F2 后,可切换到 tty2, 分别输入 hello 回车,this is a test 回车,可看到如下界面:

x01.os.13: 文件系统

其调用代码如下:

 void TestB() {
char ttyName[] = "/dev_tty1";
int stdin = open(ttyName, O_RDWR);
Assert(stdin == );
int stdout = open(ttyName, O_RDWR);
Assert(stdout == );
char buf[]; while () {
write(stdout, "$ ", );
// Spin("write");
int r = read(stdin, buf, );
buf[r] = ;
if ( StrCompare(buf, "hello") == ) {
write(stdout, "hello world!\n", );
} else {
if (buf[]) {
write(stdout, "{", );
write(stdout, buf, r);
write(stdout, "}\n", );
}
}
} Assert();
while () {
MilliDelay();
}
}

TestB

两向对照,不难看出,文件的打开,读写,删除等功能已经具备,而 tty 也成功纳入了文件系统。

文件系统,本身并不复杂,不过超级块和索引节点两个结构。但文件系统的实现,却颇为繁难,不仅涉及到硬盘的操作,而且也涉及到 tty,进程间通信等诸多方面。在某种意义上讲,文件系统处于整个操作系统的核心。因为用户程序本身,也不过是个文件而已。搞清文件系统,一是看书,二是看代码,别无他途。

耗时多日,终于将文件系统运行成功,是为记。