Solved

get active process list in linux

Posted on 2006-10-20
6
3,700 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

760 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now