c中的命名管道
我正在尝试在 C 中使用命名管道,但遇到了一些困难。就匿名管道而言,我只需使用读/写描述符创建管道,然后每次想要进行读或写时关闭另一端。这很容易做到,因为我每次都可以打开()和关闭()另一端。
对于命名管道,我有点困惑,我找到了创建命名管道的指令 mkfifo()
,但不明白如何正确读取和写入它。
谢谢
I am trying to use named pipes in C and am running into some difficulty. In terms of anonymous pipes, I just create the pipe with the r/w descriptors and then close the opposite end every time I want to do a read or write. This is easy to do since I can just open() and close() the other end every time.
With named pipes, I am a bit confused, I found the instruction mkfifo()
which creates the named pipe but don't understand how to read and write to it properly.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 mkfifo() 创建管道后(这可以在过去的任何时候完成 - 命名管道存在于文件系统中,直到它们被取消链接),读取端使用 < code>open("/path/to/pipe", O_RDONLY) 写入端使用
open("/path/to/pipe", O_WRONLY)
打开它。之后它就可以像匿名管道一样使用。
After the pipe has been created with
mkfifo()
(which could have been done at any point in the past - named pipes exist in the filesystem until they're unlinked), the reading side opens it usingopen("/path/to/pipe", O_RDONLY)
and the writing side opens it withopen("/path/to/pipe", O_WRONLY)
.After that it can be used just like an anonymous pipe.
没什么大不了的。使用 mkfifo 创建管道,然后让进程像任何文件一样对其进行读写。它也不是 C 特定的。你可以这样做:
mkfifo testfifo
cat testfifo
然后在另一个窗口中
echo "hello, world" >测试fifo
There's nothing much to it. Use mkfifo to make the pipe and then have your processes read and write to it like any file. It's not C specific either. You can do this:
mkfifo testfifo
cat testfifo
And then in another window
echo "hello, world" > testfifo
我认为你应该只使用管道,因为它们处理不同进程之间的数据传输,无论每个进程花费多少时间
I think you should just use the pipes, cause they handle the data transmission among the different processes no matter the time each proccess takes