linux - 获取进程的pid

发布于 2024-10-20 14:41:07 字数 84 浏览 5 评论 0原文

如何在 Linux 上使用 C++ 获取名为 abc 的服务的 PID,而不使用系统调用?如果您愿意提供任何示例,我将不胜感激。

How can I get the PID of a service called abc using C++ on Linux without using a system call? I would appreciate any examples that you care to offer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

给不了的爱 2024-10-27 14:41:07

由于长期以来不鼓励使用 sysctl ,因此推荐的方法是检查 /proc 并读取每个文件夹中的 comm 文件。对于您的示例,如果该文件的内容是 abc\n,那么这就是您要查找的进程。

我不太会说 C++,但这里有 POSIX C89 中的一种可能的解决方案:

#include <glob.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>

pid_t find_pid(const char *process_name)
{
    pid_t pid = -1;
    glob_t pglob;
    char *procname, *readbuf;
    int buflen = strlen(process_name) + 2;
    unsigned i;

    /* Get a list of all comm files. man 5 proc */
    if (glob("/proc/*/comm", 0, NULL, &pglob) != 0)
        return pid;

    /* The comm files include trailing newlines, so... */
    procname = malloc(buflen);
    strcpy(procname, process_name);
    procname[buflen - 2] = '\n';
    procname[buflen - 1] = 0;

    /* readbuff will hold the contents of the comm files. */
    readbuf = malloc(buflen);

    for (i = 0; i < pglob.gl_pathc; ++i) {
        FILE *comm;
        char *ret;

        /* Read the contents of the file. */
        if ((comm = fopen(pglob.gl_pathv[i], "r")) == NULL)
            continue;
        ret = fgets(readbuf, buflen, comm);
        fclose(comm);
        if (ret == NULL)
            continue;

        /*
        If comm matches our process name, extract the process ID from the
        path, convert it to a pid_t, and return it.
        */
        if (strcmp(readbuf, procname) == 0) {
            pid = (pid_t)atoi(pglob.gl_pathv[i] + strlen("/proc/"));
            break;
        }
    }

    /* Clean up. */
    free(procname);
    free(readbuf);
    globfree(&pglob);

    return pid;
}

警告:如果有多个正在运行的进程具有您正在查找的名称,则此代码将仅返回一个。如果您要更改此设置,请注意,在编写简单的 glob 时,您还将检查 /proc/self/comm,这可能会导致重复条目。

如果有多个进程具有相同的名称,则实际上没有一种方法可以确保您获得正确的进程。出于这个原因,许多守护进程都能够将其 pid 保存到文件中;检查你的文档。

Since use of sysctl has been discouraged for ages now, the recommended way of doing this is by examining each of the process entries in /proc and reading the comm file in each folder. If, for your example, the contents of that file are abc\n, that's the process you're looking for.

I don't really speak C++, but here's a possible solution in POSIX C89:

#include <glob.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>

pid_t find_pid(const char *process_name)
{
    pid_t pid = -1;
    glob_t pglob;
    char *procname, *readbuf;
    int buflen = strlen(process_name) + 2;
    unsigned i;

    /* Get a list of all comm files. man 5 proc */
    if (glob("/proc/*/comm", 0, NULL, &pglob) != 0)
        return pid;

    /* The comm files include trailing newlines, so... */
    procname = malloc(buflen);
    strcpy(procname, process_name);
    procname[buflen - 2] = '\n';
    procname[buflen - 1] = 0;

    /* readbuff will hold the contents of the comm files. */
    readbuf = malloc(buflen);

    for (i = 0; i < pglob.gl_pathc; ++i) {
        FILE *comm;
        char *ret;

        /* Read the contents of the file. */
        if ((comm = fopen(pglob.gl_pathv[i], "r")) == NULL)
            continue;
        ret = fgets(readbuf, buflen, comm);
        fclose(comm);
        if (ret == NULL)
            continue;

        /*
        If comm matches our process name, extract the process ID from the
        path, convert it to a pid_t, and return it.
        */
        if (strcmp(readbuf, procname) == 0) {
            pid = (pid_t)atoi(pglob.gl_pathv[i] + strlen("/proc/"));
            break;
        }
    }

    /* Clean up. */
    free(procname);
    free(readbuf);
    globfree(&pglob);

    return pid;
}

Caveat: if there are multiple running processes with the name you're looking for, this code will only return one. If you're going to change that, be aware that with the naive glob written, you'll also examine /proc/self/comm, which could potentially lead to a duplicate entry.

If there are multiple processes with the same name, there isn't really a way to ensure you got the right one. Many daemons have the ability to save their pids to a file for this reason; check your documentation.

波浪屿的海角声 2024-10-27 14:41:07

谷歌已经涵盖了这个:)

http://programming-in-linux.blogspot.com/2008/03/get-process-id-by-name-in-c.html

虽然它确实使用了sysctl,这是一个系统调用!

它是 C,但在 C++ 中应该同样有效

Google has this covered :)

http://programming-in-linux.blogspot.com/2008/03/get-process-id-by-name-in-c.html

Although it does use sysctl, which is a system call!

It's C but should work just as well in C++

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文