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
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.
As you have pointed out, the only difference is the flags. send/recv are for networking, whereas read/write are general I/O functions for any file descriptor. send is only useful vs write when you want to use a flag, since the flags are all network related, it doesn't make sense to call send on a non-network file descriptor (nor am I sure whether it's even valid).
Also you should note:
The in_fd argument must correspond to a file which supports mmap(2)-like operations (i.e., it cannot be a socket).
Which means you can't copy from a socket (you can copy to a socket and prior to 2.6.33 you must copy to a socket).
If fd
is a socket file descriptor, then these system calls are identical:
send(fd, data, length, 0)
is the same as write(fd, data, length)
recv(fd, data, length, 0)
is the same as read(fd, data, length)
So, unless you need to set a non-zero flags
parameter, it makes no difference whether you use send/recv
or write/read
.
The sendfile
system call is an optimization. If you have a socket sockfd
and a regular file filefd
and you want to copy some file data to the socket (e.g. if you're a web server serving up a file), then you might write it like this:
// Error checking omitted for expository purposes
while(not done)
{
char buffer[BUFSIZE];
int n = read(filefd, buffer, BUFSIZE);
send(sockfd, buffer, n, 0);
}
However, this is inefficient: this involves the kernel copying the file data into userspace (in the read
call) and then copying the same data back into kernel space (in the send
call).
The sendfile
system call lets us skip all of that copying and have the kernel directly read the file data and send it on the socket in one fell swoop:
sendfile(sockfd, filefd, NULL, BUFSIZE);