Solved

get active process list in linux

Posted on 2006-10-20
6
3,705 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

813 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