我用 C++ 编写了一个程序,将文件中的进程读取到向量中,然后逐行执行进程。
我想通过在 c++ 中使用 proc 来找出哪些进程正在运行,哪些没有
谢谢。
我的代码:
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <iterator>
#include <cstdlib>
using namespace std;
int main()
{ int i,j;
std::string line_;
std::vector<std::string> process;
ifstream file_("process.sh");
if(file_.is_open())
{
while(getline(file_,line_))
{
process.push_back(line_);
}
file_.close();
}
else{
std::cout<<"failed to open"<< "\n";
}
for (std::vector<string>::const_iterator i = process.begin(); i != process.end(); ++i)
{
std::cout << *i << ' ';
std::cout << "\n";
}
for (unsigned j=0; j<process.size(); ++j)
{
string system=("*process[j]");
std::string temp;
temp = process[j];
std::system(temp.c_str());
std::cout << " ";
}
std::cin.get();
return 0;
}
原文由 Saurabh Jadhav 发布,翻译遵循 CC BY-SA 4.0 许可协议
取自 http://proswdev.blogspot.jp/2012/02/get-process-id-by-name-in-linux-using-c.html
在执行您的流程之前,将流程名称传递给此函数。如果 getProcIdByName() 返回 -1,您可以自由运行 process_name。如果返回有效的 pid,那么,什么也不做,或者从您的软件中杀死并运行它,这取决于您的需要。