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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |