title: Linux fork function compared to Windows' CreateProcess - what gets copied?
body: <p>I am porting Windows application to Linux. I use <code>CreateProcess</code> on Windows to run child processes and redirect all standard streams (in, out, error). Streams redirect is critical, main process sends data to children and receives theirs output and error messages. Main process is very big one with a lot of memory and threads, and child processes are small ones. On Linux I see that <code>fork</code> function has similar functionality as <code>CreateProcess</code> on Windows. However, manual says that <code>fork</code> "creates parent process copy", including code, data and stack. Does it mean that if I create copy of a huge process that uses 1 GB of memory just to run a very simple command line tool that uses 1 MB of memory itself, I will need to fist duplicate 1 GB of memory with <code>fork</code>, and then replace this 1 GB with 1 MB process? So, if I have 100 threads it will be required to have 100 GB of memory to run 100 processes that need just 100 MB of memory to run? Also what about other threads in parent process that "don't know" about <code>fork</code> execution, what will they do? What <code>fork</code> function does "under the hood" and is it really effective way to create a lot of small child processes from huge parent?</p>