我正在尝试制作一个在一个文件中运行 exec 然后等待它完成的 fork。如果子进程中的 exec 被信号终止,我想打印信号,如果程序运行时间太长,我想打印超时。
pid_t pid;
pid = fork();
if(pid == 0) {
//child process
}
else {
alarm(timeout);
int status;
if(wait(pid, &status, 0) == pid) {
alarm(0);
fpw = fopen(testreport, "a+");
if(WIFSIGNALED(status)) {
fprintf(fpw, "Run time errors: signal %d\n", WTERMSIG(status);
}
else {
fprintf(fpw, "Run time errors: none");
}
fclose(fpw);
}
else {
alarm(0);
kill(pid, SIGTERM);
fpw = fopen(testreport, "a+");
fprintf(fpw, "Run time errors: timeout\n");
fclose(fpw);
}
}
Testreport 是先前声明的 char * 文件名。waitpid 不工作。当我打印 waitpid 的值和产生的 errno 时,我分别得到 -1 和 14。我查了 errno 14,它是一个 EFAULT,这表明 status 的地址是无效的。怎么会这样?