Linux C++中獲取指定pid、tid的CPU與內存佔用信息

 使用的是ps命令+popen函數,直接上代碼

 

  1. #include<iostream> 
  2. using namespace std; 
  3. #define _LINE_LENGTH 300 
  4.  
  5.  
  6. bool GetCpuMem(float &cpu,size_t &mem, int pid,int tid = -1) 
  7.     bool ret = false
  8.     char cmdline[100]; 
  9.     sprintf(cmdline, "ps -o %%cpu,rss,%%mem,pid,tid -mp %d", pid); 
  10.     FILE *file; 
  11.     file = popen(cmdline, "r"); 
  12.     if (file == NULL)  
  13.     { 
  14.         printf("file == NULL\n"); 
  15.         return false
  16.     } 
  17.  
  18.     char line[_LINE_LENGTH]; 
  19.     float l_cpuPrec=0; 
  20.     int l_mem=0; 
  21.     float l_memPrec=0; 
  22.     int l_pid=0; 
  23.     int l_tid=0; 
  24.     if (fgets(line, _LINE_LENGTH, file) != NULL)  
  25.     { 
  26.     //  printf("1st line:%s",line); 
  27.         if (fgets(line, _LINE_LENGTH, file) != NULL)  
  28.         { 
  29.     //      printf("2nd line:%s",line); 
  30.             sscanf( line, "%f %d %f %d -", &l_cpuPrec, &l_mem, &l_memPrec, &l_pid ); 
  31.             cpu = l_cpuPrec; 
  32.             mem = l_mem/1024; 
  33.             if( tid == -1 ) 
  34.                 ret = true
  35.             else 
  36.             { 
  37.                 while( fgets(line, _LINE_LENGTH, file) != NULL ) 
  38.                 { 
  39.                     sscanf( line, "%f - - - %d", &l_cpuPrec, &l_tid ); 
  40.     //              printf("other line:%s",line); 
  41.     //              cout<<l_cpuPrec<<'\t'<<l_tid<<endl; 
  42.                     if( l_tid == tid ) 
  43.                     { 
  44.                         printf("cpuVal is tid:%d\n",tid); 
  45.                         cpu = l_cpuPrec; 
  46.                         ret = true
  47.                         break
  48.                     } 
  49.                 } 
  50.                 if( l_tid != tid ) 
  51.                     printf("TID not exist\n"); 
  52.             } 
  53.         } 
  54.         else 
  55.             printf("PID not exist\n"); 
  56.     } 
  57.     else 
  58.         printf("Command or Parameter wrong\n"); 
  59.     pclose(file); 
  60.     return ret; 
  61.  
  62. int main(int argc, char** argv) 
  63.     float cpu=0; 
  64.     size_t mem=0; 
  65.     int pid=0; 
  66.     int tid=-1; 
  67.     if( argc > 1 ) 
  68.         pid = atoi(argv[1]); 
  69.     else 
  70.         pid = getpid(); 
  71.     if( argc > 2 ) 
  72.         tid = atoi(argv[2]); 
  73.     while(1) 
  74.     { 
  75.         if( GetCpuMem( cpu, mem, pid, tid ) ) 
  76.         { 
  77.             printf("%%CPU:%.1f\tMEM:%dMB\n", cpu, mem); 
  78.         } 
  79.         else 
  80.             printf("return false\n"); 
  81.         break
  82.         sleep(5); 
  83.     } 
  84.  
  85.     return 0; 

編譯:g++ GetInfo.cpp -o GetInfo -g

測試命令:

./GetInfo pid

顯示pid進程cpu及內存信息

./GetInfo pid tid

顯示pid進程的內存佔用和其中tid線程的cpu佔用信息

./GetInfo 什麼參數都不輸入顯示本進程cpu及內存信息(有點雞肋)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章