Reputation: 35
I am trying to take advantage of COW and fork as many child processes as the Linux system memory allows. 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. 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.
What is the best way to do it?
Upvotes: 0
Views: 177
Reputation: 120021
This is not possible, by the very definition of 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 (e.g. from /proc/meminfo).
The radical solution of disabling COW altogether should work too, but AFAIK Linux doesn't have this option.
Upvotes: 0