kduong has asked for the wisdom of the Perl Monks concerning the following question:

I always thought a 0 return status ($?) for perl wait() means success. I am currently experiencing a 0 return status ($?) for perl wait() in Solaris when I terminate the child process and a 256 return status ($?) for successful child exit (which contradict to what I thought). Does 0 return status in Solaris means failure? Is 256 the successful return status? Is there a list of exit code for perl wait()? I have trid to run 'man exit' in Unix but there is not a lot of information there. Please help. Any idea is appreciated.
  • Comment on What is a successful return status for perl wait() in Solaris?

Replies are listed 'Best First'.
Re: What is a successful return status for perl wait() in Solaris?
by cdarke (Prior) on Feb 01, 2008 at 14:30 UTC
    Only one byte is allowed for a return status on UNIX, so 255, 0xff, all bits set is the max allowed. Setting 256 potentially sets a bit in the more significant byte, leaving the status byte as zero.
      Thanks for the information. So does 256 exit status means a successful exit of the child process? What happened is there is a bug in one of our program and the program will abort if $? >> 8 == 0. I can see if I terminate a child process, the exit status of wait is 0. But I thought the exit status of 0 means success, that's why I am a little bit confused.
        So does 256 exit status means a successful exit of the child process?
        No, you cannot exit with 256 from UNIX, only 0-255. However, if you are talking about $?, that is a different matter. As others have said, $? contains many things other than just the return code. If the child process terminated because of a signal then the signal number is in $? as well.
        $exit_value = $? >> 8; $signal_num = $? & 127; $dumped_core = $? & 128;

        I think there has been some confusion over what you mean by an exit value.
Re: What is a successful return status for perl wait() in Solaris?
by apl (Monsignor) on Feb 01, 2008 at 14:18 UTC
    According to the Camel Book, this function waits for a child process to terminate. $? contains the pid of the deceased process, or -1 if there are no child processes.

    Revised: Please ignore.

      The return value is the PID. $? is the exit status of the child process.

      The reason why the OP is getting 256 is explained in perlvar (the entry for $?):

      The status returned by the last pipe close, backtick (``) command, successful call to wait() or waitpid(), or from the system() operator. This is just the 16-bit status word returnedby the wait() system call (or else is made up to look like it). Thus, the exit value of the subprocess is really ("$? >> 8"), and "$? & 127" gives which signal, if any, the process died from, and "$? & 128" reports whether there was a core dump. (Mnemonic: similar to sh and ksh.)

      I think it's rather (citing "perldoc -f wait"):

      ...it waits for a child process to terminate and returns the pid of the deceased process, or "-1" if there are no child processes. The status is returned in $?.

      See man 2 wait on how to interpret the return status. Also, perldoc -f system has some sample code for properly handling $?.

      wait() return the pid, but $? returns the return status. For example: $a = wait(); $a is the pid and $? is the return status.