Hi,
I want to create a process and get a (or 2) FILE* to fwrite() to its stdin and fread() its stdout, but cross platform.
I found a windows only way:
http://support.microsoft.com/kb/190351/en-us
But all unix version seem to rely on fork(), which is total overkill if you fork() a big process (or am I wrong? - I'm a windows
guy).
So, is there a way to get a read/write handle to a process without fork?
Thank you,
For POSIX systems, your only real option is fork. (There's a couple
more obscure ones which you don't want to use, or which are basically
wrappers on top of fork, such as popen and posix_spawn.)
IMHO, this is a broken state of affairs for a variety of reasons.
My biggest annoyance is that it's incredibly hard to ensure no
resource is "leaked" across a fork in a quick and portable way, and
the extra memory required for forking. Almost the entire point of
processes is fault isolation. The entire OS and the hardware chip
itself is built with this goal in mind, e.g. kernel mode and user
mode. The default usage of fork makes it incredibly easy, standard
practice even, to leak resources across a fork, which is quite
contradictory. I think the most notable thing is that it's a huge
gaping security hole when files, mmaps, etc., can be leaked from one
process to another so easily.
As mentioned else-thread, most windows / mac / unix systems implement
fork so that its virtual pages are copy on write, and this helps a
lot, but I would mention that still has some negative side effects.
Either you have overcommit on, in which case if your system is loaded,
random programs will be killed with great prejudice (aka very bad), or
you have overcommit off, which means that while a fork will not copy
all of the contents of the virtual pages, it will commit those virtual
pages, which can fail if your system is loaded and near the commit
limit. At least with overcommit off, the process gets an error when it
tries to get a new virtual page as opposed to a random process just
being instantly arbitrarily killed. I suppose the problem with
overcommit off can be solved with a larger swap partition size if you
know the size of your workload, so I guess that it's not that big of a
deal. I just wanted to make it clear.
To finish this rant, I don't mind fork existing. I just want another
primitive which spawns a process from an executable file in a clean
environment, so I can choose to be easily leak-free if so desired. I'd
prefer this new spawn would be the default for developers as well. In
the end, what fork has over this new method is simply ease of use.
With my spawn primitive, you would have to put the code for the child
process which sets up the environment into its own cpp file and
compile it into its own executable, whereas with the fork you can put
the code inline. Saves some keystrokes only.
Off topic, I know. Sorry. Rant done.
PS: Is windows broken in the same way with regards to file handles? I
can't parse the msdn website very well in this regard, but from what I
can gather, /I think that/ windows has the same resource leak problem
as unix style forks - all file handles are inherited by default into
the child, and there's no easy way to say "Only inherit these specific
X file handles".