FYI: sendfile system call

22 views
Skip to first unread message

Enrique Arizón Benito

unread,
Dec 19, 2014, 9:12:51 AM12/19/14
to mongoos...@googlegroups.com
Hi all,

I've been using Mongoose for an small project to allow transmitting/streaming video files from an Android device. It works like a charm!. Thanks to all the developers involved!

My mail is just to notice an API existing in most (all?) Unixes called "sendfile". I completely ignore if you are already aware of it so I send this e-mail "just-in-case".

In a Copy&Paste from "The Linux Programming Interface" (Michael Kerrish):
"""
Application such as web servers frequently need to transfer disk files through a socket.
One way to do this would be a loop of the following form:
while ((n = read(diskfilefd, buf, BUZ_SIZE)) > 0) write(sockfd, buf, n);

For many applications, such a loop is perfectly acceptable. However, if we frequently transfer large files it's inefficient.
We must use two system calls (possibly multiple times within a loop): one to copy the file contents from the kernel
buffer cache into user space, and the other to copy the user-space buffer back to kernel space in order to be transmitted via the socket.

This is wasteful if the application doesn't perform any processing of the file contents before transmission.

The sendfile() system call is designed to eliminate this inefficiency. The file contents are transferred directly to the socket, without passing through user space.

"""

I didn't have the time to look at the source code and ignore the internals of Mongoose but I guess It must be easy to use it in platforms with the supported sendfile API. (#ifndef WIN32)

Note: Since new Android/iPhone phones/tables can record H264 files at 10Mbs/sec or more with video files growing to hundreds of Megabytes, I think this "large file" scenario could become more and more usual.

Regards!

Enrique

Sergey Lyubka

unread,
Dec 19, 2014, 9:23:21 AM12/19/14
to mongoose-users
Thanks Enrique,

yes we are quite aware about sendfile().
Cheers and happy holidays,

Sergey

--
You received this message because you are subscribed to the Google Groups "mongoose-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoose-user...@googlegroups.com.
To post to this group, send email to mongoos...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongoose-users.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages