andorid: child process stuck in zombie state due to parent not reaping

This post is to demonstrate a condition in which the child process stuck in zombie state due to parent process not reaping it.

example program
andorid: example: child process stuck in zombie state due to parent process

software of testing device
LA.BF64.1.1-06510-8×94.0 with Android 5.0.0_r2(LRX21M) and Linux kernel 3.10.49.

hardware of testing device
Architecture arm-v8 cortex-53.

log of running this program

$ adb shell /data/child-process-zombie-due-to-parent
7859:7859, program is starting
7859:7859, parent process after fork
7861:7859, child process after fork
7861:7859, child process is ready to _exit(2)

code flow

  • process 26153’s main thread 26153:26153 starts
  • thread 26153:26153 creates thread 26153:26155
  • thread 26153:26155 runs in thread_routine and calls printf in busy loop
  • thread 26153:26153 forks child process 26158
  • child process 26158 call printf() and hits deadlock while get file lock of stdout
  • analysis: why the child process stuck in zombie state
    If a child process exits, its state will become zombie and waits for its parent process to reap it. However, if its parent process doesn’t reap it, then the state of this child process will stay in zombie state.

    how to make child process leave zombie state

  • Kill the parent process, then the child process will immediately be reparented to init process which reaps its child process.
  • As above, if the parent process exits and doesn’t run forever, then its child process will be reparented to init process.
  • Let the parent process reap the child process, the patch fix child process stuck in zombie state due to parent process shows how to do it.
  • conclusion
    The child process become zombie after termination. If its parent process is a service and doesn’t exit, then the parent process needs to waitpid to reap its child process.

    Leave a comment