#include <iostream>
#include <stdio.h>
#include <vector>
#include <unistd.h>
#include <sys/types.h>
//execute Shell commands
//Executes a shell command, the output is stored line by row in Resvec and returns the number of rows
int32_t myexec (const char *CMD, std:: Vector<std::string> &resvec) {
resvec.clear ();
FILE *pp = popen (cmd, "R"); Set up Pipeline
if (!pp) {
return-1;
}
char tmp[1024];//set an appropriate length to store each line of output
while (fgets ( TMP, sizeof (TMP), pp)! = NULL) {
if (Tmp[strlen (TMP)-1] = = ' \ n ') {
&nbs p; Tmp[strlen (TMP)-1] = ' + '; Remove line break
}
Resvec.push_back (TMP);
}
pclose (PP);//close pipe
return resvec.size ();
}
int main (int argc, const char * argv[]) {
std::vector<std::string> *vect = new std::vector<std::string> ();
pid_t pid = Getpid ();
Char *cmd = new char[1024];
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;
int i = 0;
int count = Vect->size ();
for (; i < count; i++) {
std::cout<< (*vect) [i] <<std::endl;
}
return 0;
}
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 returned
int32_t Myexec (ConstChar *cmd, STD::VECTOR<STD::String> &Resvec) {
Resvec.clear ();
FILE *pp = Popen (cmd,"R");//Build Pipelines
if (!PP) {
Return-1;
}
Char tmp[1024];//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 ();
}
int main (int argc,Constchar *Argv[]) {
STD::VECTOR<STD::String> *vect =New STD::VECTOR<STD::String>();
pid_t PID =Getpid ();
Char *cmd =Newchar[1024];
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;
int i = 0;
int count = vect->size ();
For (; i < count; i++) {
std::cout<< (*vect) [i] <<Std::endl;
}
return 0;
}
C + + uses shell commands