Zero-copy is a concept in which unnecessary copies when transferring data are eliminated. For example when reading data from Disk and moving data over the Network multiple times the data is copied to different buffers, e.g. Kernel space to use space. This all can be eliminated if the Kernel just directly moves the data to the Network Interface Card.

The name zero copy refers here to that no copies to User space are done. There Kernel still has to copy the data.

Other example:

  • Remote Direct Memory Acces.

Example

Using os.sendfile in python to use zero copy.

import socket
import os

# Create a TCP socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(('0.0.0.0', 9000))
server_socket.listen(1)

print("Waiting for a connection...")
conn, addr = server_socket.accept()
print(f"Connected by {addr}")

# Open the file to send
file_path = "large_file.dat"
with open(file_path, "rb") as f:
    # Use os.sendfile to transfer the file directly from disk to socket
    offset = 0
    file_size = os.fstat(f.fileno()).st_size
    while offset < file_size:
        sent = os.sendfile(conn.fileno(), f.fileno(), offset, file_size - offset)
        offset += sent

conn.close()
server_socket.close()