Mabus
Mabus

Reputation: 1493

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.

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).

Is there anything equivalent to perform fifo I/O in packet mode?

Upvotes: 0

Views: 1524

Answers (3)

Brandon Maier
Brandon Maier

Reputation: 21

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. So using fnctl() to set O_DIRECT on a named pipe is valid.

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

Upvotes: 2

Eric
Eric

Reputation: 24876

Both pipe & fifo are byte stream, it open a file descriptor, then use read() and write() to do communication.

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. 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 (e.g 0), then the send & receive order is the same as FIFO.

If that's what u want, then check man mq_overview for details.

Upvotes: 1

user2404501
user2404501

Reputation:

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:

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.

So you can try fixing that patch and applying it (should be easy - looks like a typo, the f should be 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. 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.)

Upvotes: 1

Related Questions