Some progress:
my $forks = 0; foreach my $i (1..3) { if ($i == 1 && fork() == 0) { $forks++; print "Running first with $$ \n"; call_sub1(); call_sub2(); call_sub3(); call_sub4(); call_sub5(); call_sub6(); exit; } elsif ($i == 2 && fork() == 0) { $forks++; print "Running second $$ \n"; call_sub1(); call_sub10(); call_sub3(); call_sub13(); call_sub5(); exit; } else { if (fork() == 0) { $forks++; print "Running third with $$ \n"; call_sub1(); call_sub4(); call_sub5(); call_sub15(); exit; } } } for (1 .. $forks) { my $pid = wait(); print "Parent saw $pid exiting\n"; } print "done\n";
It really feels messy and not clear. Also, it is not the most efficient because ALL of the 15 calls are not depend one on another. with that logic I can create 15 if-else statements but of course it is not readable and clear. Also, the names are not the same (and all of them gets arguments - I removed them so it will be more easy to see). How should I solve it? Also, should I kill the child process when its done by using exit?

In reply to Re: Learning to use fork() by ovedpo15
in thread Learning to use fork() by ovedpo15

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.