gpt4 book ai didi

c - 带有 mkfifo() 和 open() 的程序无法退出

转载 作者:太空宇宙 更新时间:2023-11-04 02:32:39 26 4
gpt4 key购买 nike

我正在尝试使用 FIFO 进行交互处理。但是当尝试创建一个 FIFO 然后打开它时,我的程序挂起(无法退出)。

if (mkfifo("./fifo.txt", S_IRUSR | S_IWUSE) < 0) {
fprint("Can not create fifo");
return 1;
}
if ((readfd = open("./fifo.txt", O_RDONLY)) < 0) {
return 1;
}

我在这里做错了什么?

非常感谢。

最佳答案

阅读fifo(7) ,特别是:

Normally, opening the FIFO blocks until the other end is opened also.

所以我猜你调用 open(2)被封锁。也许您想传递 O_NONBLOCK 标志。

你应该使用 strace(1)调试您的程序(也许还有 strace fifo 另一端的 other 程序)。并调用perror(3)错误。

也许使用 unix(7)套接字在您的情况下可能更相关。然后你可以 poll(2)之前accept(2)

你应该阅读 Advanced Linux Programming .

关于c - 带有 mkfifo() 和 open() 的程序无法退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41218086/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com