0

I am creating an application in C which I have to execute the firefox with the command execlp but every time I execute it I "lost" my current terminal, but after the execlp i still need to use the terminal which I was before, so my question is: Is there a way where I can be in one terminal call execlp and it executes in another one without block the one I am on?

here is a snippet of my code:

    pid_t child = fork();
if (child == -1) {
    perror("fork error");
} else if (child == 0) {
    exec_pid = getpid();
    execlp("firefox", "firefox", URL, NULL);
    perror("exec error");
} 
    // keep with program logic
Augusto Accorsi
  • 317
  • 5
  • 21
  • What exactly do you mean by " "lose" your current terminal"? Do you just mean that it goes out of focus? – rici Apr 13 '17 at 22:50
  • http://stackoverflow.com/questions/11807688/how-to-detach-a-process-from-terminal-in-unix – stark Apr 13 '17 at 23:10
  • after I execute exec for me to keep using the same terminal I have to either press any key or kill my program, so that is what I meant when I said I lost the terminal. – Augusto Accorsi Apr 13 '17 at 23:30
  • I can't reproduce that behaviour. Whar OS are you using? – rici Apr 14 '17 at 03:23

1 Answers1

2

If I'm understanding you correctly, you're saying that your program launches Firefox and then keeps control of your shell until Firefox terminates. If this is the case, there are a couple of ways around this.

The easiest solution is to run your program in the background. Execute it like ./my_program & and it be launched in a separate process and control of your terminal will be returned to you immediately.

If you want to solve this from your C code, the first step would be to print out the process ID of the child process after the fork. In a separate shell, use ps to monitor both your program and the forked PID. Ensure that your program is actually terminating and that it's not just stuck waiting on something.

bta
  • 43,959
  • 6
  • 69
  • 99
  • Also see [this related question](https://askubuntu.com/questions/484993/run-command-on-anothernew-terminal-window) for ways of running a program such that it launches in a new, separate terminal window. – bta Apr 13 '17 at 23:28