[英]How to check if a process is running in C++?
我用 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;
}
在C中,我使用kill(pid_t pid, int sig)
将空白信号(0)发送到特定的pid,因此检查它是否在返回此函数时处于活动状态。
我不知道你是否在C ++中有类似的功能,但因为你也在使用linux,所以可能值得检查信号 。
摘自http://proswdev.blogspot.jp/2012/02/get-process-id-by-name-in-linux-using-c.html
在执行流程之前,将流程名称传递给此函数。 如果getProcIdByName()返回-1,则可以自由运行process_name。 如果返回有效的pid,那么,什么也不做,或者从软件中删除并运行它,取决于您的需求。
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <vector>
#include <string>
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
int getProcIdByName(string procName)
{
int pid = -1;
// Open the /proc directory
DIR *dp = opendir("/proc");
if (dp != NULL)
{
// Enumerate all entries in directory until process found
struct dirent *dirp;
while (pid < 0 && (dirp = readdir(dp)))
{
// Skip non-numeric entries
int id = atoi(dirp->d_name);
if (id > 0)
{
// Read contents of virtual /proc/{pid}/cmdline file
string cmdPath = string("/proc/") + dirp->d_name + "/cmdline";
ifstream cmdFile(cmdPath.c_str());
string cmdLine;
getline(cmdFile, cmdLine);
if (!cmdLine.empty())
{
// Keep first cmdline item which contains the program path
size_t pos = cmdLine.find('\0');
if (pos != string::npos)
cmdLine = cmdLine.substr(0, pos);
// Keep program name only, removing the path
pos = cmdLine.rfind('/');
if (pos != string::npos)
cmdLine = cmdLine.substr(pos + 1);
// Compare against requested process name
if (procName == cmdLine)
pid = id;
}
}
}
}
closedir(dp);
return pid;
}
int main(int argc, char* argv[])
{
// Fancy command line processing skipped for brevity
int pid = getProcIdByName(argv[1]);
cout << "pid: " << pid << endl;
return 0;
}
使用kill(pid, sig)
但检查errno
状态。 如果您以不同的用户身份运行并且您无权访问该进程,它将因EPERM而失败,但该进程仍处于活动状态。 您应该检查ESRCH这意味着No such process
。
如果您正在运行子进程kill 将成功,直到调用waitpid
强制清理任何已失效的进程。
这是一个函数,无论进程是否仍在运行,它都会返回 true 并处理清理已失效的进程。
bool IsProcessAlive(int ProcessId)
{
// Wait for child process, this should clean up defunct processes
waitpid(ProcessId, nullptr, WNOHANG);
// kill failed let's see why..
if (kill(ProcessId, 0) == -1)
{
// First of all kill may fail with EPERM if we run as a different user and we have no access, so let's make sure the errno is ESRCH (Process not found!)
if (errno != ESRCH)
{
return true;
}
return false;
}
// If kill didn't fail the process is still running
return true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.