Where is this fork you keep mentioning? Are you doing
if (!fork()) { system(...) } or if (!fork()) { exec(...) }? If you do the former, you'll have two copies of the parent perl script running once the child exits.
Example use of launching an executable using fork():
# We are in the only process.
printf STDERR ("pid = %d\n", $$) if $DEBUG;
$pid = fork();
die("fork failed!\n") unless (defined($pid));
if (!fork())
{
# We are in the child process.
printf STDERR ("child pid = %d\n", $$) if $DEBUG;
exec(...);
# We'll never reach here if exec() worked.
# Avoiding die(); we don't want eval{} catching this.
print STDERR "exec failed!\n";
exit(2);
}
# We are in the parent.
printf STDERR ("parent (pid = %d) successfully spawned child (pid = %d
+).\n", $$, $pid) if $DEBUG;
Alternatively:
use Win32;
use Win32::Process;
sub GetLastErrorStr {
return Win32::FormatMessage(Win32::GetLastError());
}
my $process_obj;
Win32::Process::Create(
$process_obj,
"bla.exe",
"arg1 arg2",
0,
NORMAL_PRIORITY_CLASS,
"."
) || die("Error running bla.exe: " . GetLastErrorStr() . "\n");
printf STDERR ("parent (pid = %d) successfully spawned child (pid = %d
+).\n", $$, $process_obj->GetProcessID()) if $DEBUG;
...
$process_obj->kill(255);
By the way, in Windows, I don't think the process has a chance to shut itself down properly when told to die using kill(). If the application has a window, it's better to send WM_QUIT to the window (and kill it if that doesn't work). If it doesn't have a window, it provides some kind of API (usually a DLL call) that will shut it down. This could explain your 100% usage after killing the process.
-
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.