从多线程应用程序菌种 [英] Spawn process from multithreaded application

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

问题描述

我有一个情况我需要从一个非常大的,多线程应用程序,这是我没有完全掌控之内生成一个帮手的过程。

I have a situation where I need to spawn a helper process from within a very large, multithreaded application, which I do not have complete control over.

现在,我使用叉() / 执行exec()。这工作了很多时间,但在某些情况下,孩子的 Exec之前古怪崩溃()发生。我怀疑这是因为叉() ING多线程应用程序通常被认为是一个非常糟糕的主意。

Right now I'm using fork()/exec(). This works a lot of the time, but in some circumstances the child crashes weirdly before the exec() happens. I'm suspecting this is because fork()ing multithreaded applications is generally considered to be a Really Bad Idea.

我真的,真的很喜欢的方式来原子启动一个进程,而不叉() ING家长:所有的文件描述符关闭,环境搭建我的方式希望,CWD集等,这应该避免的所有的恐怖叉() ING我的父母多线程应用程序,并处理文件描述符继承等的posix_spawn()应该是理想的。不幸的是,在Linux上,的posix_spawn()使用叉()和实施 EXEC() ...

I would really, really like a way to start a process atomically, without fork()ing the parent: with all file descriptors closed, environment set up the way I want, CWD set, etc. This should avoid all the horror of fork()ing my multithreaded parent app, and dealing with file descriptor inheritance, etc. posix_spawn() should be ideal. Unfortunately, on Linux, posix_spawn() is implemented using fork() and exec()...

的vfork()的定义,直到孩子叫暂停父进程执行exec()。这似乎更像是我想要的,但我的理解是,的vfork()被普遍认为是历史的遗迹,这些天,相当于叉() ---这是仍然如此?

vfork() is defined to suspend the parent process until the child calls exec(). This would appear to be more like what I want, but my understanding was that vfork() is generally considered a historical relic these days and is equivalent to fork() --- is this still the case?

什么是处理这个问题的最不坏的办法吗?

What's the least bad way of dealing with this?

需要注意的是:


  • 我无法产卵我的进程的线程启动之前(因为我不能运行在该点code)

  • 我无法重新设计我的应用程序并不需要辅助过程中,由于外部需求

  • 我无法产卵辅助进程之前暂停我的所有线程,因为他们不属于我

这是在Linux上。 Java的参与,但所有的我的的code是在C

This is on Linux. Java is involved, but all my code is in C.

推荐答案

调用应该是安全的如果的你限制自己的原始系统调用(系统调用(SYS_fork) syscalll(SYS_execve,...)等)。呼叫到任何的glibc的套路,你会在很多麻烦。

Calling fork should be safe if you limit yourself to "raw" system calls (syscall(SYS_fork), syscalll(SYS_execve, ...), etc.). Call into any glibc routine, and you'll be in a lot of trouble.

调用的vfork 并不的你想要什么:只调用线程的vfork 被暂停,其他线程将继续运行(在相同的地址空间作为vforked孩子)。这极有可能是你的生活变得一团糟。

Calling vfork is not at all what you want: only the thread that called vfork is suspended, and other threads will continue to run (and in the same address space as the vforked child). This is very likely to complicate your life.

调用克隆直接是可能的,但非常棘手。我们有允许从多线程应用程序的子进程的安全分叉(可惜不是开源)的实现。这code是的非常的技巧,以及令人惊讶的长。

Calling clone directly is possible, but exceedingly tricky. We have an implementation that allows for safe forking of child processes from multithreaded apps (unfortunately not open source). That code is very tricky, and surprisingly long.

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

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