Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performing equivalent of "Kill Process Tree" in C++ on windows

Tags:

c++

windows

kill

We have a C++ task that will fork a new process. That process in turn may have several child processes. If the task runs past an allotted time, we will want to kill that forked process.

However, we don't want to orphan the processes it has spawned. We want them all to die. I have used Process Explorer and it has a "Kill Process Tree" option, similar to Windows Task Manager's "End Process Tree", so I'm guessing/assuming there is a public API to do this. Has anyone done this, or know of a reference to a public API that does?

like image 998
Brett McCann Avatar asked Mar 02 '09 23:03

Brett McCann


People also ask

How do I kill a PID in Windows?

To kill the process using PID a) Type the following command into the command prompt, to kill only one Process, and press Enter Key. For Example - To kill Notepad, run the command as, taskkill /PID 2404 /F, where /F is used to kill the process forcefully.

What utility do you use to kill a process in Windows?

PsKill is a kill utility that not only does what the Resource Kit's version does, but can also kill processes on remote systems. You don't even have to install a client on the target computer to use PsKill to terminate a remote process.


1 Answers

You might want to consider the "Jobs API". CreateJobObject and friends. You can enforce children processes to stay within the Job, by setting appropriate attribute. Then you can call TerminateJobObject whenever you want.

Clarification: this is NOT what Task Manager does.

like image 136
EFraim Avatar answered Sep 21 '22 15:09

EFraim