android,JNI创建进程,使用fork()

时间:2023-03-09 18:01:03
android,JNI创建进程,使用fork()
long add(long x,long y)
{
pid_t fpid; //fpid表示fork函数返回的值
int count=0;
fpid=fork();
if (fpid < 0)
LOGI("error in fork!");
else if (fpid == 0) {
LOGI("i am the child process, my process id is %d/n",getpid());
count++;
return x;
}
else {
LOGI("i am the parent process, my process id is %d/n",getpid());
count++;
return y;
}
}

JNI调用native 方法 add函数

父进程输出了打印信息,子进程没有反应。

父进程pid  17247

DDMS中查看Threads,

TID  Status   utime   stime  Name

17247 Native  19        12     main

adb shell ps命令查看

USER       PID   PPID   VSZ     RSS  STAT  NAME

root       152  1              S    zygote

u0_a66   17247  152   297120  44096  S  com.example.jni

u0_a66   17520  17247  0    0    Z  com.example.jni

貌似android的应用程序进程都由zygote进程创建

子进程确实创建了,但是没有运行,占用的内存为0(VZS,RSS),处于僵尸状态。

Z :该程序应该已经终止,但是其父程序却无法正常的终止他,造成 zombie (疆尸) 程序的状态

说明android应该不支持在JNI的native方法中创建进程,因为一个进程应该运行在一个虚拟机上,在这里如何能实现虚拟机的机制。