简体   繁体   中英

Linux: system() from a memory-intensive process without forking

I have a large C++ process (35+ GB RAM usage) on a 60 GB Linux machine which needs to system() to perform some functionality. However, because system() internally forks and execs the new process, it requires virtual memory space which is double the original process's RAM.

I understand that I can use sysctl to always enable overcommitting memory, thus allowing my process to fork. However, is there an alternative to system() which can execute a new process without requiring so much virtual memory?

If your implementation includes posix_spawn(), this is a more viable alternative.

#include <spawn.h>

http://fixunix.com/unix/84486-difference-between-spawn-fork.html

Create a second, smaller process that merely receives commands and peforms them.

Choose your favorite IPC (socket, pipe, signal, msg queue, pick your favorite IPC), and tickle that process from your huge process whenever you need to perform some small system action.

We ended up going with Subprocess from the FB folly library, since we had recently added folly as a dependency anyway. They use vfork() under the hood, which forks the process without making a copy of the process's pages and freezes the parent process until the child exec()'s.

vfork() has many potential pitfalls and seems to be widely considered deprecated/dangerous to use. For example, it requires that the forked process do nothing other than exec; because the process's pages are shared, if any memory is modified, they can modify and corrupt the parent's state. Having it abstracted in a carefully-programmed library should make it much easier to avoid mistakes when we simply want to launch a process without memory overhead.

Header for the folly Subprocess library: https://github.com/facebook/folly/blob/master/folly/Subprocess.h

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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