Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Nested forks -help!
by ariels (Curate) on Jul 23, 2001 at 18:06 UTC | |
by Anonymous Monk on Jul 23, 2001 at 19:50 UTC | |
by ariels (Curate) on Jul 24, 2001 at 11:07 UTC | |
|
Re: Nested forks -help!
by Malkavian (Friar) on Jul 23, 2001 at 19:08 UTC |