Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

mysterious fork() failure

by Marcello (Hermit)
on Jan 05, 2002 at 00:35 UTC ( #136375=perlquestion: print w/replies, xml ) Need Help??
Marcello has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have the following code:
# create new directory here if (($pid = fork()) == 0) { # remove directory here } if (!defined($pid)) { # log fork() failure here # remove directory here }
As I see it, the newly created directory should always be removed. But sometimes the directory is mysteriously still there, without any fork() errors in my logfile.

BTW, I also write an entry in the logfile is the removal of the directory fails, this is also not the case, so it looks like it never reaches the code in the child process.

Can it be that although $pid is defined, the fork() fails? And how can I catch still situation in the above code?

Any input is highly appreciated

Replies are listed 'Best First'.
Re: mysterious fork() failure
by dws (Chancellor) on Jan 05, 2002 at 01:11 UTC
    # create new directory here if (($pid = fork()) == 0) { # remove directory here } if (!defined($pid)) { # log fork() failure here # remove directory here }
    You need to reverse your tests, and check for undef'ness of $pid first. For enlightenment as to why, ponder on   print undef == 0 ? "true!\n" : "false\n";
      So,
      if (!defined($pid = fork())) { # parent process, fork() failure } elsif ($pid == 0) { # child process, successful fork() } elsif ($justSomeVar == 1) { # parent process, successful fork() } else { # parent process, successful fork() }
      is the way to go?
Re: mysterious fork() failure
by Zaxo (Archbishop) on Jan 05, 2002 at 03:51 UTC

    The possible reasons for this are in the code you don't show. When you fork, you cannot know whether the child or parent runs first. If the parent runs first, and exits immediately, the child will be killed by the system's init process before it runs. Be sure to call wait() before exiting the parent. You should call exit() at the end of the child's code.

    # create new directory here if ( !defined( $pid = fork() ) ) { # log fork() failure here # remove directory here } elsif ($pid == 0) { # remove directory here exit 0; } wait if $pid;

    Update: Fixed pid logic according to dws's prescription

    After Compline,
    Zaxo

      "...the child will be killed by the system's init process before it runs." is not true in Unix systems.

      If the parent exits/dies before the child, init will become its parent and wait() for it.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://136375]
Approved by root
help
Chatterbox?
[ELISHEVA]: I may have to resort to SOPW - but was hoping that this would be something simple
[erix]: I'd just remove the BOM, it is pretty simple
[ELISHEVA]: Simple yes. and I did consider that. but this isn't one off . An important data source that I don't control is generating bom prefixed utf8 files and I'd rather not have to be munging files every few months.
[erix]: on teh other hand a SOPW is pretty much garanteed to get an answer from tux (and probably the module fixed)
[ELISHEVA]: plus it bugs me that something that *should* be simple, *should* work- unicode and noms aren't exactly the new kids on the block
[ELISHEVA]: well then since the obvious possible mistakes on my part have been ruled out, SOPW it is.
[ELISHEVA]: the data source, or one of them, is the OECD - they provide a *lot* of data that ought to be easily available to perl programmers.
[erix]: it might be cunning to mention the module in the title... :)
[ELISHEVA]: fancy that - a title that actually describes the problem :-)
[ELISHEVA]: but actually thanks for the reminder

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (8)
As of 2017-05-28 20:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?