C-执行程序是否必须立即在多线程进程中跟踪fork? [英] C - does exec have to immediately follow fork in a multi-threaded process?

查看:93
本文介绍了C-执行程序是否必须立即在多线程进程中跟踪fork?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况: 我有一个用C编写的多线程程序.如果其中一个线程分叉,则使用exec()将子进程替换为另一个进程,并且父进程等待子进程退出.

Situation: I have a multithreaded program written in C. If one of the threads forks, the child process is replaced by another using exec() and the parent waits for the child to exit.

问题: 在fork()创建子进程之后,有几行代码编译了在以下exec()命令中使用的参数.

Problem: After the child process is created by fork() there are a few lines of code that compile the arguments to be used in the following exec() command.

假设 我是否正确假设在由fork()创建的子进程与被exec()替换的子进程之间,子进程-作为父进程的副本-将拥有父进程的所有线程,因此这些线程会运行-尽管时间很短?

Hypothesis Am I correct in assuming that in the time between the child process being created by fork() and being replaced by exec(), the child process - being a copy of the parent - will have all the threads of the parent and therefore these threads will run - albeit for a very brief period?

如果是这样,在fork()之后立即调用exec()的正确解决方案是吗?

If so, is the correct solution to call exec() immediately after fork()?

推荐答案

只有调用fork的线程将在新进程中运行.但是,在exec之前可以调用的函数有一定的限制.来自 fork :

Only the thread that calls fork will be running in the new process. However, there are limits to which functions you can call before exec. From fork:

应使用 单线程.如果是多线程 流程调用fork(),新流程 应当包含调用的副本 线程及其整个地址空间, 可能包括 互斥体和其他资源. 因此,为避免错误, 子进程只能执行 异步信号安全操作,直到 exec功能之一的时间 叫做.叉 处理程序可以通过以下方式建立 的 pthread_atfork() 函数中 为了维持申请 fork()调用中保持不变.

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 handlers may be established by means of the pthread_atfork() function in order to maintain application invariants across fork() calls.

我相信这意味着您通常应该没问题,只要有任何多线程 库正确使用pthread_atfork.

I believe this means you should generally be okay, as long as any multi-threaded libraries use pthread_atfork properly.

pthread_atfork 页进一步说明了该库如何可以保护自己:

The pthread_atfork page explains further how the library can protect itself:

预期用途是准备 处理程序获取所有互斥锁,并且 其他两个分叉处理程序释放 他们.

The expected usage is that the prepare handler acquires all mutex locks and the other two fork handlers release them.

例如,应用程序可以提供 准备程序,获取 必要的互斥库 维持和供应孩子和父母 释放那些互斥量的例程, 从而确保孩子得到 状态的一致快照 库(并且没有互斥锁 左搁浅).或者,一些 图书馆也许能够提供 一个子例程,用于重新初始化 库和所有库中的互斥锁 关联状态到某个已知值 (例如,当 图片最初执行).

For example, an application can supply a prepare routine that acquires the necessary mutexes the library maintains and supply child and parent routines that release those mutexes, thus ensuring that the child gets a consistent snapshot of the state of the library (and that no mutexes are left stranded). Alternatively, some libraries might be able to supply just a child routine that reinitializes the mutexes in the library and all associated states to some known value (for example, what it was when the image was originally executed).

这篇关于C-执行程序是否必须立即在多线程进程中跟踪fork?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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