Hi all, Any help will be much appreciated on the subject. I am writing an application whereby I would like to fork a few processes, and in these forked processes, fork additionnal processes without any of the parent processes to have to wait for their children to finish.
It is something of the form:
foreach $customer (@customers) { if ($cust_pid = fork) { #in parent sleep 1; $SIG{CHLD} = 'IGNORE'; } if ($cust_pid == 0) { #in child foreach $router (@routers) { if ($router_pid = fork) { #in parent sleep 1; $SIG{CHLD} = 'IGNORE'; } if ($router_pid == 0) { #in child #do something exit; #this exits child process at router level } } } exit; #this exits child process at customer level }

So basically, what I am trying to do is for each customer in my list of customers, fork a process, and then for each router owned by that customer, fork another process and this is where my main application takes place, at router level. I think what I have here are nested forks. My probelm is that my application is giving me really strange results when I have nested forks. Perl seems to be confusing which child belongs to which parent or something of the kind. The reason I am saying this is because when I test my code without the forking at customer level, just at router level, it works perfectly. More over, when I replace $SIG{CHLD} = 'IGNORE'; with waitpid($cust_pid, 0); at the customer level forking (ie make the parent process wait for its child process to terminate before beginning a new one), the code works perfectly as well. I need though to have all these processes run concurrently and hence I do not want to use the waitpid($cust_pid, 0); solution. Any help on possible alternative ways will be VERY appreciated.

Thanks in advance,
Elias.

PS: if you're interested by what kind of application I am writing, it's basically a polling engine that monitors some remote site routers via an ISDN link, and the polls are made from central site routers via the cisco ping mib. In order to do the polling, I am using Net::SNMP module. Just thought you might want to know.


In reply to Nested forks -help! by Anonymous Monk

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.