我有以下代码。
我的问题在代码中
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
多变的。
您可以使用以下命令获取退出状态WEXITSTATUS
宏,但前提是程序正常退出(即调用exit
或从其返回main
功能):
if (WIFEXITED(status))
printf("Child exit status: %d\n", WEXITSTATUS(status));
else
printf("Child exited abnormally\n");
阅读手册页wait了解更多信息。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)