I am trying to figure out the state of processes using C (running, sleeping, or exited), in a linux environment.
My thoughts on how to do this initially were to call execv() with the command ps 12345 where 12345 would be the process ID, and then parse the output of that in order to get the STAT. However, I don’t think that I can get the output of that into my program, as it just outputs automatically (or can I?).
I was also thinking that I could send the process a signal, but I haven’t found a good way to do this, and I don’t even know if it is possible to determine the process state in this manner.
So, my question is, how do I determine the state of a process in C?
In linux there’s a file of space-separated values called
/proc/[pid]/stat. The third value is the process state.For example:
Alternatively, there’s also a
/proc/[pid]/statusfile:I would just use
fscanfonstat.