子进程可以等待父进程在C语言中的Linux编程中终止吗? [英] Can a child process wait for the parent process to terminate in Linux programming in C?

查看:333
本文介绍了子进程可以等待父进程在C语言中的Linux编程中终止吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Linux的C编程中,我知道wait()函数用于等待子进程终止,但是子进程有一些方法(或函数)等待父进程终止吗?

In C programming for Linux, I know the wait() function is used to wait for the child process to terminate, but are there some ways (or functions) for child processes to wait for parent process to terminate?

推荐答案

简短答案:否.

父进程可以控制其子进程的终端或进程组,这就是我们拥有wait()waitpid()函数的原因.一个孩子没有对父母的那种控制权,因此没有任何内置的东西.

A parent process can control the terminal or process group of its children, which is why we have the wait() and waitpid() functions. A child doesn't have that kind of control over its parent, so there's nothing built in for that.

如果您确实需要孩子知道其父级何时退出,则可以让父级在atexit()处理程序中向该子级发送信号,然后让该子级捕获该信号.

If you really need a child to know when its parent exits, you can have the parent send a signal to the child in an atexit() handler, and have the child catch that signal.

这篇关于子进程可以等待父进程在C语言中的Linux编程中终止吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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