Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect hung processes in Linux using C? [duplicate]

Tags:

c

linux

Possible Duplicate:
Linux API to list running processes?

How can I detect hung processes in Linux using C?

like image 694
systemsfault Avatar asked Jan 23 '23 12:01

systemsfault


2 Answers

Under linux the way to do this is by examining the contents of /proc/[PID]/* a good one-stop location would be /proc/*/status. Its first two lines are:

Name: [program name] State: R (running)

Of course, detecting hung processes is an entirely separate issue.

/proc//stat is a more machine-readable format of the same info as /proc//status, and is, in fact, what the ps(1) command reads to produce its output.

like image 162
caskey Avatar answered Jan 29 '23 08:01

caskey


Monitoring and/or killing a process is just a matter of system calls. I'd think the toughest part of your question would really be reliably determining that a process is "hung", rather than meerly very busy (or waiting for a temporary condition).

In the general case, I'd think this would be rather difficult. Even Windows asks for a decision from the user when it thinks a program might be "hung" (on my system it is often wrong about that, too).

However, if you have a specific program that likes to hang in a specific way, I'd think you ought to be able to reliably detect that.

like image 37
T.E.D. Avatar answered Jan 29 '23 06:01

T.E.D.