如何杀死的工艺窗口所有儿童的递归(How to kill all recursive childre

2019-10-19 02:26发布

我有一个问题,但没有解决方案。

我的程序运行,并创造了一些过程,但过程去创造另一个进程。 我不知道如何让所有的孩子杀死所有。

我用CreateToolhelp32Snapshot函数来获得,其父母为我的程序(mainthread)所有儿童...,然后从mainthread的每个孩子让所有的孩子。

hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );

如果我用这个解决方案,性能是非常低的,因为我已经在第一时间全部过程的PID,所以我不能需要调用CreateToolhelp32Snapshot再次

其中最好的解决办法我能做得到的工艺窗口都递归的孩子吗?

Answer 1:

首先来看看的Win32 API作业 。 其中一个功能是自动终止属于同一作业的所有进程的能力。 见TerminateJobObject API。

编辑:陈先生刚的博客上讲述的是精确的主题。 销毁所有子进程(和孙子)父退出时

然后,你的问题。 你可以建立所有当前正在运行的进程的一个std ::地图,关键是在PID,并且是子进程的一个std :: vector的值。 然后,您可以递归在地图上终止进程和他的所有儿童。

// KillChildren.cpp
// Usage: pass a PID a argument

#include <Windows.h>
#include <TlHelp32.h>
#include <vector>
#include <map>

typedef std::vector<DWORD> VEC_CHILDS;
typedef VEC_CHILDS::iterator IT_CHILDS;
typedef std::map<DWORD, VEC_CHILDS> MAP_PIDS;
typedef MAP_PIDS::iterator IT_PIDS;

void KillProcess( DWORD dwPID ) {

    HANDLE hProcess = OpenProcess( PROCESS_TERMINATE | SYNCHRONIZE, FALSE, dwPID );
    if ( hProcess == NULL ) {
        printf( "Cant't OpenProcess for PID %u, Reason %u\n", dwPID, GetLastError() );
        return;
    }
    BOOL bWin32Success = TerminateProcess( hProcess, 0 );
    if ( bWin32Success == 0 ) {
        printf( "Cant't TerminateProcess for PID %u, Reason %u\n",
                dwPID, GetLastError() );
    } else {
        DWORD dwRetVal = WaitForSingleObject( hProcess, 2000 );
        if ( dwRetVal != WAIT_OBJECT_0 ) {
            printf( "Failed to Wait for Process Termination for PID %u,"
                    "RetVal %u Reason %u\n", dwPID, dwRetVal, GetLastError() );
        } else {
            printf( "Process %u Terminated\n", dwPID );
        }
    }
    CloseHandle( hProcess );

}

void KillChilds( DWORD dwParentPID, MAP_PIDS & mPids ) {
    IT_PIDS it = mPids.find( dwParentPID );
    if ( it == mPids.end() ) return;
    VEC_CHILDS & vChilds = it->second;
    for ( IT_CHILDS itChild = vChilds.begin(); itChild != vChilds.end(); ++itChild ) {
        KillChilds( *itChild, mPids );
    }
    KillProcess( dwParentPID );
}

// usage: PID as first arg
int main( int argc, char* argv[] ) {

    if ( argc <= 1 ) return -1;
    DWORD dwPID = atoi( argv[ 1 ] );

    HANDLE hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
    if ( hProcessSnap == INVALID_HANDLE_VALUE ) return -1;

    // build PID list with children
    PROCESSENTRY32 pe32;
    pe32.dwSize = sizeof( PROCESSENTRY32 );
    if( !Process32First( hProcessSnap, &pe32 ) ) {
        CloseHandle( hProcessSnap );
        return -1;
    }
    MAP_PIDS mPids;
    do {
        // Add as a Parent, with no child, yet, if not already done
        IT_PIDS it = mPids.find( pe32.th32ProcessID );
        if ( it == mPids.end() ) mPids[ pe32.th32ProcessID ] = VEC_CHILDS();
        // Process the Parent
        it = mPids.find( pe32.th32ParentProcessID );
        if ( it == mPids.end() ) {
            // unknown parent, add it with one child
            VEC_CHILDS vChilds;
            vChilds.push_back( pe32.th32ProcessID );
            mPids[ pe32.th32ParentProcessID ] = vChilds;
        } else {
            // Parent already here, add one more child
            it->second.push_back( pe32.th32ProcessID );
        }
    } while ( Process32Next( hProcessSnap, &pe32 ) );
    CloseHandle( hProcessSnap );

    KillChilds( dwPID, mPids );
    return 0;

}

如果你想测试上面的程序,这里是一个小的进程树发生器

// CreateChildren.cpp
// beware, messing may be 'fork bombing'

#include <Windows.h>

int main( int argc, char* argv[] ) {

    srand( GetTickCount() );
    // always childs if args
    if ( argc == 0 ) {
        int iRandom = rand();
        if ( iRandom % 3 ) Sleep( INFINITE );
    }

    char szFullExeName[ MAX_PATH ];
    DWORD dwCopied = GetModuleFileName( NULL, szFullExeName, sizeof( szFullExeName ) );
    if ( dwCopied == 0 ) return -1;

    STARTUPINFO si;
    memset( &si, 0, sizeof( si ) );
    si.cb = sizeof( si );
    PROCESS_INFORMATION pi;
    int ChildCount = rand() % 3;
    while ( ChildCount-- ) {
        BOOL bWin32Success = CreateProcess( szFullExeName, NULL, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi );
        if ( bWin32Success ) {
            CloseHandle( pi.hThread );
            CloseHandle( pi.hProcess );
        }
    }

    Sleep( INFINITE );
    return 0;

}


文章来源: How to kill all recursive children of process windows