gpt4 book ai didi

c - 如果子进程在父进程调用 wait() 之前退出怎么办?

转载 作者:行者123 更新时间:2023-12-03 16:13:28 26 4
gpt4 key购买 nike

我正在学习 C 中的 wait() 方法。我知道它会阻塞父进程,直到其子进程之一终止。但是如果内核决定先调度子进程并且子进程在父进程调用 wait() 之前终止呢? parent 是否会永远等待(没有其他中断),因为它无法观察到 child 的返回?

图中,如果执行顺序是:fork --> HC --> exit -->HP-->wait,那么就会出现我描述的情况。
enter image description here

最佳答案

不, parent 不会永远等待。

wait 上的文档状态:

All of these system calls are used to wait for state changes in a child of the calling process, and obtain information about the child whose state has changed. A state change is considered to be: the child terminated; the child was stopped by a signal; or the child was resumed by a signal. In the case of a terminated child, performing a wait allows the system to release the resources associated with the child; if a wait is not performed, then the terminated child remains in a "zombie" state .

If a child has already changed state, then these calls return immediately.

关于c - 如果子进程在父进程调用 wait() 之前退出怎么办?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56006339/

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