好的,所以我正在使用 FIFO,我正在尝试构建一个小型库以在未来的程序中使用。
它应该能够创建一个命名管道,对其进行读取和写入。
我能够完成所有这些功能,但读取不正确。问题如下:
如果我说它应该读取大小为 10 个字符的缓冲区,那么只有当我写入超过 10 个字符时它才会正确打印,例如:
写入=“0123456789”->读取=“”写入=“012345678910111213”->读取=“”,“10111213”
我尝试了更多代码,并对异常“with”进行了小调试。如果读取失败,则存在此异常......它的作用是:关闭,等待,重新启动,最后返回 0(无)
然后我有一个 if 仅当字符数仅大于 0 时才正确,否则将打印“空”
所以,发生的事情是: write = "0123456789" -> reads = "empty" write = "012345678910111213" -> reads = "empty","10111213"
因此,在前 10 个字符上,他说读取失败,但他将它们从 FIFO 中删除。为什么?
抱歉,如果有点混乱。这是代码:
阅读程序:
let fifo_name = "la_fila";;
(*define the length of the buffer*)
let buflen = 2;;
let main () =
Printf.printf "Hello! Any readers? ...\n";
flush stdout;
while true do
print_string ("I've read \"" ^ bufferRead fifo_name buflen ^ "\" from the fifo!\n");
flush stdout
done;;
(* run it *)
bufferCreate fifo_name;;
let _ = main ();;
这是缓冲区库:
(* Buffer Functions *)
let bufferCreate name =
try Unix.mkfifo name 0o664 with
Unix.Unix_error(n,f,arg) -> Printf.printf "%s(%s) : %s\n" f arg
(Unix.error_message n);;
let bufferRead name size =
let frd = Unix.openfile name [Unix.O_RDONLY;Unix.O_NONBLOCK] 0o644 in
let rec aux () =
let nothing = flush_all ()
in
let buffer = String.create size in
let n = try (Unix.read frd buffer 0 size) with
Unix.Unix_error(n,f,arg) -> begin (); aux (); 0; end
in
if n > 0 then String.sub buffer 0 n else "empty"
in
aux ();;
(*(String.sub buffer 0 n)*)
let bufferWrite name str =
let length = String.length str in
let fwr = Unix.openfile name [Unix.O_WRONLY] 0o644 in
Unix.write fwr str 0 length;;
编辑
open Buffers;;
let fifo_name = "la_fila";;
let main () =
Printf.printf "CUCKOO! any consumer down there? ...\n";
flush stdout;
Printf.printf "cuckoo! Here comes a consumer! \n";
let rec reget () =
Printf.printf "-type something for it\n";
flush stdout;
let str = read_line() (*here it blocks*) in
bufferWrite fifo_name str;
reget () in
reget ();;
(* run it *)
bufferCreate fifo_name;;
main ();;
我想要一些光,它杀了我......
谢谢