15

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?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • I just read that exit() is recommended over _exit() for complete termination, right here on stackoverflow.com –  Sep 07 '10 at 14:40
  • 5
    The use of `exit()` vs `_exit()` is immaterial - the exit status is handled the same either way. Use `exit()` to ensure that pending output is flushed from standard I/O channels and similar cleanup issues - actually, use `exit()` almost all the time. There are grounds for using `_exit()` but they are rare. – Jonathan Leffler Sep 07 '10 at 14:44
  • Yes `exit` is definitively the right thing. Your problem is not `exit` but `waitpid` as Damon's answer suggests. – Jens Gustedt Sep 07 '10 at 14:45
  • See also [Exit codes bigger than 255 — possible?](https://stackoverflow.com/questions/179565/exitcodes-bigger-than-255-possible/) – Jonathan Leffler Jun 01 '17 at 03:33

5 Answers5

25

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));
}
Darron
  • 21,309
  • 5
  • 49
  • 53
  • can you give me any leads on the output 255 for exit(-1) using your code? –  Sep 07 '10 at 15:51
  • In 2's-complement notation the value -1 is all bits set. If you mask that to 8 bits and treat it as unsigned it looks like 255. The inability to distinguish exit(-1) from exit(255) is an unfortunate limitation of the way exit values are returned from the wait family of functions. – Darron Sep 07 '10 at 16:19
13

The exit code is a 16-bit value.

The high-order 8 bits are the exit code from exit().

The low-order 8 bits are zero if the process exited normally, or encode the signal number that killed the process, and whether it dumped core or not (and if it was signalled, the high-order bits are zero).

Check out the <sys/wait.h> header and the documentation for the waitpid() system call to see how to get the correct values with WIFEXITED and WEXITSTATUS.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
4

See the documentation. First use WIFEXITED to determine whether it terminated normally (possibly with non-zero status). Then, use WEXITSTATUS to determine what the low-order 8 bits of the actual status are.

Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
2

Use WEXITSTATUS() to read the correct exit status of child

Pass the status returned by waitpid() or wait()

e.g.:

int cstatus;
wait(&cstatus);
printf("Child exit status : %d\n", WEXITSTATUS(cstatus));
Dimitar
  • 4,402
  • 4
  • 31
  • 47
0

It doesn't. It sets it to 255. There are only 8 bits available. See the documentation.

user207421
  • 305,947
  • 44
  • 307
  • 483