如何获取使用exec创建的子进程返回其父进程的返回值? [英] How to get the return value of child process to its parent which was created using exec?

查看:253
本文介绍了如何获取使用exec创建的子进程返回其父进程的返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了类似的问题此处.答案建议使用WEXITSTATUS.但是根据WAIT(2)的手册页,它有一个局限性.它说: WEXITSTATUS(wstatus) 返回子项的退出状态. 这由状态参数的最低有效8位组成,子级在调用exit(3)或_exit(2)时指定为子对象或作为参数 用于main()中的return语句.仅当WIFEXITED返回true时,才应使用此宏.

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.

因此,如果子级返回的值大于255,则父级将无法获得正确的值.我的问题是父进程如何接收大于255的返回值?谢谢

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

推荐答案

这取决于操作系统及其对SA_SIGINFO的支持.如果您仔细阅读POSIX的规范 sigaction() ,并且您使用SA_SIGINFO捕获有关所传递信号的更多信息,并捕获SIGCHLD信号,那么 可能能够获取更多信息,如

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>.

尤其是<signal.h>文档指出,当信号为SIGCHLD时,则:

In particular, the <signal.h> documentation notes that when the signal is SIGCHLD, then:

int si_status

如果si_code等于CLD_EXITED,则si_status保留该进程的退出值;否则,将保留该进程的退出值.否则,它等于导致进程更改状态的信号. si_status中的退出值应等于完整的退出值(即,传递给_exit()_Exit()exit()或从main()返回的值);它不应限于该值的最低有效八位.

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.

Linux sigaction() 的文档指出这在Linux上受支持.但是,与使用waitpid()或其功能系列之一相比,组织起来要困难得多,而且我还没有证明它确实可以像POSIX所指定的那样工作.

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.

这篇关于如何获取使用exec创建的子进程返回其父进程的返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆