I'm trying to return an integer value from a child process.
However, if I use exit(1)
I get 256
as the output from wait()
. Using exit(-1)
gives 65280
.
Is there a way I can get the actual int value that I send from the child process?
if(!(pid=fork()))
{
exit(1);
}
waitpid(pid,&status,0);
printf("%d",status);
Edit: Using exit(-1)
(which is what I actually want) I am getting 255 as the output for WEXITSTATUS(status)
. Is it supposed to be unsigned?
exit()
vs_exit()
is immaterial - the exit status is handled the same either way. Useexit()
to ensure that pending output is flushed from standard I/O channels and similar cleanup issues - actually, useexit()
almost all the time. There are grounds for using_exit()
but they are rare. – Hirokohiroshiexit
is definitively the right thing. Your problem is notexit
butwaitpid
as Damon's answer suggests. – Rinna