X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/blobdiff_plain/6fefc6f5af83f316e18b0cb9dbbde1cea0b98add..a5338b60e111972364a8bc6f07011c6defd213d2:/lunaix-os/kernel/demos/iotest.c?ds=sidebyside diff --git a/lunaix-os/kernel/demos/iotest.c b/lunaix-os/kernel/demos/iotest.c index 3fc8914..5e17b02 100644 --- a/lunaix-os/kernel/demos/iotest.c +++ b/lunaix-os/kernel/demos/iotest.c @@ -1,29 +1,62 @@ #include #include +#include #include #include LOG_MODULE("IOTEST") +#define STDIN 1 +#define STDOUT 0 + void _iotest_main() { char test_sequence[] = "Once upon a time, in a magical land of Equestria. " "There were two regal sisters who ruled together " "and created harmony for all the land."; - int fd = open("/dev/block/bd0", 0); + + // sda 设备 - 硬盘 + // sda设备属于容积设备(Volumetric Device), + // Lunaix会尽可能缓存任何对此设备的上层读写,并使用延迟写入策略。(FO_DIRECT可用于屏蔽该功能) + int fd = open("/dev/sda", 0); if (fd < 0) { kprintf(KERROR "fail to open (%d)\n", geterrno()); return; } - lseek(fd, 1, FSEEK_SET); + // 移动指针至512字节,在大多数情况下,这是第二个逻辑扇区的起始处 + lseek(fd, 512, FSEEK_SET); + + // 总共写入 64 * 136 字节,会产生3个页作为缓存 + for (size_t i = 0; i < 64; i++) { + write(fd, test_sequence, sizeof(test_sequence)); + } + + // 随机读写测试 + lseek(fd, 4 * 4096, FSEEK_SET); write(fd, test_sequence, sizeof(test_sequence)); - lseek(fd, -1, FSEEK_CUR); char read_out[256]; + write(STDOUT, "input: ", 8); + int size = read(STDIN, read_out, 256); + + write(STDOUT, "your input: ", 13); + write(STDOUT, read_out, size); + write(fd, read_out, size); + write(STDOUT, "\n", 1); + + // 读出我们写的内容 + lseek(fd, 512, FSEEK_SET); read(fd, read_out, sizeof(read_out)); - kprintf("%s", read_out); + // 将读出的内容直接写入tty设备 + write(STDOUT, read_out, sizeof(read_out)); + write(STDOUT, "\n", 1); + + // 关闭文件,这同时会将页缓存中的数据下发到底层驱动 + close(fd); + + kprint_hex(read_out, sizeof(read_out)); } \ No newline at end of file