我試圖做一個叉運行在一個文件的exec,然後等待它完成。如果子進程中的exec由我要打印信號的信號終止,如果程序運行時間過長,我想打印超時。waitpid不工作 - 默認錯誤
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,它表明狀態地址是無效的。這怎麼可能呢?