gpt4 book ai didi

c - fork() 阻塞父进程

转载 作者:行者123 更新时间:2023-11-30 14:23:10 25 4
gpt4 key购买 nike

我正在尝试使用 fork() 以非阻塞方式在程序内运行程序。

pid = fork();

//check for errors
if (pid < 0) {
exit(1);
}

//the child process runs the gulp
if (pid == 0) {
if (execv("/home/gulpSniffer/programname", args) < 0) {
exit(1);
}
//child is supposed to block here
}

//father is supposed to continue its run from here

但是,子进程中程序的调用会阻塞整个程序,而父进程的代码段不会被执行,因为它被子进程阻塞了。

有人知道为什么吗?

谢谢

最佳答案

您是否等待子进程在父进程中终止?这将阻塞直到子进程真正终止。

或者也许您有自己的 SIGCHLD 信号处理程序,它会以某种方式阻塞?

无法想到子级可以创建父级 block 的任何其他方式(好吧,除了任何进程间锁定机制,但您会知道是否使用了这些机制)。

此外,如果您不关心子进程何时结束,则应该设置

信号(SIGCHLD, SIG_IGN);

这样系统应该自动收获退出的 child ,并且你不会最终成为僵尸。

关于c - fork() 阻塞父进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13109373/

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