I'm attemptintg to run several separate forked SQL queries from a function in a program. I'd like to avoid zombies, run each query in the background and with nohup. The final result will be a report on data counts.

do_queries() will do the work by calling do_query() many times passing a new argument each time. I'll eventually pass in an array of all the arguments for each query.

The question is: How to background each query? Running just one query the program (parent) waits for the child to return, i.e. freezes until done. It should not freeze until all the queries are forked and done.

Also, it this is not the right approach what should I do?

sub do_queries { $rc = get_query('t_ub92_hdr_ext_key', 'D', '20101001', '20101031') +; print "-------Query Returned is $rc\n\n\n"; defined(my $pid = fork) or die "Cannot fork: $!"; unless($pid) { print "Child process running\n\n\n"; $rc = do_query($rc); if ($rc == 0) { print "Query Done!\n"; print $time = localtime(); } exit 0; print "This is the parent process(waiting!) and child ID is $p +id\n\n\n"; while (wait() != -1) {} do { my $kid = waitpid(-1, WNOHANG); } while $kid > 0; print "Parent Process is Exiting\n"; } }

In reply to Background forked processes from a function. by donfox1

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.