Problem with Runtime.exec and Android
I have an issue when I'm using Runtime.exec with my Android device and just can't figure out why it happens...
Here is an example of the tests I did :
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(开发者_StackOverflow中文版savedInstanceState);
setContentView(R.layout.main);
Runtime runtime = Runtime.getRuntime();
for(int i=0; i< 20; i++){
Log.d("TESTEXEC", "EXEC N°"+i+" : BEGIN");
try {
Process process = runtime.exec("/system/bin/ps");
process.getErrorStream().close();
process.getOutputStream().close();
process.getInputStream().close();
process.waitFor();
} catch (Throwable e) {
e.printStackTrace();
}
Log.d("TESTEXEC", "EXEC N°"+i+" : END");
}
}
}
So, basically, when this snippet works, it prints this :
EXEC N°0 : BEGIN
EXEC N°0 : END
EXEC N°1 : BEGIN
EXEC N°1 : END
...
EXEC N°19 : BEGIN
EXEC N°19 : END
But instead of that, sometimes (it looks like it's a bit random), it just prints
EXEC N°0 : BEGIN
And everything locks... The whole things seems to crash into the runtime.exec("..."), and the process.getErrorStream.close() is never called. Another strange thing : The bug always occurs on the first exec.
Here is what I get with DDMS :
I also retrieved the /data/anr/traces.txt, but it's not really useful :
----- pid 11489 at 2011-05-16 15:15:10 -----
Cmd line: com.testexec
DALVIK THREADS:
"main" prio=5 tid=1 NATIVE
| group="main" sCount=1 dsCount=0 s=N obj=0x4001d8d0 self=0xcca0
| sysTid=11489 nice=0 sched=0/0 cgrp=unknown handle=-1345021856
at java.lang.ProcessManager.exec(Native Method)
at java.lang.ProcessManager.exec(ProcessManager.java:224)
at java.lang.Runtime.exec(Runtime.java:196)
at java.lang.Runtime.exec(Runtime.java:285)
at java.lang.Runtime.exec(Runtime.java:218)
at com.testexec.MainActivity.onCreate(MainActivity.java:17)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2629)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2681)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4641)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at dalvik.system.NativeStart.main(Native Method)
"java.lang.ProcessManager" daemon prio=5 tid=8 NATIVE
| group="main" sCount=1 dsCount=0 s=N obj=0x460969f0 self=0x240f58
| sysTid=11496 nice=0 sched=0/0 cgrp=unknown handle=2363544
at java.lang.ProcessManager.watchChildren(Native Method)
at java.lang.ProcessManager$1.run(ProcessManager.java:87)
"Binder Thread #2" prio=5 tid=7 NATIVE
| group="main" sCount=1 dsCount=0 s=N obj=0x460895a0 self=0x1248e0
| sysTid=11495 nice=0 sched=0/0 cgrp=unknown handle=1236304
at dalvik.system.NativeStart.run(Native Method)
"Binder Thread #1" prio=5 tid=6 NATIVE
| group="main" sCount=1 dsCount=0 s=N obj=0x46086cf8 self=0x143480
| sysTid=11494 nice=0 sched=0/0 cgrp=unknown handle=1324096
at dalvik.system.NativeStart.run(Native Method)
"Compiler" daemon prio=5 tid=5 VMWAIT
| group="system" sCount=1 dsCount=0 s=N obj=0x46084558 self=0x144560
| sysTid=11493 nice=0 sched=0/0 cgrp=unknown handle=1153256
at dalvik.system.NativeStart.run(Native Method)
"JDWP" daemon prio=5 tid=4 VMWAIT
| group="system" sCount=1 dsCount=0 s=N obj=0x460842a0 self=0x12dc10
| sysTid=11492 nice=0 sched=0/0 cgrp=unknown handle=1152968
at dalvik.system.NativeStart.run(Native Method)
"Signal Catcher" daemon prio=5 tid=3 RUNNABLE
| group="system" sCount=0 dsCount=0 s=N obj=0x460841e8 self=0x124d00
| sysTid=11491 nice=0 sched=0/0 cgrp=unknown handle=1199296
at dalvik.system.NativeStart.run(Native Method)
"HeapWorker" daemon prio=5 tid=2 VMWAIT
| group="system" sCount=1 dsCount=0 s=N obj=0x441e2ee8 self=0x11fdd8
| sysTid=11490 nice=0 sched=0/0 cgrp=unknown handle=1185776
at dalvik.system.NativeStart.run(Native Method)
----- end 11489 -----
I'm kinda desesperate right now, so if anyone has an idea, I'd really appreciate it.
That's most likely the bug fixed in this Github commit. In essence, it deadlocks directly after fork() but before doing the actual exec() because somebody tried to malloc.
精彩评论