How to kill child process exec?

224 Views Asked by At

I want to open a link in my browser after which I want the program to terminate. To do this I am making a child process that runs xdg-open on the link using exec. I have found that if a browser is already open, Then closing the browser doesn't terminate the program, and if the browser isn't already open, closing the browser terminates the program. How do I make it so that after the child process calls xdg-open, the program should terminate if the parent process has terminated. Here is the code:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

    int main()
    {
        if (fork() == 0) {
            execlp("xdg-open", "xdg-open", "https://youtube.com", NULL);
            exit(0);
        }
        else
            printf("I am the parent\n");
        printf("Exiting now\n");
        return 0;
    }

edit: removed a print statement after the exec call as I realised after exec the child process is replaced with the program I call.

1

There are 1 best solutions below

0
On

As @someprogrammeedude pointed out it actually terminated the program but it didn't give me my promt back until I pressed enter which is why I thought it was still running.