本文介绍了如何在fork()中获取execvp的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码.
我的问题在代码中
int main() {
....
if ((uproc.pid = fork()) == -1) {
return -1;
}
if (uproc.pid == 0) {
/* child */
const char *argv[3];
int i = 0;
argv[i++] = "/bin/sh";
argv[i++] = "/my/script.sh";
argv[i++] = NULL;
execvp(argv[0], (char **) argv);
exit(ESRCH);
} else if (uproc.pid < 0)
return -1;
/* parent */
int status;
while (wait(&status) != uproc.pid) {
DD(DEBUG,"waiting for child to exit");
}
// If /my/script.sh exit accidentally in some place with error.
// can I catch this error right here?
......
}
推荐答案
子项的退出状态由wait
函数在status
变量中提供.
The exit status of the child is provided by the wait
function, in the status
variable.
您可以使用WEXITSTATUS
宏获取退出状态,但前提是程序正常退出(即称为exit
或从其main
函数返回的程序):
You get the exit status by using the WEXITSTATUS
macro, but only if the program exited normally (i.e. called exit
or returned from its main
function):
if (WIFEXITED(status))
printf("Child exit status: %d\n", WEXITSTATUS(status));
else
printf("Child exited abnormally\n");
阅读 wait
手册页以了解更多信息.
Read the manual page for wait
for more information.
这篇关于如何在fork()中获取execvp的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!