簡體   English   中英

使用 pid linux 制作一個顯示 CPU(%) 和內存使用率 (kB) 的程序

[英]Making a program that shows CPU(%) and Memory usage(kB) using pid linux

我正在嘗試創建一個程序來獲取 CPU(%) 和內存(kB) 使用情況,以在 Ubuntu 終端上顯示它。 我一直在尋找一些向我展示它的命令,我得到了;

ps -p <pid> -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) *
}

所以,我試圖將程序分成 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) */

}

我用過ps -p <pid> -o %cpu | grep -v %CPU ps -p <pid> -o %cpu | grep -v %CPU獲取 CPU(%) 使用率。

計算內存(kB)使用量: pmap -x <pid> " | grep total | awk '{print $3}'"

在這種情況下,我使用awk '{print $3}'從命令pmap -x <pid>打印第三列,並使用grep total僅打印我需要的行。

有兩種方法可以運行此代碼:

    1. ./filename cpu ---> 只會“強制”你的 CPU
    1. ./filename cpu-mem ---> 要使用你的內存,它可能會導致你的電腦崩潰。

這是一個很好的練習,可以了解 CPU 和內存的工作原理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM