Solved

Read Linux Process in C/C++

Posted on 2009-05-09
5
1,235 Views
Last Modified: 2012-05-06
Hi All,
I can't seem to be able to figure this one out, how do i write a code in C/C++ to read the output from the ps command to filter out the information i.e the RSS, PID in it?

0
Comment
Question by:passaged
5 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 24344797
#include <stdio.h>
main(){
  FILE *p=popen("ps -l","r");
  char PID[100],RSS[100];
  while( fscanf(p,"%*s %99s %*s %*s %*s %*s %*s %99s %*[^\n]",PID,RSS) == 2 ){
    printf("%s\t%s\n",PID,RSS);
  }
}
0
 
LVL 4

Expert Comment

by:ewest02
ID: 24345138
Just to add to ozo's suggestion... You can fine tune the output from ps with a variety of command line options as well as filtering the output via sed or some such...

    p=popen( "ps -eo pid,rss")

will return just the PID and RSS

  --Eric
0
 

Author Comment

by:passaged
ID: 24345348
Thank you to both of you for the suggestion, trying out ozo's sample code now.

Another question, how do you add the total RSS value for the output? And also count the number of STATS codes, how many R+,S+?

Thanks!
0
 
LVL 4

Accepted Solution

by:
ewest02 earned 500 total points
ID: 24350717
Basically...

 int64_t  tot_rss=0;
 int  tot_rplus=0;
 int  tot_splus;

struct stat {
    char * stat;
    int       cnt;
}


void
collectStats( char * str)
{
       a hundred ways  to do this...
       binary tree; linked list, etc For example each primary state code (D,R,S,T, etc) can act as the top of a tree. Each leaf is  auxillary code. A count can be maintained in each node...

For example, S+ and SLs+ might look like this...

       S
      /  \
    +    L
           \
             s
            /
          +
}

int  main()
{
    FILE *fp=popen("ps -eo pid,rss,statl","r");
    char pidStr[16];
    char rssStr[16];
    char statStr[16];

    while ( fscanf( fp,"%s %s %s%*[^\n]", pidStr, rssStr, statStr) == 3 )
    {
        printf("%s\t%s\t%s\n", pidStr, rssStr, statStr);
        tot_rssStr += strtoul( rssStr, (char **)NULL, 10);
        collectStats( statStr);
  }
}

Question: what is your C\C++ coding skill level?
0
 
LVL 3

Expert Comment

by:aatrish2001
ID: 24382808
#include<stdio.h>
int main()
{
system("ps -ef");
}
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…
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.
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…

786 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