LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How does sendfile() operate internally? (https://www.linuxquestions.org/questions/linux-newbie-8/how-does-sendfile-operate-internally-928077/)

Joohi 02-07-2012 07:17 AM

How does sendfile() operate internally?
 
I want to know whether the sendfile() function in Linux works by implementing splice() function or not, for the purpose of copying the contents of one file to another.
If I write the following:

sendfile(source,dest,NULL,block);

Or this:
splice(source,0,pipefd[1],NULL,block,SPLICE_F_MORE|SPLICE_F_MOVE);
splice(pipefd[0],NULL,dest,0,block,SPLICE_F_MORE|SPLICE_F_MOVE);

Will both methods work in a similar way internally?
Does sendfile() also make use of pipes?

NEQTAN 02-07-2012 03:52 PM

Sendfile() can be used to transmit data from a "file" to a "socket"to get data from one machine to another machine. Sendfile() needs the source of data to be "page/buffer" cache. Sendfile does copy data.

Splice requires that at least either the source or target file descriptors be a pipe. It only reads and writes. It does not actually copy.

References for your benefit:

http://blog.superpat.com/2010/06/01/...le-and-splice/

kerneltrap.org/node/6505 (Linus Torvalds word on the topic of splice() and tee(). )

Hope this helps.

PTrenholme 02-07-2012 04:28 PM

Quote:

Originally Posted by NEQTAN (Post 4596415)
Sendfile() can be used to transmit data from a "file" to a "socket"to get data from one machine to another machine. Sendfile() needs the source of data to be "page/buffer" cache. Sendfile does copy data.

Splice requires that at least either the source or target file descriptors be a pipe. It only reads and writes. It does not actually copy.

References for your benefit:

http://blog.superpat.com/2010/06/01/...le-and-splice/
http://kerneltrap.org/node/6505 (Linus Torvalds word on the topic of splice() and tee(). )

Hope this helps.

Restating the second reference as a link . . .

Joohi 02-07-2012 11:11 PM

The second link that you provided, proved to be helpful. Thank you for the help.
But I believe you were wrong when you said that sendfile() can be used to copy contents between a file and a socket. The sendfile() man page clearly states that since Linux kernels 2.6.33, the destination file descriptor can be either a socket or a regular file. In the latter case, sendfile copies contents of one file to another.

NEQTAN 02-08-2012 08:54 PM

Glad I could help. Sorry for the improper definition of sendfile() .

You should mark in your initial post TITLE that the topic is solved, if your satisfied.


All times are GMT -5. The time now is 03:47 PM.