cateof
cateof

Reputation: 6758

preventing child process becoming an orphan process

My Linux process has 4 children. After some execution time all children adopted by the init process. How do we prevent this situation? (this is not the case with Zombie processes).

The process is written in C and the OS is Linux. My code calls waitpid! What might be the problem? In 99,99% we don't have this problem.

Last update: What if someone executes "kill -9 "? This terminates immediately the parent process and leaves the children orphan.

Upvotes: 2

Views: 3951

Answers (3)

DTdev
DTdev

Reputation: 562

Check from main page for your waitpid API parameters, and make sure your parent process should not be over before all child processes are finished. Can you post your code here?

Upvotes: 0

Michael Trausch
Michael Trausch

Reputation: 3165

If your processes are being reparented by init, that means that their parent process has died. When a process' parent dies, init adopts it so that it can reap the zombie by wait()ing on the child when it (that is, init) receives SIGCHLD.

If you do not want init to become the parent of your children, you will have to ensure that your process lives until all of your children have died and been reaped by your program.

Upvotes: 4

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798814

Wait for the children to exit before exiting yourself. See the wait(2) man page for more details.

Upvotes: 1

Related Questions