views:

538

answers:

4

Hi,

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

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 :)

+2  A: 

I know only two ways:

  • Parsing output of the ps command
  • Reading files in /proc/PID, where PID is the process identifier (that's what ps does internally)
Eugene Morozov
i think you should clarify a bit
simplyharsh
A: 

Found here:

Use this command to display all of your zombie processes:

ps aux | awk '{ print $8 " " $2 }' | grep -w Z

This could be easily parsed using C.

schnaader
+1  A: 

You want the processes running on your machine then use

$ ps aux

ps displays information about a selection of the active processes. If you want a repetitive update of the selection and the displayed information, use top instead.

aatifh
yeah i guess TOP is a good idea. just need to be parsed in C.
simplyharsh
I think "using C" means in a c program (i.e. not at the command prompt), and "under Linux" tells you what OS APIs he has access to.
dmckee
@dmckee hehe I know that dude. :)
aatifh
@taurean correct
aatifh
+4  A: 

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!)

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.

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.

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.

Edit: 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. :)

Jason L
It's a nice discussion of the task pitched for a less sophisticated audience
dmckee
Your answer lead me to rethinking my approach of fixing a bug where the parent did not wait for the children properly. I reanalyzed the code and found the point where the parent missed to wait for the children.Thanks
Tilo Prütz