I've just found out there is sendfile(). In the man page of sendfile(), it says:
sendfile()is more efficient than the combination ofread(2)andwrite(2).
So, I wrote a function to copy a file using sendfile().
Is really my code more efficient than the combination of read(2) and write(2)?
/* leftsize is initialized with total size of source file. */
fd_src = open(src_fname, O_RDONLY);
if (fd_src<0){
printf("open failed(%s)\n", src_fname);
return -1;
}
fd_dest = creat(dest_fname, 0666);
if (fd_dest<0){
printf("create failed(%s)\n", dest_fname);
return -1;
}
while (leftsize > 0) {
n = sendfile(fd_dest, fd_src, NULL, max_size);
if (n<0) {
printf("sendfile failed. ret:%d errno:%d\n", n, errno);
}
leftsize -= n;
}