linux - 你如何关闭一个Qt子进程并让子进程执行清理代码?

标签 linux qt process ipc child-process

我在 Linux/Qt 中启动一个进程,然后使用 QProcess 启动一些子进程。然后最终我想优雅地关闭子进程(也就是执行一些清理代码)。

子进程正在使用 QSharedMemory现在,当我调用 QProcess::close() 时,子进程正在关闭而不调用 QSharedMemory::detach()结果是所有进程都关闭了...但是还有未清理的共享内存。

我有子进程的代码,代码中有函数 cleanup()。父进程如何以这种方式关闭QProcess,以便子进程执行cleanup()

最佳答案

我让 child 使用 unix 信号处理程序执行 Qt 清理代码。

这是一个高级解释:

  • 父进程使用QProcess打开子进程
  • 处理发生
  • 父进程使用 QProcess::terminate() 关闭子进程,这会在子进程上引发 SIGTERM 信号
  • child 为 SIGTERM 实现了一个 unix 信号处理程序
  • 从 unix 信号处理程序 qApp->exit(0);发生
  • qApp 发出 Qt 信号“aboutToQuit()”
  • 将子进程 cleanup() 槽连接到 qApp aboutToQuit() 信号

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

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();
}

结论:

confirmed that this solution works .

我认为这是一个很好的解决方案,因为:

  • 让父进程以子进程执行清理的方式关闭子进程
  • 如果父进程错误关闭并让子进程继续运行,用户/系统管理员可以使用 kill 命令杀死剩余的子进程,子进程仍会在关闭前自行清理

附注“为什么不直接在信号处理程序入口点执行清理代码?”

简短的回答是因为你做不到。这是关于为什么不能在 unix 信号处理函数中执行 Qt 清理代码的解释。来自 Qt documentation "Calling Qt Functions From Unix Signal Handlers" :

You can't call Qt functions from Unix signal handlers. The standard POSIX rule applies: You can only call async-signal-safe functions from signal handlers. See Signal Actions for the complete list of functions you can call from Unix signal handlers.

关于linux - 你如何关闭一个Qt子进程并让子进程执行清理代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7502174/

相关文章:

linux 内核 consoleblank 参数被忽略

python - PySide 入门

linux - 如何使用 `waitpid`等待Rust中的进程?

c - fork 和管道创建

linux - 我该如何解决警告 DEPMOD

c++ - QFileDialog::getOpenFileName 调试时崩溃,显然是由项目名称引起的?

node.js - Nodejs 进程挂起,我该如何调试它或收集转储?

C 程序产生两个进程树而不是一个

xml - 用于格式化 XML 文件并保留空行和注释的 CLI 工具

c++ - 如何使用TagLib-C++从MP3文件读取XingHeaders,VBRIHeaders和sampleCount