I have a memory region which contains data that I would like to send over ethernet to the other client. To increase the throughput, currently I am fiddling with sendfile API instead of the classic send/write API. But as far as I understand, the sendfile API requires a file descriptor for input, but what I have is only raw memory data. So what should I do in order to use the sendfile API?
Asked
Active
Viewed 361 times
1
-
1Create a file? The name tells you what it does; if you don't have a file, `sendfile()` doesn't apply, does it? – Jonathan Leffler Sep 03 '14 at 02:49
-
1I see, previously I thought there is a way to open the particular memory region and pass the file descriptor to sendfile. So in your opinion, which one is faster? 1. mmap the data region, then use send API to read from the memory repetitively until all data being sent. 2. Mount a ramdisk, create a raw file by using the data in memory, then use sendfile API to send over. – czteoh Sep 03 '14 at 03:29
-
OK; let me back off — there are some very complex interfaces around, and `sendfile()` may be one of them. But, a priori, and based on the name, you need a file. It isn't a POSIX standard function; it is a Linux extension. – Jonathan Leffler Sep 03 '14 at 03:39
2 Answers
3
The primary benefit of sendfile()
is that it allows you to avoid the overhead of having to first read()
data from a file descriptor into memory before you can send()
it. If the data you want to send is already in memory, sendfile()
is not needed. Using weird workarounds to move the data into a file (like mmap()
ing it) will only reduce performance.
-1
If you running linux you can look at splice

rouzier
- 1,160
- 8
- 19
-
['*splice()* moves data between two file descriptors ... where one of the descriptors must refer to a pipe'](http://linux.die.net/man/2/splice). Neither a file descriptor nor a pipe is present here. – user207421 Feb 22 '15 at 23:07