simple timer for win32, solaris and linux

E

elastic

I'm trying to implement a simple timer to work on win32 (98,2k,xp),
solaris and linux. where the last two are simple, the first seems to
be problematic.
the problem is that the kill command seems to always fail.
the pid is always negative and doesn't apear in the task manager.

this is the perl version I'm using :
This is perl, v5.6.1 built for MSWin32-x86-multi-thread
(with 1 registered patch, see perl -V for more detail)

Copyright 1987-2001, Larry Wall

Binary build 635 provided by ActiveState Corp.
http://www.ActiveState.com
Built 15:34:21 Feb 4 2003
this is the function :
sub system_with_timeout {
my ($rc);
my ($command) = @_;

$pid = fork ();
if (! (defined ($pid))) { #hosed
die "fork() error: $!\n";
} elsif ($pid == 0) { #child
$rc = system ($command);
} else { #parent (defined $pid)
for ($i = 0 ; $i < $g_APP_RUN_TIMEOUT_SEC ; $i++) {
sleep 1;
if (kill 0, $pid) {
kill 9, $pid;
return -1;
}
}
}
return $rc;
}
if anyone has any idea of what I'm doing wrong here, please help.
 
E

elastic

I'm trying to implement a simple timer to work on win32 (98,2k,xp),
solaris and linux. where the last two are simple, the first seems to
be problematic.
the problem is that the kill command seems to always fail.
the pid is always negative and doesn't apear in the task manager.

this is the perl version I'm using :
This is perl, v5.6.1 built for MSWin32-x86-multi-thread
(with 1 registered patch, see perl -V for more detail)

Copyright 1987-2001, Larry Wall

Binary build 635 provided by ActiveState Corp.
http://www.ActiveState.com
Built 15:34:21 Feb 4 2003
this is the function :
sub system_with_timeout {
my ($rc);
my ($command) = @_;

$pid = fork ();
if (! (defined ($pid))) { #hosed
die "fork() error: $!\n";
} elsif ($pid == 0) { #child
$rc = system ($command);
} else { #parent (defined $pid)
for ($i = 0 ; $i < $g_APP_RUN_TIMEOUT_SEC ; $i++) {
sleep 1;
if (kill 0, $pid) {
kill 9, $pid;
return -1;
}
}
}
return $rc;
}
if anyone has any idea of what I'm doing wrong here, please help.

sorry, should be :
sub system_with_timeout {
my ($rc);
my ($pid);
my ($command) = @_;

$| = 1;
$pid = fork ();
if (! (defined ($pid))) { #hosed
die "fork() error: $!\n";
} elsif ($pid == 0) { #child
$rc = system ($command);
exit ($rc);
} else { #parent (defined $pid)
for ($i = 0 ; $i < $g_APP_RUN_TIMEOUT_SEC ; $i++) {
if (! (kill 0, $pid)) {
return $rc;
} else {
sleep 1;
}
}
print "timeout passed - killing : $pid\n";
kill 'KILL', $pid;
return -1;
}
return $rc;
}

problem is when the child is killed by initiating parent it doesn't
kill the process launched by the call to system.

still any help will be appreciated
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top