【发布时间】:2014-02-10 09:48:29
【问题描述】:
这是我的代码。我基本上想创建一个子进程,它将通过 execvp() 执行一些命令。然而,该程序永远不会到达那里,因为它永远不会打印“Got to child”。我不明白为什么。有人可以解释错误是什么吗?因为我认为我正在遵循 t 的程序。
pid_t pid;
pid = fork();
if (pid < 0) { //if fork() returns less than 1 it failed
fprintf(stderr, "fork failed");
return 1;
}
else if (pid == 0) { //for() returns 0 then this is the child process
printf("Got to child");
int exec;
exec = execvp(args[0], args); /* (2) invoke execvp() */
if (exec < 0) {
printf("Error: executing command failed.\n");
exit(1);
}
}
else { //pid>0 therefore this is the parent process
if (background == 0){
wait(&status);
printf("Got to parent");
}
}
【问题讨论】:
-
请发布有效的、可编译的代码。另外,删除与问题无关的代码。更改后,确保它仍然可以编译,并重现问题...
-
在你的 printf 中换行或刷新它。