Why does wait () set the state to 256 instead of the – 1 exit state of the forked process?

I want to return an integer value from the subprocess

However, if I use exit (1), I get 256 as output Exit (- 1) gives 65280

Is there any way to get the actual int value I sent from the child process?

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

Edit: using exit (- 1) (this is what I actually want), I get 255 as the output of wexitstatus Should it be unsigned?

Solution

Have you ever tried "man waitpid"?

The value returned from the waitpid () call is the encoding of the exit value A set of macros will provide the original exit value Or you can try shifting the value by 8 bits if you don't care about portability

The portable version of your code will be:

if(!(pid=fork()))
{
    exit(1);
}
waitpid(pid,0);
if (WIFEXITED(status)) {
    printf("%d",WEXITSTATUS(status));
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
***
下一篇>>