C, sendfile() and send() difference?

后端 未结 3 2019
孤独总比滥情好
孤独总比滥情好 2021-01-31 11:07

sendfile() copies data between two file descripters within kernel space. Somewhere I saw if you are writing a web server in C in linux you should use send() and recv() instead o

3条回答
  •  鱼传尺愫
    2021-01-31 11:16

    send is specified by the POSIX standard, which says:

    The send() function is equivalent to sendto() with a null pointer dest_len argument, and to write() if no flags are used.

    sendfile is Linux-specific. It tells the kernel to do zero-copy I/O from a file to a socket. (Note that it only works when the source fd is a file and the destination is a socket; for generic Linux-specific zero-copy I/O, read about splice().)

    Note that there is rarely any need to use Linux-specific zero-copy I/O. The standard and portable read+write (or send) loop with a small userspace buffer (8K-16K) will generally keep that buffer in L1 cache, making it equivalent to "zero-copy" from the system RAM's point of view.

    So unless your profiling shows a difference for your particular application, stick to standard interfaces. Just MHO.

提交回复
热议问题