Hi,

I need to add a timeout for external calls, as the external program sometimes never dies (it's a ClearQuest multisite call to the shipping server, that sometimes never ends, but simply hangs).

I have three different ways of forking the call, but none works. I send the one I believe most in...

Code: ( perl )

1. use POSIX ":sys_wait_h"; 2. my $loop = 1; 3. $loop = $ARGV[0] if defined $ARGV[0]; 4. my $timeout = 10; 5. my $child = fork(); 6. unless ( $child ) { 7. # Child 8. exec( "perl count.pl $loop" ); 9. exit 0; 10. } 11. 12. sleep( $timeout ); 13. my $kid = waitpid( $child, WNOHANG ); 14. if( $kid != -1 ) { 15. print "The child is still running! Kill the process: $child. +..\n"; 16. kill( 9, $child ); 17. } 18. else { print "no timeout\n"; }
The external program count.pl, is simply a program that prints an iterator every second:

Code: ( perl )

1. my $loop = 1; 2. my $iterator = 1; 3. $loop = $ARGV[0] if defined $ARGV[0]; 4. for( ; $iterator <= $loop ; $iterator++ ) { 5. sleep( 1 ); 6. print "$iterator($$)...\n"; 7. }
The idea is fine. After ten seconds, the mother process detects that the child process is still running, and tries to kill it.

And here is the strange thing!!!

The $child value (returned from the fork() command), is not the same as the $$ in the child process!!!

That means that the kill command is trying to kill another process (which in this case doesn't exist).

If I open another command tool window, and type in: perl -e "kill( 9, <child pid> )" the child is killed.

Am I doing something wrong?

I tried also:

Code: ( perl )

1. local $SIG{ALRM} = sub { die "alarm\n" }; # NB \n required 2. alarm 10; 3. $child_pid = fork(); 4. if( $child_pid ) { 5. print "Child_pid = $child_pid\n"; 6. $pid2 = waitpid( $child_pid, 0 ); 7. } 8. elsif( $child_pid == 0 ) { 9. print "child pid= $$\n"; 10. exec( "ratlperl count.pl $x" ); 11. exit( 0 ); 12. }
I do get an alarm signal after the timeout, but I don't have the correct pid here also. kill() doesn't work.

Any help would be appreciated very much.

Cheers, /Richard


In reply to Problem to add a timeout to external calls on Win32 by webotronics

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.