Solved Kill a process and its children
-
@hbatalha said in Kill a process and its children:
There's no way to exit cleanly with QProcess, I have to kill.
Nothing to do with
QProcess
. The question was why ffmpeg has to be killed at all. Why does it not exit normally after doing whatever it is you ask it to do?when it starts ffmpeg I just need ctrl+c to kill the program along with ffmpeg instance it started.
Ctrl+C is a different matter. It is probably sent to ffmpeg when run from command prompt, or something similar. In any case, you are not running it interactively from a command prompt and you say killing the command line parent program does not terminate the ffmpeg, so that is where we are. You can read the discussion in the https://forum.qt.io/topic/86762/can-t-stop-a-process to which you referred.
I told you that if you can confirm that you are able to run Task Manager, find the ffmpeg process by name and kill it from there, then we could look at doing that from code. But you chose not to answer.
-
@hbatalha Try Detach
-
@APM0X400
Why do you think that will help? Worth a try I guess, but I would not have thought so. -
-
@JonB said in Kill a process and its children:
Why does it not exit normally after doing whatever it is you ask it to do?
It does exit normally after completing its task. But I want to give the user the option to cancel whatever it is doing any time. I can't stop ffmpeg with
QProcess
because it is a child process of the processQProcess
But you chose not to answer.
Sorry, I must have missed it. Yes I can go in the task manager and kill with no problem, that's what I have been doing.
@SGaist said in Kill a process and its children:
what exactly are you using to pilot ffmpeg ?
I am using yt-dlp which in turn uses ffmpeg to do some file conversions. Those conversion usually takes a long time, that's why I want to give the user the ability to cancel it anytime.
-
@hbatalha What about https://doc.qt.io/qt-5/qprocess.html#kill ?
-
If the QProcess::kill suggested by @jsulm does not work as expected (which I think should), then try sending the
SIGINT
signal to the process. -
@hbatalha said in Kill a process and its children:
Yes I can go in the task manager and kill with no problem, that's what I have been doing
You can try @SGaist's attempt to send the parent process a
SIGINT
and hope that might interrupt its child, I don't know how that all works on Windows.To get at the ffmpeg process to kill it: you should be able to use EnumProcesses function (psapi.h) to enumerate all running processes and look at their names, and kill the desired one. I came across https://social.msdn.microsoft.com/Forums/en-US/67331e20-a30b-417d-85e5-f9350fef908d/do-we-need-to-use-enumprocesses-and-enumprocessmodules-when-terminating-the-process-in-c?forum=vcgeneral or https://stackoverflow.com/questions/1916574/how-to-effectively-kill-a-process-in-c-win32 you might want to read through. Or Google
windows enumprocesses kill process
to get going. -
-
You send out ctrl + C to exit cleanly
kill( m_process->processId(), SIGINT ); -
I thank everyone's reply here, I was able to find a solution for windows by, as suggested in the replies, sending SIGNT signal to the parent process.
I found the solution in @JonB referenced SO post in this answer and translated it into Qt code:
QProcess::execute("taskkill", {"/pid", QString::number(process->processId()), "/t", "/f"});
I am yet to test the
QProcess::kill
behavior in Linux. It would be a bonus if someone who knows telling me. -
@hbatalha kill( m_process->processId(), SIGINT ); is same as Ctrl + C.
will work on both Windows and Linux. kill() is c code. You do not need taskkill pid on Windows and kill -9 pid on Linux. -
@JoeCFD in which header will I find the kill function?
-
@hbatalha #include <signal.h>
-
@JoeCFD said in Kill a process and its children:
@hbatalha #include <signal.h>
I am still getting
undeclared identifier 'kill'
error after including <signal.h> -
-
@JoeCFD that's for linux, I was asking for MingW equivalent
-
kill() is a posix API. It isn't part of standard C or C++.
-
@hbatalha Sorry. My bad. It is not for Windows. I thought it is standard C code.
You may try kill -9 pid on MingW. -
@JoeCFD what about taskkill that I am currently using?