简体   繁体   English

Linux 中的 Fifos 数据包模式

[英]Fifos in Linux in packet mode

I have read the Linux manpage of pipe2, that states that you can use the O_DIRECT flag to create a pipe that performs I/O in packet mode.我已经阅读了 pipe2 的 Linux联机帮助页,其中指出您可以使用 O_DIRECT 标志来创建在数据包模式下执行 I/O 的管道。

I haven't found anything similar for fifos (named pipes) although I have heard that pipes and fifos share a lot of code in Linux, and it could be useful to me in a project (we already pass messages in fifos, but we have to seek for an especial terminator, reading one byte at a time).我还没有找到类似 fifos(命名管道)的任何东西,尽管我听说管道和 fifos 在 Linux 中共享了很多代码,并且它可能对我在一个项目中有用(我们已经在 fifos 中传递消息,但我们有寻找一个特殊的终止符,一次读取一个字节)。

Is there anything equivalent to perform fifo I/O in packet mode?有什么等效于在数据包模式下执行 fifo I/O 的吗?

The O_* flags on a file descriptor can usually be changed by fcntl(fd, F_SETFL, ...) but in this case I don't think it will work, because of this:文件描述符上的O_*标志通常可以由fcntl(fd, F_SETFL, ...)但在这种情况下,我认为它不会起作用,因为:

https://lkml.org/lkml/2015/12/15/480 https://lkml.org/lkml/2015/12/15/480

It's a patch that was submitted just 2 weeks ago to support exactly this use case, and the only replies to it have been a couple of build failures from an automated tester.这是一个仅在 2 周前提交的补丁,以完全支持此用例,而对它的唯一答复是来自自动化测试人员的几次构建失败。

So you can try fixing that patch and applying it (should be easy - looks like a typo, the f should be filp )...所以你可以尝试修复那个补丁并应用它(应该很容易 - 看起来像一个错字, f应该是filp )......

or (this is the option I'd prefer) see if your needs can be met by an AF_UNIX , SOCK_SEQPACKET socket instead of a pipe with this new flag.或者(这是我更喜欢的选项)看看您的需求是否可以通过AF_UNIXSOCK_SEQPACKET套接字而不是带有这个新标志的管道来满足。 They're more powerful and more portable.它们更强大更便携。

(I assume you already tried passing the O_DIRECT to open when you open the named pipe, since that's the most obvious thing.) (我假设您已经尝试在打开命名管道时传递O_DIRECTopen ,因为这是最明显的事情。)

Both pipe & fifo are byte stream, it open a file descriptor , then use read() and write() to do communication. pipefifo都是字节流,它打开一个file descriptor ,然后使用read()write()进行通信。

If u want a packet (I am not sure what u mean, I assume you want to read a block of data without determine the boundary by yourself), POSIX - message queue might be a good choice.如果您想要一个packet (我不确定您的意思,我假设您想在不自行确定边界的情况下读取数据块), POSIX - message queue可能是一个不错的选择。 It send / receive data in unit of a message instead of byte by byte.它以消息为单位而不是逐字节发送/接收数据。

And you can also set priority on the messages which change the order of receiving, if all messages have the same priority (eg 0), then the send & receive order is the same as FIFO.并且您还可以对改变接收顺序的消息设置priority ,如果所有消息具有相同的优先级(例如0),则发送和接收顺序与FIFO相同。

If that's what u want, then check man mq_overview for details.如果这就是您想要的,请查看man mq_overview以了解详细信息。

I'm not able to switch to a socket or message queue, and am stuck with using pipe.我无法切换到套接字或消息队列,并且坚持使用管道。 But good news, the patch refereed to in user2404501's answer eventually got accepted and is in linux v4.5 and newer.但好消息是,user2404501 的回答中提到的补丁最终被接受,并且在 linux v4.5 和更新版本中。 So using fnctl() to set O_DIRECT on a named pipe is valid.所以使用 fnctl() 在命名管道上设置 O_DIRECT 是有效的。

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=0dbf5f20652108106cb822ad7662c786baaa03ff https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=0dbf5f20652108106cb822ad7662c786baaa03ff

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM