X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/blobdiff_plain/7515e526342f6ee07cbe92f5a458f1c2c4a1fcaf..d8d001a6664b88b66524989589fcd809de6d3a92:/lunaix-os/kernel/demos/iotest.c diff --git a/lunaix-os/kernel/demos/iotest.c b/lunaix-os/kernel/demos/iotest.c index 2ddeb1a..0cf7cba 100644 --- a/lunaix-os/kernel/demos/iotest.c +++ b/lunaix-os/kernel/demos/iotest.c @@ -1,5 +1,6 @@ #include #include +#include #include #include @@ -12,24 +13,30 @@ _iotest_main() "There were two regal sisters who ruled together " "and created harmony for all the land."; - int fd = open("/dev/sda", 0); // bd0 设备 - 硬盘 - int tty = open("/dev/tty", 0); // tty 设备 - 控制台 + int fd = open("/dev/sda", 0); // sda 设备 - 硬盘 + + // tty 设备 - 控制台,使用O_DIRECT打开,即所有IO绕过Lunaix内核的缓存机制 + int tty = open("/dev/tty", FO_DIRECT); if (fd < 0 || tty < 0) { kprintf(KERROR "fail to open (%d)\n", geterrno()); return; } - // 移动指针至第二个逻辑扇区(LBA=1),并写入 - lseek(fd, 1, FSEEK_SET); + // 移动指针至512字节,在大多数情况下,这是第二个逻辑扇区的起始处 + lseek(fd, 512, FSEEK_SET); + write(fd, test_sequence, sizeof(test_sequence)); + lseek(fd, 521, FSEEK_SET); write(fd, test_sequence, sizeof(test_sequence)); // 读出我们写的内容 char read_out[256]; - lseek(fd, -1, FSEEK_CUR); + lseek(fd, 512, FSEEK_SET); read(fd, read_out, sizeof(read_out)); + // 将读出的内容直接写入tty设备 write(tty, read_out, sizeof(read_out)); + write(tty, "\n", 1); close(fd); close(tty);