QProcess::close() 是否在进程上引发 unix SIGTERM 信号? [英] Does QProcess::close() raise the unix SIGTERM signal on the process?

查看:69
本文介绍了QProcess::close() 是否在进程上引发 unix SIGTERM 信号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Linux/Qt 中,我有一个 GUI 应用程序.GUI 使用 QProcess 启动其他子进程.要关闭子进程,我使用 QProcess::close().

In Linux/Qt I have a GUI application. The GUI starts up additional child processes using QProcess. To close the child processes I use QProcess::close().

是否QProcess::close() 为子进程提高unix SIGTERM信号?

Does QProcess::close() raise the unix SIGTERM signal for the child process?

(我已链接到 QProcess 和 close() 的 Qt 文档,因为我无法从文档中判断是否引发了 unix 信号...)

(I have linked to the Qt documentation for QProcess and close() because I can not tell from the documentation if a unix signal is raised...)

更新:更改问题以询问特定的 Unix 信号:SIGTERM.

UPDATE: changed question to ask about a specific unix signal: SIGTERM.

推荐答案

今天我发现 QProcess::close() 不会引发 SIGTERM 信号.为了调试这个问题,我正在捕获子进程的 stderr 和 stdout.剧透:QProcess::terminate() 确实引发了 SIGTERM 信号.

Today I found out that QProcess::close() does not raise the SIGTERM signal. To debug this issue I am capturing the stderr and stdout of the child process. Spoiler: QProcess::terminate() does raise the SIGTERM signal.

处理unix SIGTERM信号的子进程代码:

Child process code to handle unix SIGTERM signal:

static void unixSignalHandler(int signum) {
    qDebug("DBG: main.cpp::unixSignalHandler(). signal = %s\n", strsignal(signum));

    /*
     * Make sure your Qt application gracefully quits.
     * NOTE - purpose for calling qApp->exit(0):
     *      1. Forces the Qt framework's "main event loop `qApp->exec()`" to quit looping.
     *      2. Also emits the QCoreApplication::aboutToQuit() signal. This signal is used for cleanup code.
     */
    qApp->exit(0);
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    MAINOBJECT mainobject;

    /*
     * Setup UNIX signal handlers for some of the common signals.
     * NOTE common signals:
     *      SIGINT:     The user started the process on the command line and user ctrl-C.
     *      SIGTERM:    The user kills the process using the `kill` command.
     *                  OR
     *                  The process is started using QProcess and SIGTERM is
     *                  issued when QProcess::close() is used to close the process.
     */
    if (signal(SIGINT, unixSignalHandler) == SIG_ERR) {
        qFatal("ERR - %s(%d): An error occurred while setting a signal handler.\n", __FILE__,__LINE__);
    }
    if (signal(SIGTERM, unixSignalHandler) == SIG_ERR) {
        qFatal("ERR - %s(%d): An error occurred while setting a signal handler.\n", __FILE__,__LINE__);
    }
    // executes mainbobject.cleanupSlot() when the Qt framework emits aboutToQuit() signal.
    QObject::connect(qApp,          SIGNAL(aboutToQuit()),
                     &mainobject,   SLOT(cleanupSlot()));

    return a.exec();
}

处理子进程的 close() 或 terminate() 的父进程代码:

Parent process code that handles close() or terminate() of the child:

if(this->childProcess1!=NULL && this->childProcess1->state()==QProcess::Running) {
    this->childProcess1->terminate(); // raise unix SIGTERM signal on the process (let's it execute cleanup code)
    if(childProcess1->waitForFinished() == false) {
        qDebug("DBG - MainWindow::close(): Failed to close childProcess1.");
        qDebug() << "DBG - childProcess1->exitCode     =" << childProcess1->exitCode();
        qDebug() << "DBG - childProcess1->ExitStatus   =" << childProcess1->exitStatus();
        qDebug() << "DBG - childProcess1->error()      =" << childProcess1->error();
        qDebug() << "DBG - childProcess1->errorString()=" << childProcess1->errorString();
    }
}
if(this->childProcess2!=NULL && this->childProcess2->state()== QProcess::Running) {
    this->childProcess2->terminate(); // raise unix SIGTERM signal on the process (let's it execute cleanup code)
    if(childProcess2->waitForFinished() == false) {
        qDebug("DBG - MainWindow::close(): Failed to close childProcess2.");
        qDebug() << "DBG - childProcess2->exitCode     =" << childProcess2->exitCode();
        qDebug() << "DBG - childProcess2->ExitStatus   =" << childProcess2->exitStatus();
        qDebug() << "DBG - childProcess2->error()      =" << childProcess2->error();
        qDebug() << "DBG - childProcess2->errorString()=" << childProcess2->errorString();
    }
}

当父进程使用QProcess::terminate()时,子进程输出为:

When the parent uses QProcess::terminate(), the child process output is:

"DBG: main.cpp::unixSignalHandler(). signal = Terminated

当父进程使用`QProcess::close()时,子进程输出为:

When the parent uses `QProcess::close(), the child process output is:

DBG - PARENTOBJECT::close(): Failed to close childProcess.
DBG - childProcess->exitCode     = 0 
DBG - childProcess->ExitStatus   = 1 
DBG - childProcess->error()      = 1 
DBG - childProcess->errorString()= "Unknown error" 

terminate() 实验的输出证明子进程正在收到一个 SIGTERM 信号.

The output from the terminate() experiment proves that the child process is getting a SIGTERM signal.

close() 实验的输出证明子进程没有收到 SIGTERM 信号.

The output from the close() experiment proves that the child process is not getting the SIGTERM signal.

如果要向子进程发送 SIGTERM 信号,请使用 QProcess::terminate().

这篇关于QProcess::close() 是否在进程上引发 unix SIGTERM 信号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆