In your code, if the ps command returns the processes in different order, you might duplicate or miss childs. Do a sort { $a <=> $b } ... to insure that the array is in a consistent order after different calls.

Note that if a child terminates between calls to FETCH, you're bound to miss / loss childs while traversing the array.

Perhaps a better way to approach this problem would be catching SIG_CHLD and providing a custom fork() method that kept track of how many children and when they die. This would avoid doing a ps for each element of the array, each time you iterate over it.

Finally, if you're on a multiuser system, please place a sleep(1) or something like that in the infinite loop of the child. You're burning CPU time for every children you spawn in your test.

Good luck and hope this helps...


In reply to Re: Iteration problem on a tied array by fokat
in thread Iteration problem on a tied array by tommyboy

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.