English 中文(简体)
Is writing to a socket an arbitrary limitation of the sendfile() syscall?
原标题:

Prelude

sendfile() is an extremely useful syscall for two reasons:

First, it s less code than a read()/write() (or recv()/send() if you prefer that jive) loop.
Second, it s faster (less syscalls, implementation may copy between devices without buffer, etc...) than the aforementioned methods.

Less code. More efficient. Awesome.

In UNIX, everything is (mostly) a file. This is the ugly territory from the collision of platonic theory and real-world practice. I understand that sockets are fundamentally different than files residing on some device. I haven t dug through the sources of Linux/*BSD/Darwin/whatever OS implements sendfile() to know why this specific syscall is restricted to writing to sockets (specifically, streaming sockets).

I just want to know...

Question

What is limiting sendfile() from allowing the destination file descriptor to be something besides a socket (like a disk file, or a pipe)?

最佳回答

Fundamentally, the only thing limiting it is that "no-one s written the code yet".

However, I gather that the reason that no-ones written the code for those two cases you mention is that they both would require the data to be copied, which removes much of the advantage of using sendfile in the first place.

  • For a file-to-file sendfile, you d need a copy because otherwise the same page would have to be in the pagecache as both a clean page in the source file and a dirty page in the destination file. I don t think the pagecache is built to handle that case at the moment (though of course, this could be changed if there was sufficient motivation).

  • For a file-to-pipe sendfile, you need a copy regardless because the destination process needs to get a private, writeable copy of the data. Anyway, for most uses of this case we already have mmap.

问题回答

I seem to remember that it was a limitation introduced in early Linux 2.6 (2.4 didn t have the limitation).

Since 2.6.17 Linux has the splice() system call which is similar; more flexible, but slightly less efficient. Linus talked about re-implementing sendfile in terms of splice(). See http://kerneltrap.org/node/6505





相关问题
Write and test sub func

I m trying to write sub func for nachOS but when I combines it doesn t work. Don t know the reason. Details: In ../userprog/syscall.h Add : #define SC_Sub 11 int Sub(int a, int b); In ../test/ ....

Current Linux Kernel debugging techniques

A linux machine freezes few hours after booting and running software (including custom drivers). I m looking a method to debug such problem. Recently, there has been significant progress in Linux ...

How to debug driver load error?

I ve made a driver for Windows, compiled it and tried to start it via SC manager, but I get the system error from the SC manager API: ERROR_PROC_NOT_FOUND The specified procedure could not be found. ...

Configuring kernel

After create a new system call, how to update the kernel? I tried these lines, make-kpkg clean fakeroot make-kpkg -initrd -append-to-version=-custom kernel_image kernel_headers But Ubuntu asked me ...

What is the exact architecture/components of MinWin?

I ve always wanted a minimal windows NT build . Sounds like one s already there : MinWin. Can anyone tell me the exact design or architecture of MinWin and is it used in Windows 7 ? Windows Server ...

uniprocessor or multiprocessor

On unix, how could we know whether the system is multiprocessor or uniprocessor?

热门标签