Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Though I noticed this description of $SYSTEM_FD_MAX ($^F) in perlvar:

The maximum system file descriptor, ordinarily 2. System file descriptors are passed to exec()ed processes, while higher file descriptors are not.
I could not find much more describing this behaviour in the standard Perl docs. I further experimented with two programs topen.pl and topensys.pl, as shown below:
# topen.pl use strict; use warnings; open(my $fh, '<', "zz.tmp") or die "open zz.tmp: $!"; my $fdnew = fileno($fh); print "fdnew=$fdnew\n"; # topensys.pl use strict; use warnings; use Fcntl; $|=1; # $^F = 3; # uncomment this line to inherit $fh_g my $tmpfile_g = 'tmpglob.tmp'; open(my $fh_g, '<', $tmpfile_g) or die "error: open '$tmpfile_g': $!"; if (0) { # Fiddle with close-on-exec, works on Unix, not Windows. my $val = fcntl($fh_g, F_GETFD, 0) or die "error: fcntl: $!"; # $val |= FD_CLOEXEC; # turn on $val &= ~FD_CLOEXEC; # turn off fcntl($fh_g, F_SETFD, $val) or die "error: fcntl: $!"; } my $rc = system("$^X topen.pl"); sleep(30); print "rc=$rc\n";

To run this test, first create empty tmpglob.tmp and zz.tmp files, then run topensys.pl, which forks topen.pl. The sleep is there so I could run OS file handle utilities to peek at what file handles each process had open.

This all worked as advertised on Unix, at least that's what I saw, both via the fd reported by topen.pl and confirmed by the lsof utility.

However, on Windows, it seems that the underlying OS file handle for the tmpglob.tmp file is being inherited by topen.pl; that's what sysinternals handle utility told me.

Before rushing off and perl-bugging this, I thought I better discuss it first in case I've missed something.

Since Windows does not support FD_CLOEXEC, you can't use Fcntl to work around this annoyance. Though Windows can support this behaviour (for example, via CRT O_NOINHERIT flag), I don't know of any way to get at it from pure Perl code. I was thinking a new close_on_exec() method for IO::Handle might be nice. Thoughts?


In reply to Comparing close-on-exec behaviour between Unix and Windows by eyepopslikeamosquito

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2024-04-18 15:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found