Unix シグナル ハンドラを使用して、子供に Qt クリーンアップ コードを実行させました。
ここに高レベルの説明があります:
- 親は QProcess を使用して子プロセスを開きます
- 処理が発生します
- 親は、子で SIGTERM シグナルを発生させる QProcess::terminate() を使用して子プロセスを閉じます
- 子は、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();
}
結論:
この解決策が機能することを確認しました。
次の理由から、これは良い解決策だと思います。
- 子プロセスがクリーンアップを実行するように、親が子プロセスを閉じましょう
- 親が誤って閉じて子プロセスを実行したままにした場合、ユーザー/システム管理者は
killコマンドを使用して残りの子プロセスを強制終了でき、子プロセスは閉じる前に自分自身をクリーンアップします
ps "シグナル ハンドラのエントリ ポイントでクリーンアップ コードを直接実行しないのはなぜですか?"
短い答えは、できないからです。Unix シグナル ハンドラ関数で Qt クリーンアップ コードを実行できない理由について説明します。Qt ドキュメント「Unix シグナル ハンドラからの Qt 関数の呼び出し」から:
Unix シグナル ハンドラから Qt 関数を呼び出すことはできません。標準の POSIX ルールが適用されます。シグナル ハンドラーからは非同期シグナル セーフ関数のみを呼び出すことができます。Unix シグナル ハンドラから呼び出すことができる関数の完全なリストについては、シグナル アクションを参照してください。