打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
How do I automatically destroy child processes in Windows?

In C++ Windows app, I launch several long running child processes (currently I use CreateProcess(...) to do this.

I want the child processes to be automatically closed if my main processes crashes or is closed.

Because of the requirement that this needs to work for a crash of the "parent", I believe this would need to be done using some API/feature of the operating system. So that all the "child" processes are cleaned up.

How do I do this?

asked Sep 10 '08 at 0:13
jm.
7,162144671

7 Answers

up vote 39 down vote accepted

The Windows API supports objects called "Job Objects". The following code will create a "job" that is configured to shut down all processes when the main application ends (when its handles are cleaned up). This code should only be run once.:

HANDLE ghJob = CreateJobObject( NULL, NULL); // GLOBALif( 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);    }}

Then when each child process is created, execute the following code to launch each child each process and add it to the job object:

STARTUPINFO info={sizeof(info)};PROCESS_INFORMATION processInfo;// Launch child process - example is notepad.exeif (::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 NOTE: See AssignProcessToJobObject always return "access denied" on Vista if you encounter access-denied issues with AssignProcessToObject() on vista.

answered Sep 10 '08 at 0:22
jm.
7,162144671

    
To answer your question in the comment: Yes, you should CloseHandle when you don't need the handle anymore. –  Adam Mitz Sep 12 '08 at 1:30
    
Yeah, but will that end the job? –  jm. Sep 12 '08 at 7:27
1  
No, I don't think so. JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE causes the job to terminate when the last job handle is closed, process handles shouldn't make a difference. Have you tried it? –  Adam Mitz Sep 16 '08 at 2:13
    
It doesn't work on Windows 2000 :( (But it should under other versions of windows) –  Rook Jul 8 '10 at 20:56
1  
@Davit -- you should ask a new question and post some code. –  jm. Aug 20 '11 at 17:30

One somewhat hackish solution would be for the parent process to attach to each child as a debugger (use DebugActiveProcess). When a debugger terminates all its debuggee processes are terminated as well.

A better solution (assuming you wrote the child processes as well) would be to have the child processes monitor the parent and exit if it goes away.

answered Sep 10 '08 at 0:28
Rob Walker
25.9k566111

2  
Instead of having to call DebugActiveProcess, you could just pass DEBUG_PROCESS as one of your creation flags to CreateProcess. Less code that way. –  mrduclaw Jul 17 '09 at 22:12

Windows Job Objects sounds like a good place to start. The name of the Job Object would have to be well-known, or passed to the children (or inherit the handle). The children would need to be notice when the parent dies, either through a failed IPC "heartbeat" or just WFMO/WFSO on the parent's process handle. At that point any child process could TermianteJobObject to bring down the whole group.

answered Sep 10 '08 at 6:24
Adam Mitz
4,5801422

You can keep a separate watchdog process running. Its only task is watching the current process space to spot situations like you describe. It could even re-launch the original application after a crash or provide different options to the user, collect debug information, etc. Just try to keep it simple enough so that you don't need a second watchdog to watch the first one.

answered Sep 10 '08 at 0:25
Pedro
48659

You'd probably have to keep a list of the processes you start, and kill them off one by one when you exit your program. I'm not sure of the specifics of doing this in C++ but it shouldn't be hard. The difficult part would probably be ensuring that child processes are shutdown in the case of an application crash. .Net has the ability to add a function that get's called when an unhandled exception occurs. I'm not sure if C++ offers the same capabilities.

answered Sep 10 '08 at 0:19
Kibbee
36.6k22101146

3  
>> if my main processes crashes –  jm. Jul 21 '10 at 20:59

You could encapsulate each process in a C++ object and keep a list of them in global scope. The destructors can shut down each process. That will work fine if the program exits normally but it it crashes, all bets are off.

Here is a rough example:

class myprocess{public:    myprocess(HANDLE hProcess)        : _hProcess(hProcess)    { }    ~myprocess()    {        TerminateProcess(_hProcess, 0);    }private:    HANDLE _hProcess;};std::list<myprocess> allprocesses;

Then whenever you launch one, call allprocessess.push_back(hProcess);

answered Sep 10 '08 at 0:34
Adam Pierce
13k144974

1  
>> if my main processes crashes –  jm. Jul 21 '10 at 21:00

Just off the top of my head:

  • Have you considered using threads instead of processes?
  • Try passing the handle of the main thread/process to the child processes and get them to wait on that handle. This works for threads, as waiting on a thread handle waits until that thread completes and exits. Not too sure if it'll work for processes, should check out MSDN to verify this.
answered Sep 10 '08 at 4:20
Daemin
5,66122235
 
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Creating a Child Process with Redirected Input and Output (Windows)
新手也可以用c/c++写黑科技
一文读懂远程线程注入
KillSelfProcess
如何提升进程的权限
API HOOK之注册表简单监控
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服