I am writing a Linux application. What happens if I call fork()
and then run an application that takes console input? Consider the code below:
int process_id = fork();
if (process_id != 0) {
/* this is the parent process */
error = execv("../my_other_app", "parameter1", NULL);
if (error < 0) {
printf("error!");
}
} else {
/* this is the child process. Wait for my_other_app to set up */
sleep(3);
/* now continue */
}
printf("########## press ENTER to stop ##########\n");
getchar();
exit(0);
The thing is, my_other_app
also has a press ENTER to stop message. So when I do the getchar()
call, which application is reading it? The main application or the my_other_app
that I launched with execv
?
EDIT: It appears through testing that my_other_app
takes priority over the console. Does this happen every time? Is there a way to make sure the console is instead owned by the main process?