Lets say I know that a file descriptor fd
is open for reading in my process. I would like to pipe data from this fd
into a fifo
that is available for reading outside my of process, in a way that avoids calling poll
or select
on fd
and manually reading/forwarding data. Can this be done?
问题:
回答1:
You mean ask the OS to do that behind the scenes on an ongoing basis from now on? Like an I/O redirection?
No, you can't do that.
You could spawn a thread that does nothing but read the file fd and write the pipe fd, though. To avoid the overhead of copying memory around in read(2)
and write(2)
system calls, you can use sendfile(out_fd, in_fd, NULL, 4096)
to tell the kernel to copy a page from in_fd
to out_fd
. See the man page.
You might have better results with splice(2)
, since it's designed for use with files and pipes. sendfile(2)
used to require out_fd
to be a socket. (Designed for zero-copy sending static data on TCP sockets, e.g. from a web server.)
Linux does have asynchronous I/O, so you can queue up a read or write to happen in the background. That's not a good choice here, because you can't queue up a copy from one fd to another. (no async splice(2)
or sendfile(2)
). Even if there was, it would have a specific request size, not a fire-and-forget keep copying-forever. AFAIK, threads have become the preferred way to do async I/O, rather than the POSIX AIO facilities.