Why use mmap over a file?

Why use mmap

as opposed to reading from a stream in chunks into a byte array? I guess I never thought about using it mmap

before.

mmap (2)
fread (3)

// Open the file

// Read from the file
uint8_t my_buffer[MY_BUFFER_SIZE];
size_t bytes_read;
bytes_read = fread(my_buffer, 1, sizeof(my_buffer), input_file);
if (MY_BUFFER_SIZE != bytes_read) {
  fprintf(stderr, "File read failed: %s\n", filepath);
  exit(1);
}

// Close the file

// Do something with the buffer

      

+3


source to share


1 answer


There are advantages to mapping a file rather than reading it as a stream:

  • If you intend to perform random access to various widely distributed regions of a file, matching can mean that only the pages you are accessing should be actually read, keeping your code simple.

  • If multiple applications will access the same file, the mapping means that it will only be read once in memory, as opposed to when each application loads [part of] the file into its own buffers.

  • If a file doesn't fit in memory or takes up a lot of memory, displaying it can provide the illusion that it fits and simplifies your programming logic, allowing the operating system to decide how to manage the file's rotating bits in and out of physical memory.

  • If the content of the file changes, you can automatically see the new content. (This can be a questionable benefit.)



There are downsides to file matching:

  • If you only need sequential access to a file, or it is small, or you only need access to a small portion of it, the overhead of setting up memory mappings and subsequent page faults to actually trigger the content may be less efficient than just reading the file.

  • If an I / O error occurs while reading the file, your application will most likely be killed in place instead of receiving a syscall error, to which your application can gracefully respond. (Technically, you can catch SIGBUS

    in the first case, but recovering properly from this kind of thing isn't easy.)

  • If you are not using 64-bit architecture and the file is very large, there may not be enough address space to map it.

  • mmap()

    less portable than read()

    (from fread()

    , as you suggest).

  • mmap()

    will only work on regular files (on some filesystems) and on some block devices.

+9


source







All Articles