【发布时间】:2014-02-24 22:44:40
【问题描述】:
我正在编写一个 Linux 应用程序。如果我调用fork() 然后运行一个接受控制台输入的应用程序会发生什么?考虑下面的代码:
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);
问题是,my_other_app 也有一个 按 ENTER 停止消息。那么当我调用getchar() 时,哪个应用程序正在读取它?主应用还是我用execv启动的my_other_app?
编辑:通过测试,my_other_app 似乎优先于控制台。每次都会出现这种情况吗?有没有办法确保控制台归主进程所有?
【问题讨论】:
标签: c linux multithreading fork execv