使用pid linux

时间:2018-10-03 19:57:04

标签: c linux performance

我正在尝试创建一个程序,该程序获取CPU(%)和内存(kB)的使用情况以在Ubuntu Terminal上显示该程序。 我一直在寻找一些显示给我看的命令,我明白了;

ps -p <pid> -o %cpu,%mem

当我直接在终端上对其进行测试时,它可以正常工作。但是在我的程序上,它给了我这个错误: error: garbage option

这是我的代码:

#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) *
}

因此,我试图将程序分为10个步骤。每个人都要执行命令。步骤之间只有一秒钟

如何使此代码起作用?还有其他我可以用来替代的命令吗?

1 个答案:

答案 0 :(得分:2)

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

#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) */

}

我已经使用ps -p <pid> -o %cpu | grep -v %CPU来获取CPU(%)使用率。

要计算内存(kB)使用情况:pmap -x <pid> " | grep total | awk '{print $3}'"

在这种情况下,我使用awk '{print $3}'打印来自Comand pmap -x <pid>grep total的第三列,仅打印我需要的行。

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

    1. ./文件名cpu --->仅“强制” CPU
    1. ./ filename cpu-mem --->要使用您的RAM,它可能会使您的PC崩溃。

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