What causes a defunct process on the Linux system and how can you avoid it?

C++
When you create a process, it sticks around until its parent reaps it. 
(If its parent exits first, it will get auto-reaped.) A process can reap
its children using wait or waitpid. It can also cause it's children to 
be automatically reaped by using local $SIG{CHLD} = 'IGNORE'; before 
creating the child.
Source

Also in C++: