简体   繁体   English

根据可用空闲内存控制派生多少子进程

[英]controlling how many child process forked based on available free memory

I am trying to take advantage of COW and fork as many child processes as the Linux system memory allows. 我试图利用COW并在Linux系统内存允许的范围内派生尽可能多的子进程。 I would start the parent process until the forking part (at which point the process is using a certain amount of memory), then fork one child at a time until fork returns ENOMEM error. 我将启动父进程,直到派生部分(此时该进程正在使用一定量的内存),然后一次派生一个孩子,直到派生返回ENOMEM错误。 In that case, I would wait for any child to finish before forking a new one. 在这种情况下,我会等所有孩子完成后再分叉一个新孩子。 Child processes will not allocate any new memory but just work. 子进程不会分配任何新的内存,而是可以正常工作。 But this does not work, all my processes were killed by the Linux system without any memory error. 但这是行不通的,我的所有进程都被Linux系统杀死,没有任何内存错误。

What is the best way to do it? 最好的方法是什么?

This is not possible, by the very definition of COW. 根据COW的定义,这是不可能的。

Your processes may not be allocating memory explicitly, but they certainly do it implicitly. 您的进程可能没有显式分配内存,但是它们肯定是隐式分配的。 When a process tries to modify a page marked copy-on-write, the OS needs to suspend the operation and allocate a new page. 当进程尝试修改标记为写时复制的页面时,操作系统需要暂停操作并分配新页面。 This allocation may fail, and there's no place in your program to return an error to. 此分配可能会失败,并且程序中没有位置向其返回错误。 The only possible action is to kill the process. 唯一可能的措施是终止进程。

The best you can do is estimate how much memory your program will need at its peak, and combine this info with the free memory info (eg from /proc/meminfo). 您能做的最好的事情就是估计您的程序在高峰期将需要多少内存,并将此信息与空闲内存信息结合起来(例如,从/ proc / meminfo获取)。

The radical solution of disabling COW altogether should work too, but AFAIK Linux doesn't have this option. 完全禁用COW的根本解决方案也应该可行,但是AFAIK Linux没有此选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM