I use a QProcess
for a lengthy calculation on a server. It may take only a few seconds or up to several hours and works fine as long as I let it finish on its own. However, I need to have the possibility to kill the process before it finishes which works fine only on my windows machine.
In linux, my QProcess
is killed and the processing of my data is executed sucessfully, but the child processes spawned by it remain. Here is an excerpt of my code:
// constructor
server::server(QObject* parent) : QTcpServer(parent)
{
this->calc_proc = new QProcess(this);
QObject::connect(this->calc_proc, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(calc_finished(int,QProcess::ExitStatus)));
...
}
void server::start_job(){
QString working_directory = "...";
QString input_file = "...";
QStringList arguments;
arguments << "memory=max" << "batch=no" << input_file;
this->calc_proc->setWorkingDirectory(working_directory);
this->calc_proc->start("path_to_external_program", arguments);
qDebug() << "Calc started with pid: " << this->calc_proc->pid();
}
void server::kill_job(){
this->calc_proc->terminate();
//this->calc_proc->kill();
}
The behaviour does not seem to differ when using terminate()
or kill()
. The child processes are children of my process to my knowledge:
Calc started with pid: 26395
ps ax
...
26441 pts/0 S 0:00 time /msc/MSC_Nastran/20131/msc20131/linux64/analysis
26442 pts/0 Rl 16:59 /msc/MSC_Nastran/20131/msc20131/linux64/analysis
...
ps -p 26442 -o ppid=
26441
ps -p 26441 -o ppid=
26395
My QProcess
returns his pid as 26395 which has a child 26441 which has a child 26442. So I'd expect that all of these are killed when I kill mine. As stated they survive. Is there any platform-independent way of killing these as well?