Solved

Read Linux Process in C/C++

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 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

15 Experts available now in Live!

Get 1:1 Help Now