获取由 xdg-open 创建的进程的 PID

ele*_*ena 7 c fork exec

情况如下:我fork了用默认浏览器打开一个html文件的过程。以下是我的情况:

if ((pid=fork())==0) {
    execlp("/usr/bin/xdg-open", "xdg-open", url, NULL);
    /*if execlp failed, exit the child*/
    exit(0);
}
Run Code Online (Sandbox Code Playgroud)

但是,我想获取进程的 PID(打开的浏览器),以便稍后也可以关闭它。但我似乎不知道我怎么能得到它。如果您有任何建议,请告诉我。

Ser*_* L. -1

fork()将子进程的 pid 返回给父进程。

在孩子中,您可以使用标准获取它自己的pid getpid()

pid_t child_pid = fork();

if (child_pid < 0) {
    perror("fork");
    // error handling
} else if (!child_pid) {
    // child goes here
    pid_t my_pid = getpid();
} else {
    // parent continues here
}
Run Code Online (Sandbox Code Playgroud)