Solved

get active process list in linux

Posted on 2006-10-20
6
3,707 Views
Last Modified: 2009-12-16
Hi,
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.
0
Comment
Question by:rongz
  • 4
6 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 17772531
You can launch "ps -a" command using exec() or system() or popen()
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 125 total points
ID: 17772558
#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
http://procps.sourceforge.net/
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 17772595
No linux requires You to scan /proc
And ps in fact reads /proc/[PID]/stat
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 45

Expert Comment

by:sunnycoder
ID: 17772612
Even if ps reads from proc it saves significant programming effort to call ps rather than duplicate the functionality in own program!!
0
 

Author Comment

by:rongz
ID: 17773698
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.

0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 17774596
utime and stime entries in /proc/pid/stat ...
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to import linux vhd to sccvmm 1 159
shell script working with --daemon 18 96
Understanding Futures with busy waiting 2 99
Shell script for mvn build 2 76
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question