Forked process not returning to terminal

Dear All,

I wrote a sample c++ program which forks a linux application (kcalc). I compiled this program using g++ and executed it. The kcalc application is launched. After closing the kcalc application using the quit button I am not getting the terminal. I need to press enter to return to the terminal. But If the kcalc application is called within the program without forking, it will return to the terminal. Could you please help me understand why forked process are not returned to the terminal after closing.

Attached is the sample code used for execution.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  #include <stdlib.h>
  #include <sys/types.h>
  #include <unistd.h>
  #include<iostream>

  int main()
  {
      char abc[20];
      pid_t  fork_pid;
      char *cmd[] = { "kcalc", (char *)0 };
      fork_pid = 0;
      if ((fork_pid = fork()) == -1)
      {
          printf("fork into background fail \n");
      }
      else
      {
          if (fork_pid == 0)
              int ret = execvp ("kcalc", cmd);
              printf("\n\n");
      }
          return 0;
  }

Regards
Viji
The parent should wait for the child to complete. See waitpid().
> After closing the kcalc application using the quit button I am not getting the terminal. I need to press enter to return to the terminal
Try enter a command.
1
2
3
          if (fork_pid == 0)
              int ret = execvp ("kcalc", cmd);
              printf("\n\n");//bad indentation, this is outside the if (executes the parent) 


Because you didn't wait, kcalc becomes an orphan. That means that you do have a terminal, even with kcalc open
Last edited on
Topic archived. No new replies allowed.