Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I achieve zero copy mechanism in POSIX?

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?

like image 253
anikhan Avatar asked Feb 09 '23 02:02

anikhan


1 Answers

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?

like image 68
Peter Cordes Avatar answered Feb 15 '23 10:02

Peter Cordes