Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux - Find if process is ready or running

I'm developing a C++ application with process monitoring capabilities, which monitors state changes based on this model: http://www.macdesign.net/capella/it4813/images/stallings-Linux_process-thread_states-f4.18.png

However, as /proc/pid/status combines both the "ready" and "executing" states into "running", I'm at a loss as to how I can find out which of these states a process is actually in. Can anyone offer any suggestions as to how I may discover this?

like image 210
dutchgold92 Avatar asked Nov 03 '22 14:11

dutchgold92


1 Answers

The question is malformed. On a single CPU, it's literally never possible to see a process in the "running" state, because by definition the monitoring process has the CPU. If you really need fine-grained logging control over process transitions, you need to do it in the kernel (or at least with in-kernel support -- see lttng or systemtap for tools that might help here). Tools for tracking CPU usage from userspace are more coarse-grained, either using the total usage numbers already tracked by the kernel (e.g. /usr/bin/time) or by sampling the process table at intervals (bootchart works this way).

like image 198
Andy Ross Avatar answered Nov 15 '22 07:11

Andy Ross