[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Read Linux Process in C/C++

Posted on 2009-05-09
5
Medium Priority
?
1,242 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 1500 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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

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…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

656 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