Solved

Read Linux Process in C/C++

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

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 fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

861 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