-2

Recently, I've come across this video that shows how to use mmap() with file io. However, I can't find the video of his that documents the function. I don't have an understanding of what it is, why it exists, nor how it relates to files.

Too much of the jargon is flying over my head to make sense of it. I had the same problem with sites like Wikipedia.

hippietrail
  • 15,848
  • 18
  • 99
  • 158
MasonMac
  • 137
  • 1
  • 8
  • 1
    Embarrassingly, I didn't understand it when I looked at it. Overall, I don't get how making another file to represent the OG file's contents would help. Wouldn't it be like copying files? – MasonMac May 02 '20 at 03:19
  • 1
    @MasonMac: "*Overall, I don't get how making another file to represent the OG file's contents would help.*" Where is this other file coming from? Memory mapping involves one file, not two. – Nicol Bolas May 02 '20 at 03:21
  • 1
    When you find an unknown function that you want to learn more about, just pasting its name into your favorite search engine is always a good start, possibly together with some concept. Searching for `memory mapping mmap` should give you plenty of information. – Some programmer dude May 02 '20 at 03:21
  • 1
    I'm struggling to understand them too, I'm afraid. It should be something easy to understand, but I'm just not getting it. @NicolBolas. Since mmap() stores the would-be memory in a file instead of RAM, this means that the file would be a copy of the input file. Thus, what's the use of it? I imagine I'm very, very wrong. – MasonMac May 02 '20 at 03:24
  • Instead of pasting into a search engine, I pasted into a search field. The one on this page, specifically. How comprehensible do you find [When should I use mmap for file access?](https://stackoverflow.com/questions/258091/when-should-i-use-mmap-for-file-access) – JaMiT May 02 '20 at 03:29
  • I found this question to answer what I meant: https://stackoverflow.com/questions/41480184/what-is-the-functionality-of-munmap-mmap I can't post it as an answer for some reason – MasonMac May 02 '20 at 03:38

1 Answers1

5

Files are arrays of bytes stored in a filesystem.

"Memory" in this case is an array of bytes stored in RAM.

Memory mapping is something that an operating system does. It means that some range of bytes in memory has some special meaning.

Memory mapped file is generally a file in the file system, which has been mapped by the operating system to some range of bytes in memory of a process. When the process writes to the memory of the process, the operating system takes care that the bytes are written to the file, and when the process reads from the memory, operating system takes care that the file is read.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • 1
    Thanks. I thought this would've been a straight-forward question. While the comments were well meaning, I already stated that I didn't understand the documentation. – MasonMac May 02 '20 at 04:04
  • 1
    @MasonMac: Telling us you don't understand the documentation doesn't say anything about *why* you don't understand the documentation. It was the first question that was asked of you in the comments, and you never answered it. – Nicol Bolas May 02 '20 at 04:55
  • 3
    How could I answer that when I don't understand any of it? – MasonMac May 02 '20 at 16:40
  • 1
    I'll try to fix that next time – MasonMac May 02 '20 at 16:46
  • Does mmap maps to device file memory I.e. block device memory or any memory. For example if I specify the address of some io memory mapped register after opening some block device file will it work? – user786 Jan 24 '21 at 17:09