2

I'm kind of confused by mmap.

Well, I know that when we malloc a big size of memory, we will invoke the function mmap, which will allocate an area in memory. In this case, mmap just allocate some memory for some process.

However, I've heard that mmap is a kind of technique, which allows us to map a file, which is located on the hard drive, to the memory so that we can have a better performance comparing with normal IO (read & write).

For me, the two things above are totally two independent stories: one is about allocation of memory, the other is about reading and writing files with a better way.

But why are both of them called mmap? Is it just a coincidence or they are actually the same technique?

1 Answer 1

1

mmap provides a way to map pages of memory. In Linux (among others), those pages of memory can have different backing devices: notably, files, and nothing at all (for anonymous mappings, MAP_ANONYMOUS), or rather a swap device or file.

While the use cases are completely different, there is a common theme: allocating address space for a process, and defining how pages will be mapped there.

There are other use cases for mmap, in particular shared memory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.