3

我在一个禁用交换并且禁用内存过量使用的系统上工作。

假设我的进程当前消耗 100 MB 内存,系统可用内存小于 100 MB。

如果我执行 fork() 会失败,因为内核也尝试为子进程分配 100 MB 吗?

我读过Linux在分叉时使用写时复制,因此子和父共享所有页面。所以我猜 fork 应该成功?

假设 fork 成功,假设我在调用 exec() 之前在子进程中有几行代码。因此,父子进程将继续共享文本段,除非子进程触及任何堆内存,否则内存使用量不应有任何变化。这个对吗 ?

Edit: One follow-up question: With swapping/overcommit disabled, can the cumulative VSS 
(Virtual Set Size) of all the processes be more than the available physical memory ?

I tried this out.  VSS of all the processes can be much more than that of the physical 
memory available.

pmap -x output from a process. 

total kB          132588   10744    7192

First number - Virtual Set Size
Second number - Resident Set Size
third number - dirty pages

RSS is < 10% of VSS. This is with swapping and overcommit disabled.

For every shared library loaded I see something like the following..

00007fae83f07000      32      24       0 r-x--  librt-2.12.so
00007fae83f0f000    2044       0       0 -----  librt-2.12.so
00007fae8410e000       8       8       8 rw---  librt-2.12.so

00007fae84312000     252     120       0 r-x--  libevent-2.0.so.5.0.1
00007fae84351000    2048       0       0 -----  libevent-2.0.so.5.0.1
00007fae84551000       8       8       8 rw---  libevent-2.0.so.5.0.1

I guess r-x segment is code and rw- is data. But there is a 2 MB segment 
that is not loaded. I see this 2 MB segment for every single shared library. 
My process loads a lot of shared libraries. That explains the huge difference 
between VSS & RSS.

Any idea what is that 2 MB segment per shared library ? 

When overcommit is disabled, if this process calls fork() will it fail when 
the free memory is less than VSS (132588 Kb) or RSS (10744) ?
4

1 回答 1

0

是的,如果完全禁用内存过量使用,那么fork将会失败。它将失败,因为如果程序希望写入它们,它可能会取消共享其所有页面,并且严格的过度使用模式不允许这样做。

您可以替换forkvfork,这将起作用。vfork设计为仅在与execfork/exec 模型结合使用以启动新进程时使用。

于 2012-10-16T22:16:05.463 回答