Quoted from Wikipedia:
The Microsoft Windows spawn functions are inspired by Unix functions
fork and exec; however, as Windows does not support fork (at least in
the Win32 API; POSIX emulation environments such as Cygwin or SFU do),
the spawn function was supplied as a replacement for the fork-exec
combination.However, the spawn function, although it deals adequately with the
most common use cases, lacks the full power of fork-exec, since after
fork any process settings which will survive an exec may be changed.
However, in most cases, this deficiency can be made up for by using
the more low-level CreateProcess API.
I was wondering how to understand the reason that Windows spaw() is weaker than Unix fork-exec: “after fork any process settings which will survive an exec may be changed”?
When does the “change” to some process settings happen: between fork and exec or after exec?
In a child process, will the environment variables inherited from its parent process survice exec?
Are there some examples?
Thanks and regards!
The “change” will happen between the fork() and exec(). Quite a lot of the process state is inherited across an exec, see http://pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html . In particular, file descriptors are inherited (unless they have the close-on-exec bit set).
Typically one uses pipe() and the dup() family of calls to set up pipes and redirect file descriptors. E.g. connecting standard input and output of the child process to a pipe so that the parent process can communicate with the child.