Question

I need to create a large file (several gbytes) in /dev/shm. which is a RAMdisk on Linux machines. This must be as fast as possible. I'm currently using 5 separate POSIX threads in C to create sequential 100 mbyte segments, then I have another thread which concatenates them onto the main output file.

This is fast, but I want to go faster. Can I eliminate the concatenate thread? Is there any way to open a file, and have each thread write it's 100 mbyte segment to the correct place in the final output file?

Was it helpful?

Solution

The fastest way to do this is to just call truncate() or ftruncate() to expand the file to the size you want.

You can then mmap() the file into the process's memory space, and have each thread write its section into the mapped area.

OTHER TIPS

Simply open the file once for each thread, lseek to the position the thread is suppose to start at, and write normally. Or, you could use pwrite to specify the location of the write; in that case, a single open could be shared by all the threads since they don't need to use the current file position.

If you want to ensure in advance that there's space available and reserved for the entire final file, posix_fallocate should be able to take care of that for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top