多线程(pthreads) & fork
来源:互联网 发布:桌面隐藏软件下载 编辑:程序博客网 时间:2024/06/06 06:44
子进程hang在了一个mutex上,这才第一次关注多线程情况下fork的问题。
http://cboard.cprogramming.com/linux-programming/99421-pthreads-plus-fork.html
http://pubs.opengroup.org/onlinepubs/009695399/functions/fork.html
A process shall be created with a single thread. If a multi-threaded process calls fork(), the new process shall contain a replica of the calling thread and its entire address space, possibly including the states of mutexes and other resources. Consequently, to avoid errors, the child process may only execute async-signal-safe operations until such time as one of the exec functions is called.
多线程的进程fork之后,子进程只copy了调用fork的线程,(可能)包括muxtex互斥锁的状态。
pthread_atfork的文档讲了多线程中fork可能带来的问题:
http://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_atfork.html
http://www.cnblogs.com/zhenjing/archive/2010/12/28/pthread_atfork.html
One problem has to do with state (for example,memory) covered by mutexes. Consider the case where one thread has a mutexlocked and the state covered by that mutex is inconsistent while another threadcallsfork().In the child, the mutex is in the locked state (locked by a nonexistent threadand thus can never be unlocked). Having the child simply reinitialize the mutexis unsatisfactory since this approach does not resolve the question about howto correct or otherwise deal with the inconsistent state in the child.
一个线程给mutex加了锁,然后正在修改受保护的数据,另一个线程调用了fork,那么子进程中这个mutex处于加了锁的状态,但是却没有人来给它解锁了。
解决方案是:
1. 如果可能的话,fork之后尽快调用exec。在此之前,只调用async-signal-safe的库函数。
2. 使用pthread_atfork(),the prepare handler acquires all mutex locks and the other two fork handlers release them.
http://cboard.cprogramming.com/linux-programming/99421-pthreads-plus-fork.html
http://pubs.opengroup.org/onlinepubs/009695399/functions/fork.html
A process shall be created with a single thread. If a multi-threaded process calls fork(), the new process shall contain a replica of the calling thread and its entire address space, possibly including the states of mutexes and other resources. Consequently, to avoid errors, the child process may only execute async-signal-safe operations until such time as one of the exec functions is called.
多线程的进程fork之后,子进程只copy了调用fork的线程,(可能)包括muxtex互斥锁的状态。
pthread_atfork的文档讲了多线程中fork可能带来的问题:
http://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_atfork.html
http://www.cnblogs.com/zhenjing/archive/2010/12/28/pthread_atfork.html
One problem has to do with state (for example,memory) covered by mutexes. Consider the case where one thread has a mutexlocked and the state covered by that mutex is inconsistent while another threadcallsfork().In the child, the mutex is in the locked state (locked by a nonexistent threadand thus can never be unlocked). Having the child simply reinitialize the mutexis unsatisfactory since this approach does not resolve the question about howto correct or otherwise deal with the inconsistent state in the child.
一个线程给mutex加了锁,然后正在修改受保护的数据,另一个线程调用了fork,那么子进程中这个mutex处于加了锁的状态,但是却没有人来给它解锁了。
解决方案是:
1. 如果可能的话,fork之后尽快调用exec。在此之前,只调用async-signal-safe的库函数。
2. 使用pthread_atfork(),the prepare handler acquires all mutex locks and the other two fork handlers release them.
- 多线程(pthreads) & fork
- PHP 多线程扩展 pthreads
- Pthreads多线程编程(1)
- PHP pthreads 多线程操作
- php 多线程 pthreads扩展
- Pthreads多线程编程(2)
- Pthreads多线程编程(3)
- php 多线程 pthreads安装
- Pthreads 多线程入门教程
- PHP里的多线程(pthreads)
- Cocos2dx 中 引入pthreads 多线程
- PHP多线程扩展pthreads实例
- PHP多线程扩展pthreads示例
- php安装pthreads多线程扩展
- 多线程编程之一:pthreads介绍
- 多线程 fork
- Pthreads
- pthreads
- 工程中添加xml类出现结构重定义解决办法
- poj 1064 Cable master
- hg (mercurial) 一个端口serve多个repo
- ready和onload方法
- 嵌入式系统 Boot Loader 技术内幕
- 多线程(pthreads) & fork
- big table简介
- Android 检查当前是否已经连接上 Internet
- 关于短信黑名单 BroadCast
- 『神的眼睛』
- TRACE的几种方式/宏定义例子
- 强制 Android 应用使用某个 Locale
- ifstream.getline()的陷阱
- 用批处理命令自动设置办公室局域网地址及IE代理