处理SIGCHLD会返回EOF给父亲,为什么呢?

发布于 08-22 08:21 字数 413 浏览 11 评论 0原文

我有一个小 shell,它创建子进程(使用 fork())并让它们使用 execvp 执行一些命令。 它还支持 &选项,以便父亲可以同时运行其他命令。 当一个孩子死掉时,我想在控制台上写下它以及孩子的pid,所以我为SIGCHLD定义了一个sighandler:

void backdeadchild(int sig)
{
 int pid, exitstat;
 pid = wait(&exitstat);
 printf("Child with pid: %d died", pid);
}

问题是,在处理程序打印消息之后,父亲也关闭了。 我已经设法理解 SIGCHLD 在 stdin 中返回 EOF (每当父亲收到 EOF 时它就会关闭),但为什么 SIGCHLD 返回 EOF ? 如果正常的话..如何避免父亲关闭?

I have a small shell that creates children (with fork()) and make them execute some commands with execvp.
It support also the & option so the father can run other commands in the meanwhile.
When a child die i want to write that on the console and also the child pid, so i defined a sighandler for SIGCHLD:

void backdeadchild(int sig)
{
 int pid, exitstat;
 pid = wait(&exitstat);
 printf("Child with pid: %d died", pid);
}

The problem is that after the handler prints the message also the father closes.
I've managed to understand that SIGCHLD returns an EOF in stdin (and whenever father receives EOF it closes), but why SIGCHLD returns EOF?
If it's normal.. how to avoid that the father closes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

始于初秋2024-08-29 08:21:11

如果子级的 stdout (或任何其他流)连接到父级的 stdin,则父级很自然会收到 EOF

或者,如果父级不处理它,则父级可能会因 SIGPIPE (损坏的管道)信号而死亡。

If child's stdout (or any other stream) is connected to parent's stdin, it is natural that parent receives the EOF.

Alternatively parent may die from SIGPIPE (broken pipe) signal, if parent doesn't don't handle it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文