How to get the return value of child process to its parent which was created using exec?

拥有回忆 提交于 2019-12-11 15:45:21

问题


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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/52731842/how-to-get-the-return-value-of-child-process-to-its-parent-which-was-created-usi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!