【发布时间】:2015-03-03 10:59:24
【问题描述】:
我使用QProcess 在服务器上进行冗长的计算。只要我让它自己完成,它可能只需要几秒钟或几个小时,并且工作正常。但是,我需要有可能在它完成之前终止进程,这只能在我的 Windows 机器上正常工作。
在 linux 中,我的QProcess 被杀死,我的数据处理成功执行,但由它产生的子进程仍然存在。这是我的代码的摘录:
// 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();
}
使用terminate() 或kill() 时的行为似乎没有什么不同。据我所知,子进程是我的进程的子进程:
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
我的QProcess 返回他的 pid 为 26395,它有一个孩子 26441,有一个孩子 26442。所以我希望当我杀死我的时候所有这些都被杀死。如前所述,他们幸存下来。是否也有任何独立于平台的方法来杀死这些?
【问题讨论】: