如何检查进程是否在C++中运行?

How to check if a process is running in C++?

本文关键字:C++ 运行 是否 进程 何检查 检查      更新时间:2023-10-16

我用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;
}

取自 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('');
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;
}

在 C 中,我使用kill(pid_t pid, int sig)向特定的 pid 发送一个空白信号 (0(,因此检查它是否在返回此函数时处于活动状态。

我不知道你是否在C++有类似的功能,但因为你也在使用 linux,所以可能值得检查信号。