Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.


In reply to Re^2: Doing every X seconds by mr_mischief
in thread Doing every X seconds by dideod.yang

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-19 02:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found