Why does wait() set status to 255 instead of the -1 exit status of the forked process?

Have you tried “man waitpid”?

The value returned from the waitpid() call is an encoding of the exit value. There are a set of macros that will provide the original exit value. Or you can try right shifting the value by 8 bits, if you don’t care about portability.

The portable version of your code would be:

if(!(pid=fork()))
{
    exit(1);
}
waitpid(pid,&status,0);
if (WIFEXITED(status)) {
    printf("%d", WEXITSTATUS(status));
}

Leave a Comment