I work with large files and as a result of development I had to copy large files (about 10 gigabytes) in C code.
In this regard, the question arose: how effective is the combination of map()+write() (an existing file is mapped in memory and write done to an existing file)?
Will conditional Windows/macOS/Linux/*BSD handle this in a special way to optimize copying data from one file to another
Sample code that is present in my project (assume code ran on unix-like OS and all syscalls succeed, input
and output
are some regular files):
int main() {
struct stat sb;
int fdsrc, fddst;
void *p;
fdsrc = open("input", O_RDONLY);
fddst = open("output", O_WRONLY);
fstat(fdsrc, &sb);
p = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, fdsrc, 0);
write(fddst, p, sb.st_size);
return 0;
}
Will it be as fast as copy_file_range(2)
or kinda? Is there some code in Linux/*BSD sources which handles this case?
P.S.: On linux write(2)
as i know never writes more than INT_MAX bytes in one call, so in my project i use full_write() wrapper which calls write(2)
until all data written or one of the syscalls fails.
You need to sign in to view this answers
Leave feedback about this