Home > front end >  How to kill child process exec? [closed]
How to kill child process exec? [closed]

Time:09-30

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.

CodePudding user response:

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.

CodePudding user response:

it worked for me, it terminates the program if fork is not 0

int main(){

  if (fork() == 0)
      ShellExecute("open", "http://yourUrl.com")

  else
      _Exit()
}
  • Related