叉()&存储器分配行为(fork () & memory allocation behavior)

2019-09-24 05:24发布

我的系统中,交换是残疾人和内存过量使用被禁用的工作。

比方说我的过程目前占用100 MB内存,系统可用内存小于100 MB。

如果我做一个叉()将它失败,因为内核尝试分配100 MB子进程呢?

我已阅读,Linux使用写入时复制分叉的时候,让孩子与父母共享的所有网页。 所以我想应该叉成功?

假设叉成功,可以说我有子进程中的几行代码调用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) ?

Answer 1:

是的,如果内存过完全禁止随后fork将失败。 这将失败,因为该计划可能会停止共享其所有页是否想写信给他们,并严格过量使用模式将不允许这样做。

你可以取代forkvfork和会工作。 vfork被设计为当与组合以仅用于exec在叉/ EXEC模型启动一个新进程。



文章来源: fork () & memory allocation behavior