473,503 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Perl problem to add timeout for external calls on Win32

1 New Member
Hi,

I need to add a timeout for external programs, 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...

Expand|Select|Wrap|Line Numbers
  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";  }
  19.  
  20.  
The external program count.pl, is simply a program that prints an iterator every second:

Expand|Select|Wrap|Line Numbers
  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. }
  8.  
  9.  
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:

Expand|Select|Wrap|Line Numbers
  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.     }
  13.  
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
Dec 7 '07 #1
1 1930
numberwhun
3,509 Recognized Expert Moderator Specialist
When you are posting code in the forums, please be sure and use the proper code tags. They are required and as you can see from your post, I have modified your post to contain them.

Regards,

Moderator
Dec 7 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

13
2686
by: Wayne Folta | last post by:
I've been a long-time Perl programmer, though I've not used a boatload of packages nor much of the tacky OO. A couple of years ago, I decided to look into Python and Ruby. Python looked OK, but...
3
2732
by: Myron Turner | last post by:
I'm not sure whether this question belongs entirely here or in a perl group--but probably it requires knowledge of both. I've written a perl module, currently in use, which does asynchronous...
11
14929
by: Frank Rizzo | last post by:
Hello, My c# based windows service takes a while to dispose. I have to release bunch of resources all over the place and unfortunately it can take 20-40 seconds before I can cleanly exit. ...
0
9734
by: Kirt Loki Dankmyer | last post by:
So, I download the latest "stable" tar for perl (5.8.7) and try to compile it on the Solaris 8 (SPARC) box that I administrate. I try all sorts of different switches, but I can't get it to compile....
3
1365
by: Jeremy Chapman | last post by:
I've got a 2rd party API written in perl. We want to use it by converting to a c# .net assembly. Essentially it does HTTP posts to a web page to send/receive data. I've never done perl, and am...
2
5300
by: f rom | last post by:
----- Forwarded Message ---- From: Josiah Carlson <jcarlson@uci.edu> To: f rom <etaoinbe@yahoo.com>; wxpython-users@lists.wxwidgets.org Sent: Monday, December 4, 2006 10:03:28 PM Subject: Re: ...
1
2301
by: Tension | last post by:
Hi. I have a Perl script that starts a build process (plus a lot of other things). The build process needs a few environment variables set. This is done automatically in a buildEnv.bat script...
10
6931
by: happyse27 | last post by:
Hi All, I got this apache errors(see section A1 and A2 below) when I used a html(see section b below) to activate acctman.pl(see section c below). Section D below is part of the configuration...
0
7201
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7083
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7278
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5011
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3166
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3153
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
379
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.