在C++ Windows应用程序中,我启动了几个长时间运行的子进程(目前我使用CreateProcess(...)要做到这一点。
如果我的主进程崩溃或被关闭,我希望子进程自动关闭。
因为这需要解决“父进程”崩溃的问题,所以我认为这需要使用操作系统的一些API/功能来完成。这样所有的“子”进程都会被清理掉。
我该怎么做呢?
发布于 2008-09-10 00:22:50
Windows API支持称为“作业对象”的对象。下面的代码将创建一个“作业”,该作业被配置为当主应用程序结束时(当它的句柄被清除时)关闭所有进程。此代码应该只运行一次。:
HANDLE ghJob = CreateJobObject( NULL, NULL); // GLOBAL
if( ghJob == NULL)
{
::MessageBox( 0, "Could not create job object", "TEST", MB_OK);
}
else
{
JOBOBJECT_EXTENDED_LIMIT_INFORMATION jeli = { 0 };
// Configure all child processes associated with the job to terminate when the
jeli.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
if( 0 == SetInformationJobObject( ghJob, JobObjectExtendedLimitInformation, &jeli, sizeof(jeli)))
{
::MessageBox( 0, "Could not SetInformationJobObject", "TEST", MB_OK);
}
}然后,在创建每个子进程时,执行以下代码以启动每个子进程,并将其添加到作业对象中:
STARTUPINFO info={sizeof(info)};
PROCESS_INFORMATION processInfo;
// Launch child process - example is notepad.exe
if (::CreateProcess( NULL, "notepad.exe", NULL, NULL, TRUE, 0, NULL, NULL, &info, &processInfo))
{
::MessageBox( 0, "CreateProcess succeeded.", "TEST", MB_OK);
if(ghJob)
{
if(0 == AssignProcessToJobObject( ghJob, processInfo.hProcess))
{
::MessageBox( 0, "Could not AssignProcessToObject", "TEST", MB_OK);
}
}
// Can we free handles now? Not sure about this.
//CloseHandle(processInfo.hProcess);
CloseHandle(processInfo.hThread);
}vista注意:如果在VISTA上使用AssignProcessToObject()遇到访问被拒绝的问题,请查看AssignProcessToJobObject always return "access denied" on Vista。
https://stackoverflow.com/questions/53208
复制相似问题