English 中文(简体)
JNA 系统 () 是否避免了叉
原标题:Does JNA system() avoid a fork

我试图找到一个不使用叉子的替代 Java Runtime. exec () 。 问题在于我们的 JVM 消耗了大部分的内存和运行时间。 exec fork on that process 可能引起记忆问题, 即使是在书写和超版的拷贝( 经常在堆叠流中讨论, 见< a href=> https:// stackoverflow. com/ questions/ 287664/java- runtime- getruntime- exec- oproups" > Java Runtime. getRuntime (. exec () 替代品 < /a > )。

在另一个堆积流柱上,提出了使用日本国家航空局的解决办法,但对这一解决办法没有意见,也没有高分:

同样,这里也提出了一个类似的日本皇家陆军解决方案:http://sanjittmohanty.wordpress.com/2011/12/20/overfound-runtime-exec-havoc- with-jna/

我的问题是:是否使用日本国家航空局(JNA)使系统呼叫防止叉子,它是否避免了随后叉子可能造成的内存分配问题?我使用的代码是:

public class TestJNA {
 private interface CLibrary extends Library {
    CLibrary INSTANCE = (CLibrary) Native.loadLibrary("c", CLibrary.class);
    int system(String cmd);
 }
 private static int exec(String command) {
    return CLibrary.INSTANCE.system(command);
 }
 public static void main(String[] args) {
    exec("ls");
 }
最佳回答

可能不是。 您重新拨打的 < code> system () 的安装几乎肯定使用 < code> fork () 本身 -- C 的安装将看起来像 :

int system(const char *cmd) {
    if (fork() == 0) {
        execl("/bin/sh", "-c", cmd);
        _exit(1);
    }
    int result;
    wait(&result);
    return WEXITSTATUS(result);
}

一个更可行的解决办法是维持一个小的外部过程的通信线(例如管道),这个过程可以产生出你所需的子过程。 Android做了类似的事情;外部过程被称为“zygote ” 。

问题回答

http://pubs.opengroup.org/onlinepubs/009604499/conferences/system.html" rel="nofollow" {code>system 系统 < a href=> http://pubs.opengroup.org/onlinepubs/009604499/ conferences/fork.html" rel="nofolf"\\\\code>fork (或等效物),而日本国家环境署对此不能采取任何行动。

我不认为人们建议您使用 JNA 来调用 < code> system , 而是使用实际的系统调用。 例如, 该博客给出了 < code>chmod 和 < code> chown 的示例。 因此, 使用您的示例, 而不是使用 JNA 来调用 < code> system( Is) , 调用 < code> Opendir 和 < code> readdir (当然, 不论你真正拨打什么电话, 都适用同样的做法 ) 。

您可能需要做 DLL 来包扎想要的功能 。

没有叉子( ) 就不可能执行系统 。

Then how system() does work? if it still using fork? i had a problem with this error "java.io.IOException: error=12, Cannot allocate memory" and this solved it for me:

private interface CLibrary extends Library {
            CLibrary INSTANCE = (CLibrary) Native.loadLibrary(("c"), CLibrary.class);
        int system(String cmd);
    }

    private static int exec(String command) {
        return CLibrary.INSTANCE.system(command);
    }




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签