http://qs321.pair.com?node_id=1219970


in reply to Re: Doing every X seconds
in thread Doing every X seconds

You forgot to check the definedness of $child, as upon failure to fork you'll get undef. You likely do not want to pretend you're supposed to be executing the child case code in that circumstance.

I'm puzzled by "# child must explicitly exit otherwise both would continue executing this". The wording suggests both parent and child will continue executing the child case which of course is untrue. As written, the child would fall out of the else clause and the parent's waitpid would still see the child's exit status if that call to exit were not present. You then say to "# reach here if job processing completed successfully" which in your case only the parent will and the child won't because of the above-mentioned exit. Perhaps it would be clearer for the first one to say "... or the child will continue past the else block and we only want the parent to do so" or something similar.

If the intent is for both programs to be done after the branch, a shared exit just after the entire if statement could be useful. Any code you intend for the parent only to run after the child is gone could be placed in the parent's case in the if block, after the loop on waitpid. It's also possible to have the parent exit and the child continue, and that's desirable under certain circumstances.

At this point I think it's worth mentioning there are tools to do this handling for you. Depending on your exact needs, there are Parallel::ForkManager, AnyEvent::ForkManager, IPC::Run, IPC::Run3, Proc::Fork, Proc::Fork::Control, Proc::Daemon, and a whole lot more.

Replies are listed 'Best First'.
A reply falls below the community's threshold of quality. You may see it by logging in.