G'day wonderG,

Welcome to the Monastery.

"The above if block if(wait() == -1) can not be executed..."

The short answer is because "wait() == -1" is FALSE.

"...could you help to check if there is any errors in my code"

You've presented us with incomplete code. This just raises a lot of questions and means we have to engage in guesswork if we were to attempt an answer.

Please cut down your code to a minimum, that we can run, and reproduces the problem; as described in "Short, Self-Contained, Correct Example". Also, follow the guidelines in "How do I post a question effectively?".

It would also be preferable unless you do use potentially confusing constructs like "unless (...) {...} else {...}". Confused? Try s/unless you do/if you don't/. :-)

Here's some very basic code that uses wait() and doesn't have a problem.

#!/usr/bin/env perl use strict; use warnings; my $pid = fork(); if (defined $pid) { if ($pid) { print "Parent: $$; Child: $pid\n"; my $wait = wait(); print "Parent waited on Child: $wait\n"; } else { print "Child: $$\n"; exit; } } else { warn 'Fork failed!'; }

Sample output:

Parent: 553; Child: 554 Child: 554 Parent waited on Child: 554

This may highlight your problem and allow you to fix it. If not, add small amounts of code until your problem resurfaces; then post that and ask for our help.

See also waitpid().

— Ken


In reply to Re: Multiprocess - child process cannot be finished successfully by kcott
in thread Multiprocess - child process cannot be finished successfully by wonderG

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.