Merge branch 'master' into vfs-dev
[lunaix-os.git] / lunaix-os / kernel / demos / iotest.c
index 99300bdb4783daa2b9e4503825f909c7d7e7de35..5e17b02db766f66f56d4c2ac625c73e1bb47c8ed 100644 (file)
@@ -6,6 +6,9 @@
 
 LOG_MODULE("IOTEST")
 
+#define STDIN 1
+#define STDOUT 0
+
 void
 _iotest_main()
 {
@@ -18,12 +21,7 @@ _iotest_main()
     //  Lunaix会尽可能缓存任何对此设备的上层读写,并使用延迟写入策略。(FO_DIRECT可用于屏蔽该功能)
     int fd = open("/dev/sda", 0);
 
-    // tty 设备 - 控制台。
-    //  tty设备属于序列设备(Sequential Device),该类型设备的上层读写
-    //  无须经过Lunaix的缓存层,而是直接下发到底层驱动。(不受FO_DIRECT的影响)
-    int tty = open("/dev/tty", 0);
-
-    if (fd < 0 || tty < 0) {
+    if (fd < 0) {
         kprintf(KERROR "fail to open (%d)\n", geterrno());
         return;
     }
@@ -36,21 +34,29 @@ _iotest_main()
         write(fd, test_sequence, sizeof(test_sequence));
     }
 
-    lseek(fd, 521, FSEEK_SET);
+    // 随机读写测试
+    lseek(fd, 4 * 4096, FSEEK_SET);
     write(fd, test_sequence, sizeof(test_sequence));
 
-    // 读出我们写的内容
     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));
 
     // 将读出的内容直接写入tty设备
-    write(tty, read_out, sizeof(read_out));
-    write(tty, "\n", 1);
+    write(STDOUT, read_out, sizeof(read_out));
+    write(STDOUT, "\n", 1);
 
     // 关闭文件,这同时会将页缓存中的数据下发到底层驱动
     close(fd);
-    close(tty);
 
     kprint_hex(read_out, sizeof(read_out));
 }
\ No newline at end of file