我有以下程序
int external_apply(char *type)
{
int pfds[2];
if (pipe(pfds) < 0)
return -1;
if ((pid = fork()) == -1)
goto error;
if (pid == 0) {
/* child */
const char *argv[8];
int i = 0;
argv[i++] = "/bin/sh";
argv[i++] = "script_file.sh";
argv[i++] = "apply";
close(pfds[0]);
dup2(pfds[1], 1);
close(pfds[1]);
execvp(argv[0], (char **) argv);
exit(ESRCH);
} else if (pid < 0)
goto error;
/* parent */
close(pfds[1]);
int status;
while (wait(&status) != pid) {
printf("waiting for child to exit");
}
return 0;
error:
close(pfds[0]);
return -1;
}
叉子调用我的脚本文件。脚本文件包含导致管道关闭(有时)的命令。如果管道被 scipt 关闭,等待将导致程序崩溃。
脚本关闭管道时如何避免程序崩溃?