#include <iostream>#include<stdio.h>#include<vector>#include<unistd.h>#include<sys/types.h>//Execute shell Command//executes a shell command, the output is stored line by row in Resvec, and the number of rows is returnedint32_t Myexec (Const Char*cmd, STD::VECTOR<STD::string> &Resvec) {resvec.clear (); FILE*pp = Popen (cmd,"R");//Build Pipelines if(!PP) { return-1; } Chartmp[1024x768];//set an appropriate length to store each line of output while(Fgets (TMP,sizeof(TMP), pp)! =NULL) { if(Tmp[strlen (TMP)-1] =='\ n') {Tmp[strlen (TMP)-1] =' /';//Remove line break} resvec.push_back (TMP); } pclose (PP); //Close Pipe returnresvec.size ();}intMainintargcConst Char*argv[]) {std::vector<STD::string> *vect =NewSTD::VECTOR<STD::string>(); pid_t PID=Getpid (); Char*cmd =New Char[1024x768]; sprintf (cmd,"ps-p%d-o ' Pid,pcpu,rss '", PID); Std::cout<< cmd <<Std::endl; int32_t a= Myexec (cmd, *vect); Std::cout<< a <<Std::endl; inti =0; intCount = vect->size (); for(; i < count; i++) {Std::cout<< (*vect) [i] <<Std::endl; } return 0;}
C + + uses shell commands