I want to share/transfer data between two process locally/network. General IPC mechanism shared memory and Message Queues can be used to transfer data. But these mechanisms involve multiple copies.
I came across zero copy mechanism, which reduces the copying overhead
on CPU. Linux supports this using sendfile
and splice
. These APIs are not in POSIX.
How can I achieve zero copy using only POSIX APIs?
Shared memory between two processes is zero-copy if you keep the shared data in the shared memory. Otherwise there has to be a copy somewhere (e.g. into and out of shared mem). You can reduce this to one copy if one of the processes keeps the shared data in shared memory, and the other process just reads it from there.
The Linux man pages for sendfile(2)
and vmsplice(2)
don't mention a POSIX alternative, so I doubt there is one. To send data between processes with only one copy, set up a pipe between them and use vmsplice
to put pages into the pipe with zero-copy. On the receiving end, I think just use read(2)
to get the pages out of the pipe.
Over the network, zero-copy is even harder. Why no zero-copy networking in linux kernel? has some comments and answers. The receive side would be hard to implement on top of the usual socket API, unless it only worked when a thread was blocked on read(2)
on the socket. Otherwise, how would it know where in the process's virtual memory to put the packet?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With