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

On a Linux system, I have one piece of memory of 7 MB fixed size (no growth), the contents of which I update in a real-time application.

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

Considering modern (at the end of 2011) processors and hard drives, what is the most efficient way to implement this functionality? I don't care if the recording actually takes some time, but since this is a real-time application, I need to return to the current application as soon as possible.

What methodologies should I try?

My baseline is standard base fopen (), binary loop fwrite (), fclose ().

I read that mmap () can be useful. Maybe asynchronous I / O? Are there other methodologies that I have to carry out for comparison? From the top of your head, which methodology do you think will be the fastest?

+7
source share
2 answers

mmap(2) is the path. Just call msync(2) using MS_ASYNC when you want to write it.

+9
source

I would combine the two mentioned approaches: I would use mmap to map the memory to a file, then configure a separate thread (with a lower priority) to msync every second. (In this case, the actual msync arguments msync not too important; you do not need MS_ASYNC , since you will not block the main thread.)

Another alternative might be worth a try asynchronous IO. It is not clear to me from my documentation what will happen if you never however restore the results, so you may need some kind of reaper code to prevent loss of resources. (Asynchronous IO seems rather vague in Posix that IMHO is a good reason to avoid this.)

+1
source

All Articles