Getting the PID of a process created using xdg-open

The situation is this: I force the process to open the html file with the default browser. Here is how it looks in my case:

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

However, I want to get the PID of the process (open browser), so that I can close it later too. But I don't seem to know how I can get this. Please let me know if you have any suggestions.

+3
source share
2 answers

You should already have the PID of the child process, the fork () man page says:

If successful, the PID of the child process is returned in the parent, and 0 is returned in the child.

So, in the child process you find 0 and run your program, in the parent pid there will be the actual PID of the child.

, , , xdg-open, PID, , PID .

, , , , โ€‹โ€‹ ps pgrep. , PID , xdg-open.

+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
}
0

All Articles