LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   getting the cpu usage of a single process (https://www.linuxquestions.org/questions/linux-newbie-8/getting-the-cpu-usage-of-a-single-process-572147/)

kits123 07-25-2007 07:50 AM

getting the cpu usage of a single process
 
this is the code to get the pids and %cpu n %mem


Code:

FILE *fp;
char pid[MAX]="ps -eo pid,pcpu,pmem";
fp=popen(pid,"r");
while (fgets(pid, MAX, fp)!=NULL)
printf("%s",pid);
pclose(fp);


if i give input -> pid of a process, how can i get the %cpu and mem of that process only?:confused:
:scratch:
can someone gimme the code to it?:rolleyes:

thanks in advance

:newbie:

chrism01 07-25-2007 08:03 PM

ps -eo pid,pcpu,pmem|grep <wanted_pid>

kits123 07-25-2007 10:50 PM

Quote:

Originally Posted by chrism01
ps -eo pid,pcpu,pmem|grep <wanted_pid>


hii that thing works when given at command prompt right?

but it wont work like this:--

Code:

scanf("%d", pid);
 char path[MAX]= "ps -eo pid,pcpu,pmem | grep $pid";

i know that wrong...
could yu correct it please?
i want the user to give a pid as in input and the give him the %cpu and %mem as output...
could yu please gimme the code...:confused:


All times are GMT -5. The time now is 05:42 PM.