Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between QProcess::kill() and QProcess::terminate()?

I read some documentation but it isn't clear enough to me. I know that both "end" a process and that kill() is meant to force it to end, but what is terminate() supposed to do then?

like image 597
Michael Avatar asked Jan 09 '23 04:01

Michael


1 Answers

Dunno what's not clear if you have written:

void QProcess::kill()

Kills the current process, causing it to exit immediately.

On Windows, kill() uses TerminateProcess, and on Unix and OS X, the SIGKILL signal is sent to the process.

http://doc.qt.io/qt-5/qprocess.html#kill


void QProcess::​terminate()

Attempts to terminate the process.

The process may not exit as a result of calling this function (it is given the chance to prompt the user for any unsaved files, etc).

On Windows, terminate() posts a WM_CLOSE message to all toplevel windows of the process and then to the main thread of the process itself. On Unix and OS X the SIGTERM signal is sent.

Console applications on Windows that do not run an event loop, or whose event loop does not handle the WM_CLOSE message, can only be terminated by calling kill().

http://doc.qt.io/qt-5/qprocess.html#terminate

So, basically ​terminate() is less brutal, but does not guarantee that process will be terminated.

On Unix terminate() uses SIGTERM signal, while kill() sends SIGKILL to the process. The difference between them is that SIGTERM can be caught by a process, which allows it to perform cleanup etc. SIGTERM can be ignored. SIGKILL will literally kill process, process can not ignore it.

On Windows WM_CLOSE message is posted, when you call terminate(), so application can also gracefully handle it. kill() calls TerminateProcess(), which is more or less Windows equvalent of SIGKILL.

I think terminate() SIGTERM and WM_CLOSE could be handled by Qt and translated into normal Qt events, but you have to try it yourself. You can of course handle them by system specific functions.


"what causes terminate() to not exit the process."

It is you, because you can catch terminate() signals/messages and do whatever you want, or it can be user of your application if he is prompted if he really wants to quit app. Yet another resource on WM_CLOSE.

like image 99
mip Avatar answered Jan 23 '23 22:01

mip