Assume I spawn a process on (csh) command line, like
> du -a / >& /dev/null &
which creates a process with id 1234. Now, I can suspend/resume that process with
> kill -STOP 1234
> kill -CONT 1234
and can query the process state via 'jobs' or 'ps. How can I though query that state programatically (UNIX/POSIX system, no /proc/ available)?
The only call closest to the problem seems waitpid(), which can use WUNTRACED to get notifications on some signals, including SIGSTOP. That seems to work only for processes spawned within the same application - so I would not be able to write a tool which, for the example above, works like
> check_proc_state -p 1234
1234 is Running
Another option is to do something like
FILE* fd = popen ("ps -ewwo pid=,uid=,state=,command=", "r");
and to grep the output - but ps output is not well standardized (well, it actually is, see ps, but the various implementations do not necessarily adhere to the spec), and also this seems a very cumbersome way to do that. Not to speak of performance...
Any better ideas? The solution ideally should work on MacOS, BSD, Linux, and AIX.
Thanks, Andre.
PS.: yes, I grep'ed the forum: huge amount of related posts, and I may well have missed relevant discussions in the noise. My apologies, and thanks for any pointers...