|Don't ask to ask, just ask|
Passing a File Descriptor to a New Process on Windowsby hardburn (Abbot)
|on Apr 23, 2014 at 13:28 UTC||Need Help??|
hardburn has asked for the
wisdom of the Perl Monks concerning the following question:
I have a script that opens a filehandle and then calls fork() and exec() to run a separate program for doing the reading. The trouble is that by default, Perl sets the close-on-exec flags on new filehandles. This can be changed with Fcntl:
This doesn't work on Strawberry Perl on Windows, though, due to the F_GETFD macro being missing.
At Sebastian Riedel's suggestion, I tried local $^F = 10; instead. This makes Perl consider up to 10 open filehandles to be "system filehandles" which do not get the close-on-exec flag set (when using this, remember that you'll already have three filehandles open: STDIN, STDOUT, and STDERR).
This worked on Linux and Cygwin, but Strawberry Perl still failed to reopen the filedescriptor with "Bad file descriptor". This is the error you usually get when the close-on-exec flag is still set.
Strawberry Perl is built with threads:
Since fork() would be emulated with threads in this case, perhaps that accounts for the difference with Cygwin?
Full example code below. The first one (fd_pass.pl) is passed a file to open and then forks off to exec the second one (fd_get.pl).
"There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.