in reply to Extremely odd behavior with net::ping and fork

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.

Replies are listed 'Best First'.
Re^2: Extremely odd behavior with net::ping and fork
by wolfger (Deacon) on Aug 18, 2004 at 17:15 UTC
    Where is this fork you keep mentioning?
    Holy cripes... I posted the wrong source! How did I manage that?
    The call to &startup was replaced in a later version with
    defined(my $pid = fork) or die "Cannot fork: $!"; unless ($pid) { # Child process is here system ('maincnc.exe'); } # Parent process is here
    Problem is fixed now, using exit;after the system(). I must have accidentally opened the older version of the script after rebooting my computer, rather than the new version. Maybe I'd have had beter help if I posted the correct source!
    You guys were great in helping me anyway. Thanks for all the feedback!

    --
    Believe nothing, no matter where you read it, or who said it - even if I have said it - unless it agrees with your own reason and your own common sense.
    (Buddha)