Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c

linux

io

memory

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-time 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?

like image 772
kfmfe04 Avatar asked Oct 12 '11 07:10

kfmfe04


Video Answer


2 Answers

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

like image 150
Ignacio Vazquez-Abrams Avatar answered Sep 22 '22 10:09

Ignacio Vazquez-Abrams


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.)

like image 24
James Kanze Avatar answered Sep 22 '22 10:09

James Kanze