Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: Re: When starting a process, at what point does "open()" return?

by esh (Pilgrim)
on Aug 18, 2003 at 06:03 UTC ( #284487=note: print w/replies, xml ) Need Help??

in reply to Re: When starting a process, at what point does "open()" return?
in thread When starting a process, at what point does "open()" return?

On UNIX systems, open("...|") ... returns after calling fork().

If this is true, how does open return an error if the program is not found, not executable, or has a bad shabang spec?

perl -e 'open F, "nosuchprogram |" or die "open: $!\n"; print "don +e\n";'
open: No such file or directory

I had assumed that this was because it was only returning after the exec() but I'll admit this seems to take a bit more inter-process communication.

-- Eric Hammond

Replies are listed 'Best First'.
Re: Re: Re: When starting a process, at what point does "open()" return?
by MarkM (Curate) on Aug 18, 2003 at 16:11 UTC

    I did some extra checking for you. I hate unexplained behaviour as well... :-)

    With open("program |") Perl opens two pipes on systems that provide fork() (UNIX). The first pipe is for capturing STDOUT, and remains open after execve(). The second pipe is for passing an errno value from the new child to the parent, and is automatically closed as part of execve(). If execve() fails, the value of errno is written in native binary format over the second pipe. The caller monitors this second pipe, and waits until the second pipe closes before it succeeds.

    I wasn't able to quickly determine when this code was introduced into Perl. It does make things convenient. :-)


      system(), backticks and pipe open now reflect exec failure
      On Unix and similar platforms, system(), qx() and open(FOO, ``cmd |'') etc., are implemented via fork() and exec(). When the underlying exec() fails, earlier versions did not report the error properly, since the exec() happened to be in a different process. The child process now communicates with the parent about the error in launching the external command, which allows these constructs to return with their usual error value and set $!.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2021-06-15 12:58 GMT
Find Nodes?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)

    Results (72 votes). Check out past polls.