Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( [id://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 = "./runaway.pl"; # 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

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 making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-03-28 12:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found