检查 wait() 是否失败
Checking if wait() failed
为了知道 wait() 是否起作用,像下面这样检查是否正确?理论上,如果wait()没有失败,应该return给父进程结束的子进程pid,否则父进程pid就是1吧?
switch (process = fork())
{
case -1:
// Fork fail
perror("Fork failed");
exit(EXIT_FAILURE);
case 0:
//Child process
HERE CODE DOES SOMETHING
exit(EXIT_SUCCESS);
default:
//Parent process
pid=wait(&status);
if(pid==1){
perror("Wait failed");
}else{
exit(EXIT_SUCCESS);
}
}
引用 man 2 wait
:
RETURN VALUE
wait(): on success, returns the process ID of the terminated child; on
error, -1 is returned.
所以要检查 wait(2)
是否失败,这应该足够了:
if (wait(&status) == -1) {
perror("wait failed");
exit(1);
}
为了知道 wait() 是否起作用,像下面这样检查是否正确?理论上,如果wait()没有失败,应该return给父进程结束的子进程pid,否则父进程pid就是1吧?
switch (process = fork())
{
case -1:
// Fork fail
perror("Fork failed");
exit(EXIT_FAILURE);
case 0:
//Child process
HERE CODE DOES SOMETHING
exit(EXIT_SUCCESS);
default:
//Parent process
pid=wait(&status);
if(pid==1){
perror("Wait failed");
}else{
exit(EXIT_SUCCESS);
}
}
引用 man 2 wait
:
RETURN VALUE
wait(): on success, returns the process ID of the terminated child; on error, -1 is returned.
所以要检查 wait(2)
是否失败,这应该足够了:
if (wait(&status) == -1) {
perror("wait failed");
exit(1);
}