如何确定一个进程的状态(即如果它是一个僵尸) [英] How to determine the state of a process (i.e. if it is a zombie)

查看:156
本文介绍了如何确定一个进程的状态(即如果它是一个僵尸)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能得到一个进程的状态(即如果它是一个僵尸)使用C Linux下?

how can I get information on the state of a process (i.e. if it is a zombie) using C under Linux?

阅读的答案到目前为止,我想有点窄我的问题后:我想preFER一个纯C的解决方案。阅读PS源(其内容的/ proc /)后,我认为应该有一个更好的办法,并在这里问:)

After reading the answers so far I want to narrow my question somewhat: I would prefer a pure C solution. After reading the ps source (which reads /proc/) I thought that there should be a better way and asked here :)

推荐答案

您会希望了解如何使用在/ proc /伪文件系统通过典型的C标准库调用交互。上手所需的文档包含任何Linux发行版,是一个简单的谷歌搜索了。

You'll want to learn about interacting with the /proc/ "psuedo-filesystem" via typical C standard library calls. The documentation necessary to get started is included with any Linux distro and is a simple google search away.

(现在你知道该怎么寻找。我知道,最有挑战的一般!)

(Now that you know what to search for. I know that's usually most of the challenge!)

总之,一个运行Linux系统在/ proc /目录中的目录和文件反映了正在运行的内核,它(自然),包括进程的状态。但是,在你所充电前必须牢记的一些信息。

In short, the directories and files within the /proc/ directory of a running Linux system reflect the state of the running kernel, which (naturally) includes processes. However, before you charge in you need to keep some information in mind.

一个僵尸进程是不一样的事,作为一个孤立的过程。一个孤立的过程是在这个过程中父已退出后,不正确的等待状态,运行留下了一个过程。一个僵尸进程是已经退出正确,发布了其所有的资源的过程,但维持进程表的地方。

A zombie process isn't the same thing as an orphaned process. An orphaned process is a process left running in a waiting state after the process' parent has exited incorrectly. A zombie process is a process which has exited properly, released all its resources, but is maintaining a place in the process table.

当一个进程是由程序启动这通常发生。你看,直到父程序正确地获取子进程的返回状态的内核将不会删除进程表中完成的子进程的条目。这就说得通了;要不然怎么会父程序知道是否退出子不当?

This typically happens when a process is launched by a program. You see, the kernel won't remove a finished sub-process' entry in the process table until the parent program properly fetches the return status of the sub-process. That makes sense; how else would the parent program know if the subprocess exited improperly?

因此​​,所有的子进程在技术上是用于至少一个很短的时间僵尸。这是不是天生坏的状态,一个程序是在

So all subprocesses are technically zombies for at least a very short time. It's not inherently a bad state for a program to be in.

事实上,植物大战僵尸有时故意创建的。例如,有时一个僵尸条目到位由程序一会儿左边,这样进一步启动的进程不会得到相同的PID为previously推出(现在僵尸)的过程。

Indeed, "zombies" are sometimes created intentionally. For example, sometimes a zombie entry is left in place by a program for a while so that further launched processes won't get the same PID as the previously-launched (and now zombie) process.

在换句话说,如果你去SIGCHLDing僵尸进程不必要您可以创建产卵程序的严重问题。但是,如果一个进程已经一个半小时以上的僵尸,它可能是一个错误的信号。

In other words, if you go SIGCHLDing zombie processes unnecessarily you might create a serious problem for the spawning program. However, if a process has been a zombie for a half hour or more, it's probably a sign of a bug.

编辑:这个问题对我变了!没有,有没有比PS怎么做的简单的方法。如果有,它会被集成到PS很久以前。在/ proc文件的BE-所有最终所有源对内核的状态信息。 :)

The question changed on me! No, there's no simpler way than how ps does it. If there was, it would have been integrated into ps a long time ago. The /proc files are the be-all-end-all source for information on the kernel's state. :)

这篇关于如何确定一个进程的状态(即如果它是一个僵尸)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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