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);

In reply to Help with waitpid and forking ? by VicBalta

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.