Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   cpu usage of a single process (http://www.go4expert.com/forums/cpu-usage-single-process-t5470/)

sandyk 26Jul2007 09:30

cpu usage of a single process
 
this is the c code to get the pids and %cpu n %mem in linux/unix


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);

in the above code:
if i give input as pid of a process, how can i get the %cpu and mem of that process only? :confused:

can someone gimme the code to it? :rolleyes:

thanks in advance :)

pradeep 26Jul2007 10:23

Re: cpu usage of a single process
 
Code:

ps -eo pid,pcpu,pmem | awk '{if ($1==<pid_here>) print "PID\tCPU\tMEM\n" $1 "\t"  $2 "\t"  $3}'

#example
ps -eo pid,pcpu,pmem | awk '{if ($1==1389) print "PID\tCPU\tMEM\n" $1 "\t"  $2 "\t"  $3}'

# output
PID    CPU    MEM
1389    0.0    0.4


sandyk 26Jul2007 10:41

Re: cpu usage of a single process
 
hii i got that in shell scripting...


how do i do that in c code??

the c code which i have given has to work in linux.
i want to modify the code so that when given input by user->pid of a process, it shud give the %cpu and %mem...
like:

printf("enter the pid:);
scanf("%d",pid);

....then i'm stuck here...

sandyk 26Jul2007 10:46

Re: cpu usage of a single process
 
[QUOTE=sandyk]
printf("enter the pid:");
QUOTE]

typo

pradeep 26Jul2007 10:51

Re: cpu usage of a single process
 
Code: C

FILE *fp;
  int p;
  printf('Enter pid: ');
  scanf("%d",p);
 
  char pid[MAX]=sprintf("ps -eo pid,pcpu,pmem | awk '{if ($1==%d) print \"PID\\tCPU\\tMEM\\n\" $1 \"\\t\"  $2 \"\\t\"  $3}'",p);
  fp=popen(pid,"r");
  while (fgets(pid, MAX, fp)!=NULL)
  printf("%s",pid);
  pclose(fp);


sandyk 26Jul2007 11:05

Re: cpu usage of a single process
 
i'm getting the following errors for the line with sprintf


over.c:11: warning: passing arg 2 of `sprintf' makes pointer from integer without a cast
over.c:11: error: invalid initializer

can yu please tell me why is that?

pradeep 26Jul2007 11:31

Re: cpu usage of a single process
 
Sorry for the blunder,

Code: C

//this
 scanf("%d",p);
// should be
  scanf("%d",&p);


sandyk 26Jul2007 11:38

Re: cpu usage of a single process
 
ohh i got that..
but sprintf is not working as in that is the thing which is showing errors.

sandyk 26Jul2007 13:40

Re: cpu usage of a single process
 
Code:

#include<stdio.h>
#include<stdlib.h>
#define MAX 1024

main()
{
FILE *fp;
int p,i;
char path[MAX]="ps -eo pid,pcpu,pmem";

//popen executes the command in the string

fp=popen(path,"r");

printf("Enter the pid:");
scanf("%d",&p);

while (fgets(path, MAX, fp)!=NULL)
{
//check whether the pid is there or not

for(i=0;i<=MAX;i++)
{
if (path[i] == p)
printf("%s",pid);//print the cpu n mem of that pid
}
fflush(fp);
}
pclose(fp);
}


input=pid of a process
output=its corresponding %cpu and %mem

but this is not working...someone please help me compile it.

pradeep 26Jul2007 13:50

Re: cpu usage of a single process
 
Code: C

#include <stdio.h>
#define MAX 50

int main(void)
{
  FILE *fp;
  int p;
  printf("Enter pid: ");
  scanf("%d",&p);
 
  unsigned char pid[MAX];
  char out[25];

  sprintf(pid,"ps -eo pid,pcpu,pmem | awk '{if ($1==%d) print \"PID\\tCPU\\tMEM\\n\" $1 \"\\t\"  $2 \"\\t\"  $3}'",p);

  fp=popen(pid,"r");
  while (fgets(out, MAX, fp)!=NULL)
    puts(out);

  pclose(fp);

    return 0;
}



All times are GMT +5.5. The time now is 17:15.