popen方法在Android NDK [英] popen on android NDK

查看:2135
本文介绍了popen方法在Android NDK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

时的popen不支持的Andr​​oid NDK?

Is popen not supported by android NDK?

我读<一href="http://groups.google.com/group/android-ndk/browse_thread/thread/96dc739136be46dd/937528d963b52109?lnk=gst&q=popen#937528d963b52109"相对=nofollow>这个页面并想知道如果这是真的

I read this page and wondering if this is true

同样是可能符合POSIX的popen(),但它不是当前   通过仿生支持,所以你不能使用,在Android的JNI。相反,你   大概可以使用该系统()和管道输出到一个文件,然后   事后读取该文件。貌似Java的方式,将   如果你会做在Java渲染清洁。

The same is possible with POSIX popen() but it is not currently supported by bionic, so you can't use that in Android JNI. Instead you can probably use the system() and pipe the output to a file and then read that file afterwards. Looks like the Java approach will be cleaner if you will be doing the rendering in Java.

不过,我也看到有人建议使用的popen。我也尝试过自己,但有时我的应用程序崩溃,我不知道为什么。

But I also read someone suggesting to use popen. I also tried it myself but sometime my app crashes and I dont know why.

时popen方法在安全的Andr​​oid NDK使用?

Is popen safe to use in android ndk?

推荐答案

我认为这取决于什么版本的NDK中使用的是

I think it depends on what version of the NDK you are using

也期待姜饼源代码树中的仿生我找到的popen的实现。 执行可能不正确的libc 100%POSIX,但它至少官能到一定程度。

Also looking in the gingerbread source tree for bionic I did find an implementation of popen. The implementation might not be 100% posix of libc correct, but it is at least functional to some degree.

使用NDK V6,下面的例子编译没有任何问题,它运行Android设备上。

Using NDK v6, the following example compiles without any problems, and it runs on my android device.

#include <stdio.h>
#include <stdlib.h>
int main()
{
 FILE *fpipe;
 char *command="/system/bin/ps";
 char line[256];
 if ( !(fpipe = (FILE*)popen(command,"r")) ) exit(1)
 while ( fgets( line, sizeof line, fpipe))
 {
   puts(line);
 }
 pclose(fpipe);
}

更新: 似乎比popen方法在pre ICS使用的vfork()版本,而不是fork()的,以及那些vfork的()已知会导致堆栈损坏。

UPDATE: Seems than popen on pre ICS versions used vfork() instead of fork(), and those vfork() are known to cause stack corruption.

因此​​,从ICS以后的popen应保存到使用,但在较早的Andr​​oid版本也可用,但柠车。

So from ICS onward popen should be save to use, but on earlier android versions it is available but verry buggy.

这篇关于popen方法在Android NDK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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