gpt4 book ai didi

c++ - QThreaded C++ 守护程序未正常关闭

转载 作者:行者123 更新时间:2023-11-28 02:56:01 26 4
gpt4 key购买 nike

我编写了一个 Qt5/C++ 程序,它在后台 fork 和运行,响应信号停止并正常关闭。一切听起来不错,但是当我“ps ax | grep myprog”时,我看到我的一堆程序仍在运行;例如:

29244 ?        Ss   149:47 /usr/local/myprog/myprog -q
30913 ? Ss 8:37 /usr/local/myprog/myprog -q
32484 ? Ss 0:11 /usr/local/myprog/myprog -q

如果我在前台运行该程序,那么该进程不会在进程列表中停留 - 它会按预期结束。这只发生在后台时。为什么?


更新:我发现我的程序处于futex_wait_queue_me状态(queue_me并等待唤醒、超时或信号)。我确实有 3 个独立的线程 - 这可能是相关的。所以我将调试器附加到其中一个等待进程并发现了这个:

(gdb) bt
#0 0x000000372460b575 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
#1 0x00007f8990fb454b in QWaitCondition::wait(QMutex*, unsigned long) ()
from /opt/Qt/5.1.1/gcc_64/lib/libQt5Core.so.5
#2 0x00007f8990fb3b3e in QThread::wait(unsigned long) () from /opt/Qt/5.1.1/gcc_64/lib/libQt5Core.so.5
#3 0x00007f8990fb0402 in QThreadPoolPrivate::reset() () from /opt/Qt/5.1.1/gcc_64/lib/libQt5Core.so.5
#4 0x00007f8990fb0561 in QThreadPool::waitForDone(int) () from /opt/Qt/5.1.1/gcc_64/lib/libQt5Core.so.5
#5 0x00007f89911a4261 in QMetaObject::activate(QObject*, int, int, void**) ()
from /opt/Qt/5.1.1/gcc_64/lib/libQt5Core.so.5
#6 0x00007f89911a4d5f in QObject::destroyed(QObject*) () from /opt/Qt/5.1.1/gcc_64/lib/libQt5Core.so.5
#7 0x00007f89911aa3ee in QObject::~QObject() () from /opt/Qt/5.1.1/gcc_64/lib/libQt5Core.so.5
#8 0x0000000000409d8b in main (argc=1, argv=0x7fffba44c8f8) at ../../src/main.cpp:27
(gdb)

更新:

我注释掉了我的2个线程,所以现在只有主线程在运行,问题是一样的。

是否有一种特殊的方法可以使后台进程退出?为什么主线程不会关闭?


更新:已解决 - Qt 不喜欢 Fork。 (参见另一个 StackExchange 问题)。我必须将 fork 移动到最高级别(在 Qt 执行任何操作之前),然后 Qt 不会在退出时挂起。

最佳答案

PROCESS STATE CODES

Here are the different values that the s, stat and state output specifiers (header "STAT" or "S") will display to describe the state of a process:

  D    uninterruptible sleep (usually IO)
R running or runnable (on run queue)
S interruptible sleep (waiting for an event to complete)
T stopped, either by a job control signal or because it is being traced.
W paging (not valid since the 2.6.xx kernel)
X dead (should never be seen)
Z defunct ("zombie") process, terminated but not reaped by its parent.

For BSD formats and when the stat keyword is used, additional characters may be displayed:

  <    high-priority (not nice to other users)
N low-priority (nice to other users)
L has pages locked into memory (for real-time and custom IO)
s is a session leader
l is multi-threaded (using CLONE_THREAD, like NPTL pthreads do)
+ is in the foreground process group.

所以你的进程都处于“S: interruptible sleep”。也就是说,它们都在等待阻塞系统调用。

您可能会从以下命令中更好地了解您的程序正在等待什么:

$ ps -o pid,stat,wchan `pidof zsh`
PID STAT WCHAN
4490 Ss rt_sigsuspend
4814 Ss rt_sigsuspend
4861 Ss rt_sigsuspend
4894 Ss+ n_tty_read
5744 Ss+ n_tty_read
...

“wchan(等待 channel )”显示阻塞的内核函数(=~ 系统调用)。

另见

关于c++ - QThreaded C++ 守护程序未正常关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21979014/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com