Razvanica has asked for the wisdom of the Perl Monks concerning the following question:

Hello all,
I keep on fighting with threads and fork() on Windows. The problem now is that I am trying to kill a child thread. I have a daemonn listening for requests on a socket and when it receives "stop" it should kill all child threads and keep on listening on the socket. I am trying to do it like this:
foreach my $child (@childPids) { kill 9, $child; }
This code seems to kill only threads which did not create other child threads. I mean, if I have a thread where I also do a fork(), that thread it is not killed.
What I want is to find a way for killing all threads, except the main one. Any idea how can I do that?
Thanks.

Replies are listed 'Best First'.
Re: killing on win32
by BrowserUk (Patriarch) on Sep 06, 2007 at 10:38 UTC

    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.
      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.
Re: killing on win32
by technojosh (Priest) on Sep 06, 2007 at 13:13 UTC
    Take a look at:
    Win32::Process::KillProcess

    I know there are a few issues with it, but if you have your @childPids, then, from the cpan docs:

    Win32::Process::KillProcess($pid, $exitcode)

    Terminates any process identified by $pid. $exitcode will be set to the exit code of the process.

      The problem is that the 'pid's returned by fork are not real pids, they are pseudo-fork perl-internal-use-only, negated thread ids, so they cannot be used with any of the win32 systems calls.

      To be clear, the $pid used in the Win32::Process examples must be real, win32 process ids and Perl does not expose these.


      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.

        When I want to kill processes manualy on w2003, I drop down to the command line and play with wmic process list (you could get started with wmic process list /?). At least one form of the output provides the ppid (parent process) as well as the real pid and the full command line. This makes me think you could walk up or down the tree of all pids and be pretty sure of identifying those that are yours and kill things from the bottom up.

        I haven't used it, but Win32::Process::Info::WMI seems to have the ability to query wmi programmatically to get this information. If that works, then all you'd have to worry about is how patched the particular win32 you're dealing with is and whether it has an up-to-date WMI.


        I humbly seek wisdom.