No, you are not reaping the child correctly. In your case, if the child process finishes before the parent process exits, the child will become a zombie. Then, when the parent process finishes, the child will be reparented to init
(whether it has finished and is a zombie, or is still running). init
is then reaping the child for you.
To reap the child, add a call to wait()
before exit
.
By the way, you have another bug - you are creating the pipe after the fork
, so the parent and child each create a (different) pipe - they're not connected. Move the if (pipe(...
up before the fork()
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…