get active process list in linux

In HP-UX, you can get a list of active processes and their relevant information by calling pstat_getproc(). How can this be achieved in linux using c/c++? I.e. how can i get a list of current running processes?

It seems that one way to do it is: read the /proc and go to all /proc/xxxx/staus(xxxx are digits) and check if xxxx is in running state, if yes, count the process as active process and push it to a list. I wonder if there is other simple way and can somebody show me an example. Thanks.
Who is Participating?
sunnycoderConnect With a Mentor Commented:
#define BUF_LEN 256

FILE * ptr;
char buffer [BUF_LEN];

ptr = popen ("ps -a", "r");
while ( fgets (buffer, BUF_LEN,ptr)
         printf ("%s\n",buffer);

Add error checking as required.

If you do not wish to use these APIs to launch ps command and wish to do this in code, then look into code for ps command here
You can launch "ps -a" command using exec() or system() or popen()
No linux requires You to scan /proc
And ps in fact reads /proc/[PID]/stat
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Even if ps reads from proc it saves significant programming effort to call ps rather than duplicate the functionality in own program!!
rongzAuthor Commented:
By the way, In HP-UX, ticks of cpu time of a process can be get from
pst_status structure filled when calling pstat_getproc().
How in linux get the ticks of cpu time of a process. Thanks.

utime and stime entries in /proc/pid/stat ...
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.