LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-09-2014, 01:43 AM   #1
vlrk
Member
 
Registered: Dec 2008
Posts: 51

Rep: Reputation: 1
Avoiding Zombie Process


Hello All,

Zombie gets created when parent does not wait for its child and gets terminated.

Necessarily parent has to wait for end of all the child's it got created.

We can do this by waitpid() ( wait for all the child ) or handle SIGCHLD .

In SIGCHLD handler do all the cleanup , in order to make sure you release all the resources.

Can any body confirm here is my understanding
correct here related to Zombie.

Also i would like to know what does we mean proper cleanup in case of fork child?.

thanks in advance.
 
Old 12-09-2014, 05:14 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
> proper cleanup in case of fork child?

Nothing special: if/when wait (or waitpid, waitid) returns the exit status, the zombie-child cease to exist.
 
Old 12-09-2014, 07:14 AM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,667
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
The "zombie process" state is defined so that the parent process can easily rendezvous with the terminated child in order to collect its final status. It avoids a "race condition."

Specifically: When the child process ends, it must signal its parent. However, there will now be an unpredictable delay until the parent process, having been signaled, actually is dispatched to run on a particular CPU or core. During this interval, the dead child's process-table entry is marked as a "zombie," so that it won't be garbage-collected or re-used until the parent gets a chance to read it. Once the parent process has done so, the child's process-table entry can be discarded.

Since process-table zombies could "pile up" if the parent didn't attend to this duty, Linux takes sensible steps to prevent it. You must clean-up after your own kids.

Usually, parent-processes relegate all of their duties to their children. (Yeah, just like in real life ... ) Consider, for example, the classic init process: it forks a bunch of processes, then sits around and waits for them to die. It maintains the pool of processes according to the inittab, and it is the "grim reaper of last resort" for any orphans, but it does not, itself, do anything besides that. With this strategy, the overall design is greatly simplified and race-conditions are eliminated. The parent process maintains the pool of worker-children, but it does not do the work itself. It does not "wait on" anything other than child-termination.
 
1 members found this post helpful.
Old 12-09-2014, 01:15 PM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
The parent doesn't have to block while waiting either, waitpid() can be invoked with zero timeout so it won't block, but as stated, the parent is responsible to check for signals from the child as part of the closure for the lifespan of the child processes. I'm not sure it will always be SIGCHLD and in fact to me I wait on the child pid and then check exit status, many times I believe it is SIGTERM that I see, which indicates that the child exited normally. In fact, if the child was terminated by a signal; such as SIGKILL, then the status will indicate that the child was terminated by a signal and you can extract the signal from the status. There are several macros available, see waitpid(2) for information: WIFEXITED(status), WEXITSTATUS(status), WIFSIGNALED(status), WTERMSIG(status), WISTOPPED(status), WSTOPSIG(status) are some of the macros which return the relevant desired information from the status.
 
Old 12-09-2014, 02:15 PM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,667
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
All true.

That being said, and perhaps for the reasons said, I happen to think that by-far the cleanest design is to have a "father process" that does nothing except to manage the children. The process does a waitpid(), with no timeout, and to properly attend to whatever “child re-launching” responsibilities might be called-for based on which particular child terminates. (In fact, I have seen more than one system which actually more or less re-used init as its father-process ...)

In such a system design, child-processes do everything, including "soliciting incoming requests and waiting for them to arrive," and/or "disposing of completed requests." The father-process is aware of the individual requirements of each of its children, but in the overall scheme of what-it-is that the total system is doing, its role is totally passive.
 
Old 12-09-2014, 02:28 PM   #6
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Creating a Daemon to Launch and Monitor Your Processes

How to Kill Those Zombies
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
zombie process vikilinux Linux - Software 1 04-03-2014 01:39 AM
zombie process. vidyadhar03 Red Hat 2 03-20-2014 05:03 AM
Zombie process awanish.tiwari Linux - Newbie 5 01-21-2011 12:45 PM
zombie process sang_froid Linux - Server 1 05-25-2010 08:23 AM
zombie process ihatecomputers Linux - Software 2 03-24-2005 07:09 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration