VicBalta has asked for the wisdom of the Perl Monks concerning the following question:
Hi
I am writing a program that is Copying directories. But it has to do more the just two some times it does 6 or 8. So I wrote a program that forks a process and does them in pair of two's. So I have a for loop that forks off a process. But the problem that I am running into is that some times when one process finishes before the other process it ends that process. I am not sure how it is working with the fork. Im not sure if there is more than two forks at a time. And I always see the problem on the last copy. Maybee I think the problem might be that when it exits the for loop it exits the program and never waits for the other process to finish. Once it forks I want it to wait for both process's to finish before it goes on with the for loop. Is this the right thing to do? So that it will only have 2 process at a time. Or should I let it go on with the for loop? will it them make more than 2 process's? So if I do that can I put some thing after the for loop to wait for all process? I have tried putting in a waitpid. But it didn't not help the problem. Maybee I am setting it up wrong? So my questions on Forking are.
1) Is it best to do it this way with pairs of two or can you fork more process's off.
2) Can you set the limit of process to be forked off?
3) How can I make it wait for the process to finish. Can I make it wait for all process
# count is the number of directoires (always even) # $here is used to iterate through the directories # sub wanted goes throught directory and does the copying # @directory has the list of directoires for($num = 0; $num < ($count-1)/2;$num++) { $here = $num; $pid = fork(); if($pid == 0) { $here++; print "1 about to find $directory[$here] : $here\n"; find({wanted => \&wanted},"$directory[$here]"); } else { if($here!=0){$here = $here+2;} print "2 about to find $directory[$here] $here\n"; find({wanted => \&wanted},"$directory[$here]"); } do{ $kid = waitpid(-1,&WNOHANG); }until $kid == -1; } exit(0);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Help with waitpid and forking ?
by tachyon (Chancellor) on Sep 05, 2001 at 20:48 UTC | |
by VicBalta (Scribe) on Sep 05, 2001 at 22:01 UTC | |
by tachyon (Chancellor) on Sep 05, 2001 at 22:18 UTC | |
|
Re: Help with waitpid and forking ?
by VicBalta (Scribe) on Sep 05, 2001 at 18:26 UTC | |
by jlongino (Parson) on Sep 05, 2001 at 19:46 UTC |