in reply to killing on win32

Using fork on win32 for anything other than simple uses just creates problems. On win32, fork is emulated using a thread, and exec spawns a new process running the exec'd program and the thread waits for that child process. The 'pid' you get back is a funny (negated) thread id, not a process id.

You are given no access to the process id of the spawned process, and as there is (could not be) any parent-child relationship between the thread that is spawned and the process it monitors, killing the thread via is funny thread-id doesn't kill the process.

Upshot. Using fork on win32 is fraught with problems. If you need to do it, using the forking open which returns the real pid of the forked process is usually preferrable. However, even that has problems. Unless you take care to ensure that Perl doesn't decide to run the program you asked for via a shell, you can end up with the pid of the shell rather than the program you asked to run and again, you have no access to the pid of the actual process and cannot kill it.

Historically, unlike POSIX platforms, all win32 processes were considered peers. Ie. There was no parent-child relationship between a process B start by a process A. Killing process A left process B untouched. There are now, and have been for many years, options to CreateProcess that create a hierarchal relationship between processes such that killing process A above would also terminate process B, (see the "End process tree" option on the Task Manager context menu), but Perl has never caught up in this regard.

If, as implied by your post, you are mixing threads with fork, you are in a world of hurt.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

Replies are listed 'Best First'.
Re^2: killing on win32
by Razvanica (Novice) on Sep 06, 2007 at 10:49 UTC
    Thanks BrowserUk,
    As I already said, I need to use fork on Windows. I am not mixing it with threads or with anything else. I am calling the child processes "threads" because that's what they really are. I would like to know if there is a way of killing all the "child-threads" on Windows and keep the main thread still alive.
    Thanks.
      would like to know if there is a way of killing all the "child-threads" on Windows and keep the main thread still alive.

      As I tried to explain above, not unless you can get access to the real process ids.

      The only ways I know how to do that is to use the forking open:

      my $pid = open my fh, "/path/to/the/exe args |" ...;

      or the asynchronous spawn (

      my $pid = system 1, '/path/to/the/exe args';

      rather than fork & exec.

      Neither of which maps particularly well onto the fork & exec idioms typical of *nix code.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.