Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^4: Perl Pipeline exception handling (inherit handle)

by tye (Sage)
on Jul 28, 2016 at 19:33 UTC ( [id://1168753]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Perl Pipeline exception handling
in thread Perl Pipeline exception handling

Inheritance of file handles in child processes is very similar between Unix and Windows. All of the same concepts are there in both places, but they have different names and are manipulated using different functions. Perl layers the Unix-style concepts over the top of the Windows equivalents. If you want to have a Perl child process on Windows inherit a file handle, it requires you to use the Windows concepts and then rewrap them in the Unix equivalents so Perl can use them (because Perl only does that for you automatically for STDIN, STDOUT, and STDERR). Win32API::File (bundled with Perl) provides all of the tools you need to do this.

In Unix, to share a file handle with a child process that is not running the same script, that is, when you did fork(2) followed by exec (perhaps indirectly, such as via system), you have to:

  1. mark the file descriptor as "do not close on exec" (there are 2 ways to do that)
  2. fork the child and exec the new program in the child
  3. inform the child of the file descriptor (a small integer)
  4. have the child re-open the file descriptor (by passing something like "<&=$fd" to open)

In Windows, the steps are almost the same. Instead of a file descriptor, you deal with a native Windows "file handle" (a pointer, which is just a large integer), which you must not confuse with a Perl "file handle". The Windows steps are:

  1. mark the native handle so it will be inherited [using HANDLE_FLAG_INHERIT]
  2. spawn the child (don't use Perl's fork as that doesn't spawn a child, it creates a thread and a new interpreter instance)
  3. inform the child of the native handle, a large integer [requires GetOsFHandle()]
  4. have the child re-open a Perl file handle that just wraps the given native handle [using OsFHandleOpen()]

I've done this successfully without much trouble before. I thought I'd posted code for such here but my search did not turn such up. I might rewrite that later and post it.

See also: Re^2: Passing a File Descriptor to a New Process on Windows (Win32API::File)

- tye        

Replies are listed 'Best First'.
Re^5: Perl Pipeline exception handling (inherit handle)
by BrowserUk (Patriarch) on Jul 29, 2016 at 13:14 UTC

    Are you actually recommending this to the OP as a solution? (Bonus points for a 1-word answer.)


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.

      No, not as a first choice. You covered the easier solutions. I added this for completeness or even just academic interest. There are more complex situations where this approach can be useful.

      - tye        

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1168753]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (7)
As of 2024-03-28 22:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found