使用" EXEC()"与NDK [英] Using "exec()" with NDK

查看:137
本文介绍了使用" EXEC()"与NDK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我记得读书的地方,最好不要用EXEC的C code,通过NDK编制之内。

I remember reading somewhere that it isn't advisable to use "exec" within the C code, compiled by NDK.

什么是推荐的方法?难道我们试图推EXEC code到Java的空间;也就是说,所以JNI(或应​​用程序)会产生新的进程,(和有关其中的结果向下传递回NDK)?

What is the recommended approach? Do we try and push the EXEC code up to the Java-space; that is, so the JNI (or application) spawns the new process, (and where relevant passes the results back down to the NDK)?

推荐答案

首先,它不建议以使用 EXEC 。您的所有code一般是应该生活在一个单一的过程,是你的主要Android应用程序,通过Android框架进行管理。任何其他进程容易获得由系统随时杀死了(虽然在实践中不present的Andr​​oid版本出现,据我所看到的)。

First off, it's not recommended to use either fork or exec. All of your code is generally supposed to live in a single process which is your main Android application process, managed by the Android framework. Any other process is liable to get killed off by the system at any time (though in practice that doesn't happen in present Android versions as far as I have seen).

据我了解的理由很简单,Android的框架不能很好地管理你的应用程序的生命周期和生命周期,如果你去产卵等过程。

The rationale as I understand it is simply that the Android frameworks can't properly manage the lifetime and lifecycle of your app, if you go to spawn other processes.

您在这里有没有真正的选择,但为了避免在所有启动其他可执行文件。这意味着你需要把你的可执行文件code到您直接链接到应用程序库,并呼吁使用NDK正常的函数调用,从Java code。通过JNI触发。

You have no real alternative here but to avoid launching other executables at all. That means you need to turn your executable code into a library which you link directly into your application and call using normal NDK function calls, triggered by JNI from the Java code.

是比较困难的。如果你真的需要一个多进程模式,并希望符合规则的信中,你需要为Android框架,从受精卵进程fork你安排。要做到这一点,你应该运行在其中规定在在 AndroidManifest一个不同的进程中运行不同的服务你所有的背景code。 XML

Is more difficult. If you really need a multi-process model, and want to fit within the letter of the rules, you need to arrange for the Android framework to fork you from its Zygote process. To do this, you should run all your background code in a different Service which is stated to run in a different process within the AndroidManifest.xml.

要采取这种极端,如果你需要在内存保护和隔离的原因不同进程中code运行多个相同的情况下,你可以做什么的Andr​​oid浏览器所做的:

To take this to extremes, if you need multiple identical instances of the code running in different processes for memory protection and isolation reasons, you can do what Android Chrome does:


  • 运行所有的背景/分叉code在服务的一个子类

  • 创建多个子类的

  • 列出每个子类中作为一个单独的服务你的的Andr​​oidManifest.xml 每一个不同的过程属性

  • 在主code,记不清楚哪些服务你已经解雇了,而不是和使用管理它们 startService / stopService

  • Run all your background/forked code in a subclass of Service
  • Create multiple subclasses of that
  • List each of these subclasses as a separate service within your AndroidManifest.xml each with a different process attribute
  • In your main code, remember exactly which services you've fired up and not, and manage them using startService/stopService.

当然,如果你已经把你的本地code到库,而不是可执行文件,你可能不需要反正。剩下的唯一的理由使用是实现内存保护/隔离。

Of course, if you've turned your native code into a library rather than an executable, you probably don't need fork anyway. The only remaining reason to use fork is to achieve memory protection/isolation.

在实践中相当多的应用程序忽略了这一切,并使用 / EXEC 家乡$ C $内ç直接。此刻,它的工作原理,至少对于短正在运行的任务。

In practice quite a lot of apps ignore all this and use fork/exec within their native code directly. At the moment, it works, at least for short-running tasks.

这篇关于使用" EXEC()"与NDK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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