Solved

Read Linux Process in C/C++

Posted on 2009-05-09
5
1,239 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
[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
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

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

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…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

724 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