LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Process Enumeration with C++ (https://www.linuxquestions.org/questions/programming-9/process-enumeration-with-c-190611/)

amitrawal_17 06-07-2004 05:05 AM

Process Enumeration with C++
 
hello ,everybody,

i am stuck.. i m new to Linux and know nothing abou it.....
i want to know following things with C++ program on Linux....

i would be gien list of processes...like
textpad
apache server cte... and i have to fetch the followings......

Process is runnin or not/???
Process related Info - memoru consumed..cpu uasage ..No of threads runnning..etc...

i am writting a C++ application ..so plz give me related help...
i know three are linux command which gives hese info like PS but how to use them in C++ code...how can i capture the out put of such commands ...or is there another way like we havein Windows platform, called WIN-API...
plzgive me C++ COde it would be a greatr help for me...
really expecfting some quick answers...i m really really really in hurry and badly stuck...

regs
Ammit

jim mcnamara 06-07-2004 11:22 AM

popen() will let you execute a command and capture the output via a redirected pipe.

Code:

FILE *fd;
char cmdoutput[496]={'\0'};
fd=popen("ps -ef", "r");
while(1){
        if(fgets(cmdoutput,4095,fd)!=NULL){
                  /* cmdoutput  has a line of output */
        } else{
                if(feof(fd)) break;
                perror("Pipe processing error");
                exit(EXIT_FAILURE);
        }
 }
 pclose(fd);


amitrawal_17 06-08-2004 01:16 AM

sorry to bug u again but i m still facing the problem.i m very new to linux and programming . i fired the samplecode given by you and it returns the following result..


OUTPUT
***********************************************************
EXECUTING:
/home/amit/Projects/Try/src/try
----------------------------------------------


amit 18186 18185 0 11:47 pts/3 00:00:00 ps -ef


----------------------------------------------
Program exited successfully with errcode (0)
Press the Enter key to close this terminal ...

******************************************************************


even if i try changing the command it is not helping me i have fired ps -aux too but the result is the same...
i m confused.....
my inputs would be process name only and i have to find out everything about the process then...
like
CPU usgae
No of THreads Running
Memory Usage etc...

plz help me out.....

best regs
Ammit


All times are GMT -5. The time now is 10:25 PM.