Splice is a newly added zero-copy data sending function in linux2.6 kernel. It is mainly used to send data to or receive data from the pipeline. Similar to splice, the zero copy sending function also has sendfile. The difference is that sendfile sends data to the peer end through socket. The so-called zero copy refers to (compared with the traditional read/write mode), in the process of data transmission, there is no need to apply for a buffer for data in the user State, that is, data copies between the user and kernel are not generated (moves data between two file descriptors without copying ).
Between kernel address space and user addressspace ). For example, the process of copying files using the classic read/write method is as follows:
1. buf = malloc (len) \ first apply for a memory with a length of len
2. read (fd1, buf, len) \ reads len Length data in fd1 of the first file into buf
3. write (fd2, buf, len) \ write data in the buf to the fd2 File
In non-direct io scenarios, file data will be copied twice between the page cache in the kernel state and the buf in the user State (one read process and one write process)
Splice System calls the following parameters:
Ssize_t splice (int fd_in, loff_t * off_in, int fd_out,
Loff_t * off_out, size_t Len, unsigned int flags );
Int fd_in: file descriptor for reading data
Loff_t * off_in: the starting offset of the data to be read.
Int fd_out: The file descriptor of the data to be written.
Loff_t * off_out: Start offset of the data to be written.
Size_t Len: length of data to be written
Unsigned int flags: Flag
Note that, in the application called by the splice system, either fd_in or fd_out must have a descriptor of the pipeline.
The process for copying files using splice is as follows:
1. Call mkfifo or pipe to create an MPS queue.
2. splice (file_fd1, & off_in_1, pipe_fd_w, & off_out_w, Len, 0) \ move data in the fd1 file to the write end of the Pipeline
3. splice (pipe_fd_r, & off_in_r, file_fd2, & off_out_2, Len, 0) \ move data to file 2 through the read end of the Pipeline
However, the 2.6.32 kernel test showed that the performance of copying files in splice mode is not higher than that in read/write mode, which indicates there is room for improvement in this aspect.