开发者

C/C++ Linux: fastest write of a fixed chunk of memory to file (1 Hz)

On a Linux system, I have one 7MB chunk of memory of fixed size (no growth) whose contents I refresh in a real-time application.

I need to write this chunk of memory to disk (same file) once per second.

Taking into consideration modern (late 2011) CPUs and HDDs, what is the most efficient way to implement this functionality? I don't care if the write actually takes some time, but as this is a real-ti开发者_运维百科me app, I need to return to the running app ASAP.

What methodologies should I be trying?

My baseline is a standard baseline fopen(), binary fwrite(), fclose() cycle.

I have read that mmap() might be useful. Maybe asynchronous I/O? Are there other methodologies that I should be benchmarking? Off the top of your head, which methodology do you think would be fastest?


mmap(2) is the way to go. Just call msync(2) with MS_ASYNC when you want to write it.


I'd combine the two approaches mentionned: I'd use mmap to map the memory to the file, then set up a separate thread (with lower priority) to msync it every second. (In this case, the actual arguments to msync are not too important; you don't need MS_ASYNC, since you won't be blocking the main thread.)

Another alternative possibly worth trying would be asynchronous IO. It's not clear to me from my documentation what happens if you never recover the results, however, so you may need some sort of reaper code to prevent lost resources. (Asynchronous IO seems rather underspecified in Posix, which IMHO is a good reason to avoid it.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜