gpt4 book ai didi

c - 如何将子进程的返回值返回给使用exec创建的父进程?

转载 作者:太空宇宙 更新时间:2023-11-04 07:52:45 25 4
gpt4 key购买 nike

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

因此,如果子项返回的值大于 255,则父项不会获得正确的值。我的问题是父进程如何接收大于 255 的返回值?谢谢

最佳答案

这取决于操作系统及其对 SA_SIGINFO 的支持.如果您阅读了 POSIX 的规范 sigaction() 小心,如果你使用 SA_SIGINFO捕获有关所传递信号的额外信息,您会捕获 SIGCHLD信号,那么您可能能够获得额外的信息,如Signal Actions中所述和 <signal.h> .

特别是 <signal.h>文档指出,当信号为 SIGCHLD 时,然后:

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.

Linux 文档 sigaction() 表示这在 Linux 上受支持。然而,它比使用 waitpid() 更难组织。或其函数系列之一,我还没有证明它确实按照 POSIX 指定的方式工作。

关于c - 如何将子进程的返回值返回给使用exec创建的父进程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52731842/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com