如何检查进程是否处于挂起状态 (Linux)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3659065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if a process is in hang state (Linux)
提问by anil
Is there any command in Linux through which i can know if the process is in hang state.
Linux 中是否有任何命令可以让我知道进程是否处于挂起状态。
回答by bobince
What do you mean by ‘hang state'? Typically, a process that is unresponsive and using 100% of a CPU is stuck in an endless loop. But there's no wayto determine whether that has happened or whether the process might not eventually reach a loop exit state and carry on.
你说的“挂状态”是什么意思?通常,一个无响应并使用 100% CPU 的进程陷入无限循环。但是没有办法确定这是否已经发生,或者进程是否最终可能不会达到循环退出状态并继续进行。
Desktop hang detectors just work by sending a message to the application's event loop and seeing if there's any response. If there's not for a certain amount of time they decide the app has ‘hung'... but it's entirely possible it was just doing something complicated and will come back to life in a moment once it's done. Anyhow, that's not something you can use for any arbitrary process.
桌面挂起检测器只是通过向应用程序的事件循环发送消息并查看是否有任何响应来工作。如果在一段时间内没有他们决定应用程序已“挂起”……但它完全有可能只是在做一些复杂的事情,并且一旦完成就会恢复生机。无论如何,这不是您可以用于任何任意过程的东西。
回答by Manoj R
Unfortunately there is no hung state for a process. Now hung can be deadlock. This is block state. The threads in the process are blocked. The other things could be live lock where the process is running but doing the same thing again and again. This process is in running state. So as you can see there is no definite hung state. As suggested you can use the top command to see if the process is using 100% CPU or lot of memory.
不幸的是,进程没有挂起状态。现在挂了就可以死锁了。这是块状态。进程中的线程被阻塞。其他事情可能是进程正在运行的活锁,但一次又一次地做同样的事情。该进程处于运行状态。因此,如您所见,没有明确的挂起状态。根据建议,您可以使用 top 命令查看进程是否使用 100% CPU 或大量内存。
回答by Dummy00001
Is there any command in Linux through which i can know if the process is in hang state.
Linux 中是否有任何命令可以让我知道进程是否处于挂起状态。
There is no command, but once I had to do a very dumb hack to accomplish something similar. I wrote a Perl script which periodically (every 30 seconds in my case):
没有命令,但有一次我不得不做一个非常愚蠢的黑客来完成类似的事情。我写了一个 Perl 脚本,它周期性地(在我的例子中每 30 秒一次):
- run
ps
to find list of PIDs of the watched processes (along with exec time, etc) - loop over the PIDs
- start
gdb
attaching to the process using its PID, dumping stack trace from it usingthread apply all where
, detaching from the process - a process was declared hung if:
- its stack trace didn't change and time didn't change after 3 checks
- its stack trace didn't change and time was indicating 100% CPU load after 3 checks
- hung process was killed to give a chance for a monitoring application to restart the hung instance.
- 运行
ps
以查找被监视进程的 PID 列表(以及执行时间等) - 循环PID
- 开始
gdb
使用其 PID 附加到进程,使用 从中转储堆栈跟踪thread apply all where
,从进程中分离 - 在以下情况下,进程被宣布挂起:
- 它的堆栈跟踪没有改变,3次检查后时间也没有改变
- 在 3 次检查后,它的堆栈跟踪没有改变,时间表明 CPU 负载为 100%
- 挂起的进程被杀死,以便监控应用程序有机会重新启动挂起的实例。
But that was very very very very crude hack, done to reach an about-to-be-missed deadline and it was removed a few days later, after a fix for the buggy application was finally installed.
但这是非常非常非常粗略的 hack,为了达到即将错过的最后期限,它在几天后被删除,在最终安装了错误应用程序的修复程序之后。
Otherwise, as all other responders absolutely correctly commented, there is no way to find whether the process hung or not: simply because the hang might occur for way to many reasons, often bound to the application logic.
否则,正如所有其他响应者完全正确评论的那样,无法找到进程是否挂起:仅仅是因为挂起可能由于多种原因而发生,通常与应用程序逻辑有关。
The only way is for application itself being capable of indicating whether it is alive or not. Simplest way might be for example a periodic log message "I'm alive".
唯一的方法是应用程序本身能够指示它是否活着。最简单的方法可能是例如定期记录消息“我还活着”。
回答by RRM
you could check the files
你可以检查文件
/proc/[pid]/task/[thread ids]/status