多线程程序中的fork [英] fork in multi-threaded program

查看:23
本文介绍了多线程程序中的fork的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说在程序中混合分叉和线程可能会产生很大的问题,通常会导致神秘的行为,尤其是在处理共享资源时,例如锁、管道、文件描述符.但我从来没有完全理解危险到底是什么,什么时候会发生.如果有这方面专业知识的人能更详细地解释一下什么是陷阱以及在这样的环境中编程时需要注意什么,那就太好了.

I've heard that mixing forking and threading in a program could be very problematic, often resulting with mysterious behavior, especially when dealing with shared resources, such as locks, pipes, file descriptors. But I never fully understand what exactly the dangers are and when those could happen. It would be great if someone with expertise in this area could explain a bit more in detail what pitfalls are and what needs to be care when programming in a such environment.

例如,如果我想编写一个从各种不同资源收集数据的服务器,我想到的一个解决方案是让服务器产生一组线程,每个线程都会调用另一个程序来完成实际工作,打开管道从孩子那里取回数据.这些线程中的每一个都响应自己的工作,它们之间没有数据交换,并且当收集数据时,主线程有一个队列,这些工作线程只会将结果放入队列中.这个解决方案会出现什么问题?

For example, if I want to write a server that collects data from various different resources, one solution I've thought is to have the server spawns a set of threads, each popen to call out another program to do the actual work, open pipes to get the data back from the child. Each of these threads responses for its own work, no data interexchange in b/w them, and when the data is collected, the main thread has a queue and these worker threads will just put the result in the queue. What could go wrong with this solution?

请不要仅通过回答"我的示例场景来缩小您的答案.任何与示例无关但有助于提供简洁设计的建议、替代解决方案或经验都会很棒!谢谢!

Please do not narrow your answer by just "answering" my example scenario. Any suggestions, alternative solutions, or experiences that are not related to the example but helpful to provide a clean design would be great! Thanks!

推荐答案

当你确实有一些线程在运行时,fork 的问题是,fork 只复制调用它的一个线程的 CPU 状态.就好像所有其他线程都立即死亡,无论它们在哪里.

The problem with forking when you do have some threads running is that the fork only copies the CPU state of the one thread that called it. It's as if all of the other threads just died, instantly, wherever they may be.

这样的结果是锁没有被释放,共享数据(例如 malloc 堆)可能被破坏.

The result of this is locks aren't released, and shared data (such as the malloc heap) may be corrupted.

pthread 确实提供了 pthread_atfork 功能 - 理论上,您可以在分叉之前获取程序中的每一个锁,之后释放它们,也许可以让它活着 - 但这是有风险的,因为你总是会错过一个.当然,其他线程的堆栈也不会被释放.

pthread does offer a pthread_atfork function - in theory, you could take every lock in the program before forking, release them after, and maybe make it out alive - but it's risky, because you could always miss one. And, of course, the stacks of the other threads won't be freed.

这篇关于多线程程序中的fork的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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