I have seen similar questions here and here. The answers suggest to use WEXITSTATUS. But according to man page of WAIT(2), it has a limitation. It says:
WEXITSTATUS(wstatus)
returns the exit status of the child. This consists of the least significant 8 bits of the status argument that the child specified in a call to exit(3) or _exit(2) or as the argument
for a return statement in main(). This macro should be employed only if WIFEXITED returned true.
So, if the child returns a value larger than 255, parent doesn't get the correct value. My question is how can a parent process receive the returned value which is larger than 255? Thanks
It depends on the o/s and its support for SA_SIGINFO
. If you read the specification of POSIX sigaction()
carefully, and if you use SA_SIGINFO
to capture extra information about the signals delivered, and you catch the SIGCHLD
signal, then you may be able to get extra information, as documented in Signal Actions and <signal.h>
.
In particular, the <signal.h>
documentation notes that when the signal is SIGCHLD
, then:
int si_status
If si_code
is equal to CLD_EXITED
, then si_status
holds the exit value of the process; otherwise, it is equal to the signal that caused the process to change state. The exit value in si_status
shall be equal to the full exit value (that is, the value passed to _exit()
, _Exit()
, or exit()
, or returned from main()
); it shall not be limited to the least significant eight bits of the value.
The documentation for Linux sigaction()
indicates that this is supported on Linux. It is, however, considerably harder to organize than using waitpid()
or one of its family of functions, and I have not demonstrated that it really works as POSIX specifies.
My question is how can a parent process receive the returned value which is larger than 255?
It can't. The return value of a process must be from 0 to 255. If you want to communicate any other value between a child and a parent, you need some form of interprocess communication such as pipes.