Linux Network Programming IX: Splice function, efficient 0 copies

Source: Internet
Author: User
Tags assert

from:http://blog.csdn.net/jasonliuvip/article/details/22600569

Linux Network Programming IX: Splice function, efficient 0 copies

Recently looking at "Linux High Performance Server Programming", here to do a diary to motivate themselves, while sharing in need of friends.


1. Splice function

[CPP]View PlainCopy
    1. #include <fcntl.h>
    2. ssize_t Splice (int fd_in, loff_t *off_in, int fd_out, loff_t *off_out, size_t len, unsigned int flags);


Splice is used to move data between two file descriptors, which is also a 0 copy.

The fd_in parameter is the descriptor to be entered. If it is a pipe file descriptor, the off_in must be set to NULL, otherwise the off_in indicates where to start reading from the input data stream and, if NULL, is read from the current offset of the input data stream.

The fd_out/off_out is the same as above, but for output.

The len parameter specifies the length of the moving data.

The flags parameter controls how the data is moved:

    • The splice_f_nonblock:splice operation is not blocked. However, if the file descriptor is not set to an I/O that is not blocked, then the call splice may still be blocked.
    • Splice_f_more: Tells the OS kernel that the next SPLICE system call will have more data coming.
    • Splice_f_move: If the output is a file, this value causes the operating system kernel to attempt to read the data directly from the input pipeline buffer into the output address space, and this data transfer process does not occur without any data copy operations.

2. When using splice, at least one of the fd_in and fd_out must be a pipe file descriptor.

Returns the number of bytes moved when the call succeeds; it may return 0, indicating that no data needs to be moved, which usually happens when the pipe is read from the pipeline and the pipeline is not written.

Return-1 On failure, and set errno


3. Code: Through splice the contents of the client into the pipeline, and then read from the pipeline to the client, so as to achieve efficient and simple echo service. The entire process does not perform recv/send, and therefore does not involve user-space-to-kernel-space data copies.

[CPP]View PlainCopy
    1. echo Server implemented with splice
    2. #include <stdio.h>
    3. #include <stdlib.h>
    4. #include <unistd.h>
    5. #include <sys/socket.h>
    6. #include <netinet/in.h>
    7. #include <arpa/inet.h>
    8. #include <assert.h>
    9. #include <errno.h>
    10. #include <string.h>
    11. #include <fcntl.h>
    12. int main (int argc, char **argv)
    13. {
    14. if (argc <= 2) {
    15. printf ("Usage:%s IP port\n", basename (Argv[0]));
    16. return 1;
    17. }
    18. const Char *ip = argv[1];
    19. int port = atoi (argv[2]);
    20. struct sockaddr_in address;
    21. Bzero (&address, sizeof (address));
    22. address.sin_family = af_inet;
    23. Address.sin_port = htons (port);
    24. Inet_pton (Af_inet, IP, &address.sin_addr);
    25. int sock = socket (pf_inet, sock_stream, 0);
    26. ASSERT (sock >= 0);
    27. int reuse = 1;
    28. setsockopt (sock, Sol_socket, SO_REUSEADDR, &reuse, sizeof (reuse));
    29. int ret = bind (sock, (struct sockaddr*) &address, sizeof (address));
    30. ASSERT (Ret! =-1);
    31. RET = Listen (sock, 5);
    32. ASSERT (Ret! =-1);
    33. struct SOCKADDR_IN client;
    34. socklen_t client_addrlength = sizeof (client);
    35. int connfd = Accept (sock, (struct sockaddr*) &client, &client_addrlength);
    36. if (CONNFD < 0) {
    37. printf ("errno is:%s\n", Strerror (errno));
    38. }
    39. else {
    40. int pipefd[2];
    41. RET = pipe (PIPEFD); //Create Pipelines
    42. ASSERT (Ret! =-1);
    43. //Direct client data on CONNFD to the pipeline
    44. ret = Splice (CONNFD, NULL, pipefd[1], NULL,
    45. 32768, Splice_f_more | Splice_f_move);
    46. ASSERT (Ret! =-1);
    47. //directs the output of the pipe to the CONNFD
    48. ret = Splice (pipefd[0], NULL, CONNFD, NULL,
    49. 32768, Splice_f_more | Splice_f_move);
    50. ASSERT (Ret! =-1);
    51. Close (CONNFD);
    52. }
    53. Close (sock);
    54. return 0;
    55. }

Linux Network Programming IX: Splice function, efficient 0 copies

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.