在fork之前或之后在磁盘文件上调用mmap()有什么区别? [英] What is the difference between calling mmap() on a disk file before or after a fork?

查看:142
本文介绍了在fork之前或之后在磁盘文件上调用mmap()有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力了解mmap()如何与磁盘支持的文件一起使用,并且大多数情况下都可以使用它,但是我仍然有这个问题.

I've been working on understanding how mmap() works with disk-backed files, and am mostly getting it, but I still have this question.

在主进程派生一堆辅助子进程以及文件支持的只读m​​mapped db的情况下,mmap是否发生在分支之前的主进程或子进程中,这无关紧要?

In a situation with a master process that forks a bunch of worker child processes, and a file-backed read-only mmapped db, does it matter if the mmaps happen in the master process before the forks, or in the child processes?

我的理解是,如果它发生在派生之前在主进程中,那么在内存页表中,所有的映射 页面具有读取时设置为页面错误的设置,触发内核从磁盘(或页面高速缓存)加载页面,并且在派生一个孩子的页面后,将意味着该页面位于页面中. mmap已准备好供其他孩子阅读而不会引起重大页面错误.

My understanding is that if it happens in the master process before the fork, then in the memory page table, all of the mapped pages are given the setting to make a page fault when they are read, triggering the kernel to load the page from disk (or from the page cache), and after the fork one child's reading of a page will mean the page is there in the mmap ready for other children to read without causing a major page fault.

但是,如果在分叉之后的子进程中发生了mmap,其他工作人员子进程是否会受益于共享这些加载的页面-它们是否都使用相同的基础mmap来有效?还是每个工作人员的孩子都必须触发页面错误并自己加载每个页面?

But if the mmap happens in the child processes after the fork, do the other worker children get the benefit of sharing those loaded pages--are they all in effect using the same underlying mmap? Or does each worker child have to trigger a page fault and load each page themselves?

推荐答案

它对页面错误活动没有影响.文件的页面映射对于OS是全局的,它表示特定页面是否在RAM中.映射了文件的每个进程的PTE都指向此公共数据结构.尝试访问不在RAM中的页面的第一个进程将只有一个页面错误.这将触发它被读入,并且其他尝试访问同一页面的进程将能够使用该RAM.

It makes no difference to page fault activity. The page map of a file is global to the OS, it says whether a particular page is in RAM or not. The PTE of every process that has the file mapped points to this common data structure. There will only be a page fault for the first process that tries to access a page that isn't in RAM. That will trigger it to be read in, and other processes that try to access the same page will be able to use that RAM.

两种情况之间的区别是分配给映射块的虚拟地址是否相同.如果在派生之前调用mmap,该地址将被复制到所有子代中.如果在分叉后调用mmap,则它们可能会获得不同的地址.如果需要,在所有进程中使用相同的地址可使您将指针传递到进程之间的映射块中.您可以在块内的对象之间使用指针.如果它们并非都位于同一地址,则需要使用偏移量,并且所有进程都必须将偏移量添加到基址.

One difference between the two scenarios is whether the virtual addresses assigned to the mapped block are the same. If you call mmap before forking, the address will be copied in all the children. If you call mmap after forking, they could get different addresses. Using the same address in all processes allows you to pass pointers into the mapped block between the processes if you want. You can have pointers between objects within the block. If they're not all at the same address, you need to use offsets, and the processes will all have to add the offsets to the base address.

这篇关于在fork之前或之后在磁盘文件上调用mmap()有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆