Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mmap File-backed mapping vs Anonymous mapping in Linux [closed]

  • what is the main difference between File-backed mapping & Anonymous mapping.
  • How can we choose between File-backed mapping or Anonymous
    mapping, when we need an IPC between processes.
  • What is the advantage,disadvantage of using these.?
like image 316
user1762571 Avatar asked Jan 08 '17 04:01

user1762571


People also ask

What is anonymous mapping in Linux?

The anonymous memory or anonymous mappings represent memory that is not backed by a filesystem. Such mappings are implicitly created for program's stack and heap or by explicit calls to mmap(2) system call. Usually, the anonymous mappings only define virtual memory areas that the program is allowed to access.

How does mmap work in Linux?

mmap works by manipulating your process's page table, a data structure your CPU uses to map address spaces. The CPU will translate "virtual" addresses to "physical" ones, and does so according to the page table set up by your kernel. When you access the mapped memory for the first time, your CPU generates a page fault.

Why mmap is used in Linux?

mmap allows all those processes to share the same physical memory pages, saving a lot of memory. mmap also allows the operating system to optimize paging operations.

What is mmap in Linux with example?

The mmap() function asks to map length bytes starting at offset offset from the file (or other object) specified by the file descriptor fd into memory, preferably at address start. This latter address is a hint only, and is usually specified as 0. The actual place where the object is mapped is returned by mmap().


1 Answers

mmap() system call allows you to go for either file-backed mapping or anonymous mapping.

void *mmap(void *addr, size_t lengthint " prot ", int " flags ,int fd, off_t offset)

File-backed mapping- In linux , there exists a file /dev/zero which is an infinite source of 0 bytes. You just open this file, and pass its descriptor to the mmap() call with appropriate flag, i.e., MAP_SHARED if you want the memory to be shared by other process or MAP_PRIVATE if you don't want sharing.

Ex-

     .
     .
if ((fd = open("/dev/zero", O_RDWR)) < 0)
printf("open error");
if ((area = mmap(0, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED,fd, 0)) == MAP_FAILED)
  {
      printf("Error in memory mapping");
      exit(1);
  }
close(fd); //close the file because memory is mapped
  //create child process 
    .
    .

Quoting the man-page of mmap() :-

The contents of a file mapping (as opposed to an anonymous mapping; see MAP_ANONYMOUS below), are initialized using length bytes starting at offset offset in the file (or other object) referred to by the file descriptor fd. offset must be a multiple of the page size as returned by sysconf(_SC_PAGE_SIZE).

In our case, it has been initialized with zeroes(0s).

Quoting the text from the book Advanced Programming in the UNIX Environment by W. Richard Stevens, Stephen A. Rago II Edition

The advantage of using /dev/zero in the manner that we've shown is that an actual file need not exist before we call mmap to create the mapped region. Mapping /dev/zero automatically creates a mapped region of the specified size. The disadvantage of this technique is that it works only between related processes. With related processes, however, it is probably simpler and more efficient to use threads (Chapters 11 and 12). Note that regardless of which technique is used, we still need to synchronize access to the shared data

After the call to mmap() succeeds, we create a child process which will be able to see the writes to the mapped region(as we specified MAP_SHARED flag).

Anonymous mapping - The similar thing that we did above can be done using anonymous mapping.For anonymous mapping, we specify the MAP_ANON flag to mmap and specify the file descriptor as -1. The resulting region is anonymous (since it's not associated with a pathname through a file descriptor) and creates a memory region that can be shared with descendant processes. The advantage is that we don't need any file for mapping the memory, the overhead of opening and closing file is also avoided.

if ((area = mmap(0, SIZE, PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
   printf("Error in anonymous memory mapping");

So, these file-backed mapping and anonymous mapping necessarily work only with related processes.

If you need this between unrelated processes, then you probably need to create named shared memory by using shm_open() and then you can pass the returned file descriptor to mmap().

like image 81
Shubham_K Avatar answered Sep 28 '22 13:09

Shubham_K