- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
背景
我正在用 C 语言编写一个共享库,与 LD_PRELOAD
动态链接,这意味着拦截和覆盖预加载它的应用程序的网络调用,例如 socket()
、connect()
、recv()
、send()
等
在库的初始化中,我使用 pthread_create()
启动了一个线程。该线程轮询一些映射到用户空间的内核内存。
该库旨在足够通用以处理 - 但不限于 - 网络基准测试应用程序,例如 netperf、iperf、sockperf.
我的问题
在大多数情况下,一切正常,生活是甜蜜的,除了一个。恶魔化的应用程序。例如,如果我将 netserver(netperf 基准测试应用程序的服务器端)作为守护程序启动,即没有 -D
参数,一个应用程序做的第一件事就是调用 fork()
。在 fork 上,父进程使用 exit(EXIT_SUCCESS)
关闭,子进程监听连接。 parent 退出的事实杀死了我的轮询线程。
所以我想要实现的是,如果父进程已经离开,则让子进程生成一个新的轮询线程。我可以拦截并覆盖 fork()
调用,但从根本上说,我怎样才能让 child 知道 parent 是否已经离开?我不能做任何假设,因为库必须是通用的。
谢谢。
最佳答案
您可以定期轮询 getppid() 函数。一旦它开始返回“1”(init 进程的 ID)——你的 parent 就死了。
更新
摘自“man pthread_create”:
The new thread terminates in one of the following ways: ...
- Any of the threads in the process calls exit(3), or the main thread performs a return from main(). This causes the termination of all threads in the process.
因此,如果您的线程是由调用 exit 的 netserver 进程创建的 - 是的,该线程将被终止
关于c - 叉(): way for a child process to check if parent is dead?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12539884/
我是一名优秀的程序员,十分优秀!