Here is the problem. I want to write a progam that forks itself the specified number of times. And each of the forked programs runs another script a specified number of times. Here is what I tried but its not doing what I want, sigh
#!/usr/local/bin/perl -w # %W% %G% # Michael Petnuch, June 26, 2002 use strict; my %opt; %opt = get_args(); foreach ( 1 .. $opt{'F'}) { my $child; if(!defined($child = fork())) { die "Cannot fork: $!"; if(!defined($child = fork())) { die "Cannot fork: $!"; } elsif ($child == 0) { fork_it(); } else { sleep(2); next; } } ####### SUBS ####### sub fork_it { foreach( 1 .. $opt{'P'}) { exec("$opt{'E'} > /dev/null 2>&1"); } } sub help { my $error = shift; print $error, "\n" if(defined($error)); print STDERR "\n" ."Usage: $0 [-f number of childs] [-p number script processess +]\n" ." [-e what to execute] [-s how long to sleep inbetwe +en forks]\n\n" ."Example: $0 -f 2 -p 2 -e /usr/bin/tty_wrapper.sh -s 2\n\n"; exit(0); } sub get_args { my %opt; while ($_ = lc shift @ARGV) { if(/-f/) { $opt{'F'} = shift @ARGV; if(/-f/) { $opt{'F'} = shift @ARGV; } elsif (/-s/) { $opt{'S'} = shift @ARGV; } elsif (/-p/) { $opt{'P'} = shift @ARGV; } elsif (/-e/) { $opt{'E'} = shift @ARGV; } elsif (/(-|--)(h$|help$)/) { help(); } else { help("Invalid parameter $_"); } } return %opt; }

In reply to Fork horror by debiandude

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.