思路
在系统中,根据进程名,查找进程。若存在,目标进程。则,仅保留此副本。否则,支持拉起一个新的副本。
实现
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <iostream>
#include <string.h>
void test();
int main(int argc, char * argv[]){
std::cout << "hello,world!" << std::endl;
test();
return 0;
}
void test(){
FILE * fp;
char buf[100] = {0};
memset(buf, 0, 100);
char cmd[256] = "ps x | grep my_proc_name| grep -v \"grep\"";
std::cout << cmd << std::endl;
fp = popen(cmd, "r");
if(!fp){
std::cout << "popen failed!" << std::endl;
return;
}
fgets(buf, 256, fp);
if(0 == buf[0]){
std::cout << "buf is empty" << std::endl;
}else{
std::cout << "buf is not empty" << std::endl;
}
std::cout << "buf=" << buf << std::endl;
pclose(fp);
return;
}