服务器编程——函数splice实现零拷贝使用解释

splice函数用于在两个文件描述符之间移动数据,也是零拷贝操作。

函数原型如下:

       #define _GNU_SOURCE         /* See feature_test_macros(7) */
       #include <fcntl.h>

       ssize_t splice(int fd_in, loff_t *off_in, int fd_out,
                      loff_t *off_out, size_t len, unsigned int flags);

        splice()  moves  data  between  two  file descriptors without copying between kernel address space and user address space.  It transfers up to len bytes of data from the file descriptor fd_in to the file descriptor fd_out, where one of the descriptors must refer to a pipe.


        If fd_in refers to a pipe, then off_in must be NULL.  If fd_in does not refer to a pipe and off_in is NULL, then bytes are read from fd_in  starting  from the  current  file  offset, and the current file offset is adjusted appropriately.  If fd_in does not refer to a pipe and off_in is not NULL, then off_in must point to a buffer which specifies the starting offset from which bytes will be read from fd_in; in this case,  the  current  file offset of fd_in is not changed.  Analogous statements apply for fd_out and off_out.


       The flags argument is a bit mask that is composed by ORing together zero or more of the following values:

       SPLICE_F_MOVE      Attempt  to  move  pages instead of copying.  This is only a hint to the kernel: pages may still be copied if the kernel cannot  move the pages from the pipe, or if the pipe buffers don't refer to full pages.  The initial implementation of  this  flag  was  buggy:  therefore  starting in Linux 2.6.21 it is a no-op (but is still permitted in a splice() call); in the future, a correct implementation may be restored.

       SPLICE_F_NONBLOCK  Do not block on I/O.  This makes the splice pipe operations nonblocking, but splice() may nevertheless block because  the  file descriptors that are spliced to/from may block (unless they have the O_NONBLOCK flag set).

       SPLICE_F_MORE      More  data  will  be  coming  in  a subsequent splice.  This is a helpful hint when the fd_out refers to a socket (see also the description of MSG_MORE in send(2), and the description of TCP_CORK in tcp(7))

       SPLICE_F_GIFT      Unused for splice(); see vmsplice(2).


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值