Solved

get active process list in linux

Posted on 2006-10-20
6
3,708 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
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…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

733 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