使用pid linux制作显示CPU(%)和内存使用率(kB)的程序

使用pid linux制作显示CPU(%)和内存使用率(kB)的程序,c,linux,performance,C,Linux,Performance,我正在尝试创建一个程序,获取CPU(%)和内存(kB)的使用率,以便在Ubuntu终端上显示它。 我一直在寻找某个命令来显示它,我得到了它 ps -p <pid> -o %cpu,%mem ps-p-o%cpu,%mem 当我直接在终端上测试它时,它工作得很好。但在我的程序中,它给了我一个错误: 这是我的密码: #include <unistd.h> #include<time.h> #include <stdio.h> #include &

我正在尝试创建一个程序,获取CPU(%)和内存(kB)的使用率,以便在Ubuntu终端上显示它。 我一直在寻找某个命令来显示它,我得到了它

ps -p <pid> -o %cpu,%mem
ps-p-o%cpu,%mem
当我直接在终端上测试它时,它工作得很好。但在我的程序中,它给了我一个错误:

这是我的密码:

#include <unistd.h>
#include<time.h>
#include <stdio.h>
#include <stdlib.h>
#include<string.h>

int main (int argc, char *argv[], char *envp[]) {
   int pid ; /* Process ID */
   char usage[150];
   char kill[50];
   pid = fork () ; /* Process reaplication */
   sprintf(usage,"%s%d%s","ps -p ", pid,  " %cpu,%mem");
   sprintf(kill, "%s%d", "kill -9 ", pid);

   if ( pid < 0 ) { /*if fork do not work*/
       perror ("Erro: ") ;
       exit (-1) ; /* Ends process with error: -1 */
   } else if( pid > 0 ) { /* If i'm parent process*/
       for(int i=0;i<10;i++) {
          system("clear");
          printf("Processing (%ds)\n", i);
          int aux;
          for(aux=i;aux>0;aux--) {
              printf("=");
          }
          printf("=\n");
          system(usage);
          sleep(1);
       }
       system(kill);
   } else /* senão, sou o processo filho (pid == 0) { */
      if(strcmp(argv[1],"cpu")==0) { /* if argv[1] = cpu -> Execute code using intese cpu*/
         for(;;){}
      }
      if(strcmp(argv[1],"cpu-mem")) { /* if argv[1] = cpu-mem -> Execute code using intese cpu and memory */
         int moment = clock();
         for (;;) {
            while(clock() - moment < 5){} /* makes mem use less intense  */
            malloc(sizeof(1000));
         }
      }
   }
   perror ("Erro: ") ; /* if do not work */
   printf ("Tchau !\n") ;
   exit(0) ; /* Ends process with success (código 0) *
}
#包括
#包括
#包括
#包括
#包括
int main(int argc,char*argv[],char*envp[]{
int pid;/*进程ID*/
字符用法[150];
杀炭[50];
pid=fork();/*进程重新应用*/
sprintf(使用率,“%s%d%s”、“ps-p”、pid、“%cpu,%mem”);
sprintf(kill,“%s%d”,“kill-9”,pid);
if(pid<0){/*if-fork不工作*/
佩罗尔(“埃罗:”);
退出(-1);/*以错误结束进程:-1*/
}else如果(pid>0){/*如果我是父进程*/
对于(int i=0;i0;aux--){
printf(“=”);
}
printf(“=\n”);
系统(使用);
睡眠(1);
}
系统(kill);
}else/*senão,sou o processo filho(pid==0){*/
if(strcmp(argv[1],“cpu”)==0{/*if argv[1]=cpu->使用intese cpu执行代码*/
对于(;){}
}
if(strcmp(argv[1],“cpu mem”){/*if argv[1]=cpu mem->使用intese cpu和内存执行代码*/
int矩=时钟();
对于(;;){
而(clock()-moment<5){}/*使mem的使用强度降低*/
马洛克(sizeof(1000));
}
}
}
佩罗(“错误:);/*如果不起作用*/
printf(“Tchau!\n”);
退出(0);/*以成功结束过程(código 0)*
}
所以,我试图将程序分为10个步骤。每个人都要执行命令。两个步骤之间有一秒钟的时间


我怎样才能让这个代码工作?有没有其他命令可以替代这个命令?

已经解决了。我就是这样做的:

#include <unistd.h>
#include<time.h>
#include <stdio.h>
#include <stdlib.h>
#include<string.h>

int main (int argc, char *argv[], char *envp[]) {
int pid ; /* Process ID */
char mem_usage[50];
char cpu_usage[50];
char kill[50];
pid = fork () ; /* Process reaplication */
sprintf(cpu_usage,"%s%d%s","ps -p ", pid, " -o %cpu | grep -v %CPU");
sprintf(mem_usage, "%s%d%s", "pmap -x ", pid," | grep total | awk '{print $3}'");
sprintf(kill, "%s%d", "kill -9 ", pid);

if ( pid < 0 ) { /*if fork do not work*/
    perror ("Erro: ") ;
    exit (-1) ; /* Ends process with error: -1 */
}
else if( pid > 0 ) /* If i'm parent process*/
{
    for(int i=0;i<10;i++)
  {
    system("clear");
    if(i == 0 || i == 3 || i == 6 || i == 9)
    {
    printf("Processing.\n");
    }
    else if(i == 1 || i == 4 || i == 7)
    {
      printf("Processing..\n");
    }
    else if(i == 2 || i == 5 || i == 8)
    {
      printf("Processing...\n");
    }
    printf("%ds\n", i+1);
    printf("================\n");
    printf("CPU(%%)\n");
    system(cpu_usage);
    printf("MEM(kB)\n");
    system(mem_usage);
    sleep(1);
  }
  system(kill);
}
else /* senão, sou o processo filho (pid == 0) */
{
  if(strcmp(argv[1],"cpu")==0) /* if argv[1] = cpu -> Execute code using intese cpu*/
  {
    for(;;){}
  }
  if(strcmp(argv[1],"cpu-mem")==0) /* if argv[1] = cpu-mem -> Execute code using intese cpu and memory */
  {
    int moment = clock();
    for (;;) {
        sleep(0.001); /* makes mem use less intense  */
    malloc(50* sizeof(int));
}
  }

}
perror ("Erro: ") ; /* if do not work */

printf ("Tchau !\n") ;
exit(0) ; /* Ends process with success (código 0) */

}
#包括
#包括
#包括
#包括
#包括
int main(int argc,char*argv[],char*envp[]{
int pid;/*进程ID*/
char mem_用法[50];
字符cpu_使用率[50];
杀炭[50];
pid=fork();/*进程重新应用*/
sprintf(cpu使用率,“%s%d%s”,“ps-p”,pid,“-o%cpu | grep-v%cpu”);
sprintf(mem_用法,“%s%d%s”,“pmap-x”,pid,“| grep total | awk'{print$3}”;
sprintf(kill,“%s%d”,“kill-9”,pid);
if(pid<0){/*if-fork不工作*/
佩罗尔(“埃罗:”);
退出(-1);/*以错误结束进程:-1*/
}
如果(pid>0)/*如果我是父进程*/
{
对于(int i=0;我使用intese cpu执行代码*/
{
对于(;){}
}
if(strcmp(argv[1],“cpu mem”)==0)/*if argv[1]=cpu mem->使用intese cpu和内存执行代码*/
{
int矩=时钟();
对于(;;){
睡眠(0.001);/*使mem的使用强度降低*/
malloc(50*sizeof(int));
}
}
}
perror(“Erro:”);/*如果不起作用*/
printf(“Tchau!\n”);
退出(0);/*成功结束进程(código 0)*/
}
我已经使用了
ps-p-o%cpu | grep-v%cpu
来获取cpu使用率(%)

要计算内存(kB)使用量:
pmap-x“| grep total | awk'{print$3}'”

在本例中,我使用了
awk'{print$3}'
从Com打印第三列,并
pmap-x
grep total
仅打印我需要的行

有两种方法可以运行此代码:

  • ./filename cpu-->将仅“强制”您的cpu
  • ./filename cpu mem->如果要使用RAM,可能会使电脑崩溃

这是学习CPU和内存如何工作的一个很好的练习。

提示:尝试使用
strace ps-p-o%CPU,%mem
查看syscalls
ps
用于获取所需信息的功能。