如何检查进程是否在 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;
}

原文由 Saurabh Jadhav 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 464
1 个回答

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

原文由 metamorphling 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题