Are sharing a memory-mapped file and sharing a memory region implemented based on each other?

6,582

Solution 1

Shared memory can be backed by a regular file, a block device, or swap. It depends on how the memory region was created.

When multiple processes are using the same shared memory region, their individual virtual addresses will be pointing to the same physical address. Writes by one process become visible to others directly, without needing to pass through the disk file (if any) that the memory is attached to.

If there is a file behind the shared memory, the kernel occasionally syncs changed pages from RAM to the file. The processes using the memory normally don't need to know when this happens, but if necessary, they can call msync to make it happen sooner.

If there is no file behind the shared memory, the kernel may move pages to swap when it needs to free some RAM, just like it does with non-shared process memory. When they're swapped back in, they get a new physical address which is immediately available to all processes that have mapped the shared memory.

There's one more thing that confused me the first time I looked at it: if you map a file with mmap, you have to use MAP_SHARED if you want to make changes and have them saved back to the file, even if there's only one process involved. At first I thought MAP_SHARED was the wrong name for this functionality, but on further reflection, you're "sharing" your modifications with other processes that access the file with read, or processes that will come along later and mmap it. So it makes sense, kind of.

Solution 2

Are sharing a memory-mapped file and sharing a memory region implemented based on each other?

Pretty much, at the end of the day /dev/shm is just a ramdisk.

Does a "memory-mapped file" reside on disk or main memory?

Yes!

The portions of the file that are accessed are copied (by the swapper) into buffers - which is RAM.

Any writes to that memory will be immediately visible in the file, but will not be written to physical disk until the buffer is synced or flushed.

Share:
6,582

Related videos on Youtube

Tim
Author by

Tim

Elitists are oppressive, anti-intellectual, ultra-conservative, and cancerous to the society, environment, and humanity. Please help make Stack Exchange a better place. Expose elite supremacy, elitist brutality, and moderation injustice to https://stackoverflow.com/contact (complicit community managers), in comments, to meta, outside Stack Exchange, and by legal actions. Push back and don't let them normalize their behaviors. Changes always happen from the bottom up. Thank you very much! Just a curious self learner. Almost always upvote replies. Thanks for enlightenment! Meanwhile, Corruption and abuses have been rampantly coming from elitists. Supportive comments have been removed and attacks are kept to control the direction of discourse. Outright vicious comments have been removed only to conceal atrocities. Systematic discrimination has been made into policies. Countless users have been harassed, persecuted, and suffocated. Q&A sites are for everyone to learn and grow, not for elitists to indulge abusive oppression, and cover up for each other. https://softwareengineering.stackexchange.com/posts/419086/revisions https://math.meta.stackexchange.com/q/32539/ (https://i.stack.imgur.com/4knYh.png) and https://math.meta.stackexchange.com/q/32548/ (https://i.stack.imgur.com/9gaZ2.png) https://meta.stackexchange.com/posts/353417/timeline (The moderators defended continuous harassment comments showing no reading and understanding of my post) https://cs.stackexchange.com/posts/125651/timeline (a PLT academic had trouble with the books I am reading and disparaged my self learning posts, and a moderator with long abusive history added more insults.) https://stackoverflow.com/posts/61679659/revisions (homework libels) Much more that have happened.

Updated on September 18, 2022

Comments

  • Tim
    Tim over 1 year

    Are sharing a memory-mapped file and sharing a memory region implemented based on each other? The following two quotes seem to say so, and seem a chicken-egg problem to me.

    Operating System Concepts introduces sharing a memory-mapped file in the following. Do the multiple processes share the same file by sharing the same physical memory region holding the content of the file?

    Multiple processes may be allowed to map the same file concurrently, to allow sharing of data. Writes by any of the processes modify the data in virtual memory and can be seen by all others that map the same section of the file. Given our earlier discussions of virtual memory, it should be clear how the sharing of memory-mapped sections of memory is implemented: the virtual memory map of each sharing process points to the same page of physical memory—the page that holds a copy of the disk block. This memory sharing is illustrated in Figure 9.22.

    enter image description here

    It also introduces shared memory in the following.

    • Do multiple processes share a memory region by sharing a memory-mapped file?

    • Does a "memory-mapped file" reside on disk or main memory? I think it is on the disk, but "The memory-mapped file serves as the region of shared memory between the communicating processes" seems to mean that it resides in main memory.

    Quite often, shared memory is in fact implemented by memory mapping files. Under this scenario, processes can communicate using shared memory by having the communicating processes memory-map the same file into their virtual address spaces. The memory-mapped file serves as the region of shared memory between the communicating processes (Figure 9.23).

    enter image description here

    Thanks.

    • Jasen
      Jasen over 5 years
      The Linux Programmers Guide has a good discussion of this and some demonstration code
  • Tim
    Tim over 5 years
    Thanks. (1) how do you understand this example of the chicken egg paradox? (2) "Any writes to that memory will be immediately visible in the file," do you mean visible to other process?