没有exec的fork和共享对象使用的pthread_mutex_t [英] fork without exec, and pthread_mutex_t used by shared object

查看:110
本文介绍了没有exec的fork和共享对象使用的pthread_mutex_t的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个Web服务器项目,该项目执行fork而没有exec.该程序依赖于OpenSSL,而OpenSSL需要多个锁(确切地说,CRYPTO_NUM_LOCKS大约是40个).这些锁通常在Linux上为pthread_mutex_t,并且在创建任何线程之前先在主线程上安装/创建这些锁.

I'm working with a web server project that performs a fork without an exec. The program depends upon OpenSSL, and OpenSSL needs a number of locks (CRYPTO_NUM_LOCKS to be exact, which is about 40 at the moment). The locks are typically pthread_mutex_t on Linux, and they are installed/created on the main thread before any threads are created.

fork创建一个新进程.但是,在fork之后,互斥锁中使用的句柄是浅复制的.也就是说,它们具有相同的特征,但是我认为它们在新过程的背景下没有有用的价值.

fork creates a new process. However, after the fork, the handles used in the mutex are shallow copied. That is, they have the same bits but I don't believe they have a useful value in the context of the new process.

我认为普遍的问题是共享对象中线程和派生的安全性. libcrypto上的 OpenSSL Wiki 页讨论了

I think the general problem is that of thread and fork safety in a shared object. The OpenSSL wiki page on libcrypto discusses thread and fork safety, and I'm not sure how to address some of the problems.

是否有一种方法可以指定应在fork上重新初始化库(例如OpenSSL)?

Is there a way to specify that a library (such as OpenSSL) should be re-initialized on fork?

推荐答案

除非您将互斥锁放在共享内存中,并且将pthread_mutexattr_setpshared设置为PTHREAD_PROCESS_SHARED来初始化互斥锁,否则互斥锁在整个fork中将无效.

The mutexes won't be valid across the fork unless you put them in shared memory and initialize the mutex attribute with pthread_mutexattr_setpshared set to PTHREAD_PROCESS_SHARED.

不清楚是分叉然后创建线程还是先创建线程然后分叉.如果是后者,则fork将仅应用于调用fork的线程.在新进程中没有其他线程被创建.

It's not clear if you are forking and then creating threads or creating the threads and then forking. If the latter, then fork will only apply to the thread that calls fork. None of the other threads are created in the new process.

如果要分叉然后进行线程化,并且互斥锁仅适用于子进程及其线程,则只需在派生之后创建它们即可.

If you are forking and then threading and the mutexes are meant to only apply to the child process and its threads then just create them after the fork.

这篇关于没有exec的fork和共享对象使用的pthread_mutex_t的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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