0

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.

  • If `execlp` succeeds, you will never see `printf("Child Exiting\n");`. `execlp` never returns if it's successful. – Ted Lyngmo Sep 29 '21 at 09:47
  • oh yeah just realised that, but how do I terminate the program after exec? as in when calling it from the terminal I don't get my prompt back after the link is opened – walidathome Sep 29 '21 at 09:52
  • Are you sure you "don't get [your] prompt back"? Have you tried pressing the `Enter` key in the terminal? What happens then? If the child-process outputs anything, the shell will not be able to detect that and redisplay the prompt. It's still there though, before the output. – Some programmer dude Sep 29 '21 at 09:56
  • oh I had not tried that, thanks. Why do I need to press enter though? – walidathome Sep 29 '21 at 09:57
  • Another detail: If `execlp` fails, you probably want to signal that to the parent process somehow. `exit(0)` is usually used to signal success so you may want to use some other exit code. – Ted Lyngmo Sep 29 '21 at 10:01
  • Open a new shell. Don't write any commands, and only press `Enter`. That's what's happening in your case. You give an empty line as input to the shell, which forces it to display the prompt. – Some programmer dude Sep 29 '21 at 10:07
  • @Someprogrammerdude ah makes sense. Thank you for clearing it up! – walidathome Sep 29 '21 at 10:10
  • 1
    @TedLyngmo oh okay, thank you for pointing that out! – walidathome Sep 29 '21 at 10:12
  • @Someprogrammerdude I had one more question, when you run any other command say you run an ls, after listing the files, it redisplays your prompt, so why doesn't my program redisplay the prompt after it terminates? – walidathome Sep 29 '21 at 10:14
  • Perhaps the child-process finishes before your parent process finishes? The shell will show the prompt when your program exits. If the child process write output *after* the parent process exits, that output will be shown but the shell will not know about it and doesn't redisplay the prompt. – Some programmer dude Sep 29 '21 at 10:17
  • When you call `fork` *your* programs becomes a parent process of the new child process created by `fork`. The shell then becomes the *grand* parent of the new child process. The shell only know about your process, and nothing about the child process it creates. – Some programmer dude Sep 29 '21 at 10:32
  • oh okay that cleared things up for me. Thanks! – walidathome Sep 29 '21 at 10:33

1 Answers1

1

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.