繁体   English   中英

QProcess :: kill()不会杀死Linux中的孩子

[英]QProcess::kill() does not kill children in linux

我使用QProcess在服务器上进行冗长的计算。 只要我自己完成,它可能只需要几秒钟或几个小时,就可以正常工作。 但是,我需要有可能在进程完成之前终止该进程,该进程仅在Windows机器上可以正常运行。

在linux中,我的QProcessQProcess ,对数据的处理成功执行,但是由它产生的子进程仍然保留。 这是我的代码的摘录:

// 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。 如前所述,它们可以生存。 是否有任何与平台无关的方法来杀死这些对象?

user4419802使我走上了正确的轨道。 在Linux中,杀死进程不会杀死其子进程。 他们被上移并继续生活而不再有父母。

通常,您希望在pids子代时保存所有pids (并且有多个答案告诉您如何操作),但是在我的情况下这是不可能的,因为我没有在生成子代,但是我调用的外部应用程序却这样做没有我的知识。 因此,我想出了以下对我有用的解决方案:

QProcess* main_process = new QProcess(this);
...

// linux: a child is spawned which is not killed with its parent
//        therefore the process id is retrieved and the process killed independently
#if defined(Q_OS_LINUX)
QProcess get_child_a;
QStringList get_child_a_cmd;
get_child_a_cmd << "--ppid" << QString::number(main_process->processId()) << "-o" << "pid" << "--no-heading";
get_child_a.start("ps", get_child_a_cmd);
get_child_a.waitForFinished(5000);
QString child_a_str = get_child_a.readAllStandardOutput();
int child_a = child_a_str.toInt();

QProcess::execute("kill " + QString::number(child_a));
#endif

// windows & linux: kill main process
main_process->kill();

就我而言,我知道仅返回一个子进程ID,因此解析get_child进程输出是一种简单的转换。

QProcess::kill()只是SIGKILL 因此,这与Qt无关,而与Unix进程管理有关。

您可以阅读有关该问题的信息如何在父级退出后使子进程死亡?

最初的想法是@Bowdzone!

由于答案对我来说不是100%有用的,因为我的父母产生了多个子进程,所以我对该方法进行了一些调整:

void CameraReceiver::stopChildProcesses(qint64 parentProcessId) {
    qDebug() << "stopChildProcesses for parent id:" << parentProcessId;

    QProcess get_childs;
    QStringList get_childs_cmd;
    get_childs_cmd << "--ppid" << QString::number(parentProcessId) << "-o" << "pid" << "--no-heading";
    get_childs.start("ps", get_childs_cmd);
    get_childs.waitForFinished();
    QString childIds(get_childs.readAllStandardOutput());
    childIds.replace('\n', ' ');

    QProcess::execute("kill " + childIds);
}

然后,我可以简单地调用以下命令:

stopChildProcesses(parentProcess.processId());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM