After two days of power stop, I suddenly had to be idle. Finding a broken guitar is no longer a string.
Ding ding Dangdang knocked on the power. To pick up time, stay up late for three or four o'clock.
From my top postsX01.lab. DownloadDownloadX01. OS .12.tar.gz, Decompress the package and enter the OS directory on the terminal. Enter the bochs command to see the following interface:
Note that the following four lines are open, write, read, and unlink files. The call code is as follows:
1 void TestA() { 2 int fd, n; 3 char path[] = "/test"; 4 char bufw[] = "hello"; 5 const int Len = 3; 6 char bufr[Len]; 7 8 fd = open(path , O_CREAT | O_RDWR); 9 Print("fd: %d\n", fd);10 11 n = write(fd, bufw, StrLength(bufw));12 Print("write ok!\n");13 14 close(fd);15 16 fd = open(path, O_RDWR);17 n = read(fd, bufr, Len);18 bufr[n] = 0;19 Print("read: %s\n", bufr);20 21 close(fd);22 23 if (unlink(path) == 0)24 Print("unlink file: %s", path);25 26 // Spin("TestA");27 while (1) {28 MilliDelay(2000);29 }30 }
Testa
After pressing F2, you can switch to tty2 and enter "hello" and "this is a test" respectively. The following page is displayed:
The call code is as follows:
1 void TestB() { 2 char ttyName[] = "/dev_tty1"; 3 int stdin = open(ttyName, O_RDWR); 4 Assert(stdin == 0); 5 int stdout = open(ttyName, O_RDWR); 6 Assert(stdout == 1); 7 char buf[128]; 8 9 while (1) {10 write(stdout, "$ ", 2);11 // Spin("write");12 int r = read(stdin, buf, 70);13 buf[r] = 0;14 if ( StrCompare(buf, "hello") == 0 ) {15 write(stdout, "hello world!\n", 13);16 } else {17 if (buf[0]) {18 write(stdout, "{", 1);19 write(stdout, buf, r);20 write(stdout, "}\n", 2);21 }22 }23 }24 25 Assert(0);26 while (1) {27 MilliDelay(2000);28 }29 }
Testb
The two-way comparison shows that functions such as opening, reading, writing, and deleting a file are available, and tty is also successfully incorporated into the file system.
The file system itself is not complicated, but the structure of the super block and the index node is two. However, the implementation of the file system is quite complicated. It involves not only hard disk operations, but also TTY and inter-process communication. In a sense, the file system is at the core of the entire operating system. Because the user program itself is just a file. To find out the file system, read books and read code.
It takes several days to finally run the file system successfully.
X01. OS. 13: File System