Advanced I/O functions (2)-splice Functions

Source: Internet
Author: User

Splice function:

Function Description: it is used to move data between two file descriptors. It is also a zero copy operation. The function is defined as follows:

1 #include <fcntl.h>3   ssize_t splice(int fd_in,loff_t* off_t,int fd_out,loff_t* off_out,size_t len,unsigned int flags);

Parameter description:

Fd_in: file descriptor of the data to be entered.

Off_t: If fd_in is a pipeline file descriptor, The off_t parameter must be null, which indicates reading from the current offset position of the data stream; If fd_in is not a pipeline file descriptor (such as socket ), it specifies the specific offset position.

Len: Specifies the length of the moving data.

Flags: controls how data is moved. It can be set to the bitwise XOR or of the values in the following table.

Common flags parameters of table splice and their meanings

Common Values Description
Splice_f_move If appropriate, move data by page memory.
Splice_f_nonblock Non-blocking splice operations, but the actual effect is still affected by the blocking status of the file descriptor itself.
Splice_f_more Give the kernel a prompt: more data will be read in subsequent splice calls.
Splice_f_gift It has no effect on splice.

Note:

When using the splice function, fd_in and fd_out must have at least one pipeline file descriptor. the number of bytes to be moved after the call is successful. it may return 0, which reads data from the MPs queue and is not written to any data. -1 is returned and errno is set.

Example: Use the splice function to implement a zero-copy echo server model.

1 # include <sys/socket. h> 2 # include <netinet/in. h> 3 # include <ARPA/inet. h> 4 # include <unistd. h> 5 # include <stdlib. h> 6 # include <string. h> 7 # include <fcntl. h> 8 9 int main (INT argc, const char * argv []) {10 if (argc! = 2) {11 printf ("Usage: % s ip_address port_number \ n", argv [0]); 12 Return-1; 13} '14 15 const char * IP = argv [1]; 16 int Port = atoi (argv [2]); 17 18 int ret; 19 struct sockaddr_in address; 20 bzero (& Address, sizeof (Address); 21 address. sin_family = af_inet; 22 inet_ton (af_inet, IP, & address. sin_addr); 23 address. sin_port = htons (port); 24 25 int sockfd = socket (af_inet, sock_stream, 0); 26 assert (sockfd! =-1); 27 28 ret = BIND (sockfd, (struct sockaddr *) & Address, sizeof (Address); 29 assert (Ret! =-1); 30 31 ret = listen (sockfd, 5); 32 assert (Ret! =-1); 33 34 while (1) {35 struct sockaddr_in peer; 36 bzero (& peer, sizeof (PEER); 37 socklen_t Len = sizeof (PEER ); 38 39 int connfd = accept (sockfd, (struct sockaddr *) & peer, Len); 40 if (connfd <0) {41 printf ("errno is: % d \ n ", errno); 42 break; 43} 44 else {45 int pipefd [2]; 46 ret = pipe (pipefd); 47 assert (Ret! =-1); 48 49/* direct the client data on connfd to the pipeline */50 ret = splice (connfd, null, pipefd [1], null, 32768, splice_f_more | 51 splice_f_move); 52 assert (Ret! =-1); 53/* direct the data in the pipeline to the connfd client file descriptor */54 splice (pipefd [0], null, connfd, null, 32768, splice_f_more | 55 splice_f_move); 56 assert (Ret! =-1); 57} 58} 59 60 close (connfd); 61 close (sockfd); 62 Return 0; 63}

We use the splice function to read the content from the client to pipefd [1], and then use the splice function to read the content from pipefd [0] to the client. In this way, a simple and efficient echo service is implemented. The entire process is to execute the Recv/send operation, so data copying between the user space and the kernel space is not involved.

Advanced I/O functions (2)-splice Functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.