如何fork JVM?
可能的重复:
Java - C-Like Fork?
我想知道如何分叉子 JVM从 JDK 或者甚至可以这样做吗?
一些框架(例如 hadoop)会为特定任务创建一个子 JVM,因此请对这个主题进行一些说明。
谢谢!
Possible Duplicate:
Java - C-Like Fork?
I wanted to know how is it possible to fork a child JVM from a JDK or even is it possible to do so?
Some frameworks like hadoop fork a child JVM for specific tasks thus Please throw some light on the subject.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Fork 通常与 Spawn 混淆。
Spawn是fork + exec(意思是启动一个进程
它取代了当前的并继承了一些
它的外部资源,例如打开的套接字)。
Spawn 可在 Java 中使用(通过 System.exec 等)。
Java 中没有 Fork,因为它会带来极大的问题。
Fork 需要克隆地址空间。
无法有效执行此操作的内核支持
在大多数非 Unix 操作系统中;例如 U/win 和 Cygwin 一直在挣扎
在 Win32 下模拟 fork。
在 Java 等语言中,垃圾收集器
& JIT 编译器倾向于接触 Vmem 页面,例如
分叉后该空间不会长期保持共享状态。
克隆有很多副作用。
例如,输入或输出缓冲区将在所有
分叉的孩子们。 SysV 共享内存将被分离。
大多数语言和许多库只是拒绝
支持分叉。这包括 (POSIX) 线程 API ;
子进程在执行之前不允许使用线程
(即,成为一个生成)!
Perl 使用 fork 是因为它的内部结构与 C/Unix 极其接近,
而且它的多线程性能很糟糕。
Unix shell 设计上使用 fork,这就是它的快照方式
所有局部变量和状态。这也是为什么没有
适用于非 Unix 环境的不错的 Unix shell。
Fork is commonly confused with spawn.
Spawn is fork + exec (which means start a process
which replaces the current one and inherits some of
its external resources, such as open sockets).
Spawn is available in Java (via System.exec and the like).
Fork is not in Java, because it would be extremely problematic.
Fork requires cloning of the address space.
The kernel support to do this efficiently is not available
in most non-Unix OSes; e.g. U/win and Cygwin have struggled
to emulate fork under Win32.
In a language such as Java, the garbage collector
& JIT compilers would tend to touch Vmem pages such
that the space would not long remain shared after fork.
Cloning has many side effects.
E.g., input or outputs buffers will be processed in all
of the forked children. SysV shared memory will be detached.
Most languages and many libraries simply refuse to
support forking. This includes the (POSIX) threading API ;
The child is not allowed to use threads until it execs
(i.e., becomes a spawn) !
Perl uses fork because its internals are extremely close to C/Unix,
and its multithreading is abysmal.
Unix shell uses fork by design, which is how it snapshots
all local variables and state. That's also why there is no
decent Unix shell for a non-Unix environment.
总的来说,我认为这在你的意思上是不可能的。您可以使用 System.exec() 来分离新进程,并可以通过这种方式调用新的 JVM。请注意,您当然可以直接从本机代码调用
fork()
,但用海报的话说 这里,“不要。”In general, I don't believe this is possible in the sense you mean. You can
System.exec()
to spin off a new process, and can call a new JVM that way. Note that you can certainly callfork()
directly from native code, but in the words of a poster here, "Don't. Just don't."