Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Fellow Monks,
once again yet another fork()-question is posted. I definetely think there should be a tutorial around but I couldn't find any. And reading man perlipc didn't make me much smarter, too. Thus here I go...

The problem is: I need to start a program that is known to hang from time to time and to crash at other times. Well, OK, sometimes it works, too :-)

What I did so far is: loop over a certain amount of tries, each time fork and excute the program in the child while the parent process waits for a maximum amount of time, eventually killing the child. Speaking in code:

my $prg = "./"; # program that hangs, dies or exits 0 my $timeout = 4; # seconds to wait my $tries = 4; # number of tries my $run_ok = 0; # just another flag for my $try (0..$tries) { print "\n\n-----------------#$try\n"; if (run()) { print "--- run OK\n"; $run_ok = 1; last; } } unless ($run_ok) { die "---Ieerg!\n"; } #################### sub run { my $return_code = undef; my $pid = fork(); unless (defined $pid) { die "could not fork\n"; } if ($pid == 0) { # child print " child: exec\n"; exec("$prg arguments"); exit; } else { # parent print " parent: wait\n"; my $time = time(); my $kill_flag = 1; while (time() - $time < $timeout) { my $kid = waitpid(-1,WNOHANG); # needs a use POSIX ":sys_wait_h" if ($kid == -1) { $kill_flag = 0; $return_code = 1; last; } sleep 1; } kill(9, $pid) if $kill_flag; } return $return_code; }
The problem with this solution is that I don't get any information whether the program exited with an error or not. Substituting the exec for a system resulted in many processes hanging in the process table.

Can you give me any clues?

Regards... Stefan
you begin bashing the string with a +42 regexp of confusion

In reply to YA fork() question: catching runaway and erroneous processes by stefan k

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others examining the Monastery: (4)
    As of 2018-06-23 14:37 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (125 votes). Check out past polls.