Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Red Ant

Zombie processes (UNIX)

This topic is 5273 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

If I do a fork to create a new process, the parent process has to do a wait() at some point to reap the child process when it has finished, right? If I don''t then the child process becomes a zombie (process table doesn''t get cleaned up properly or something). My problem is wait(), waitpid() and whatever they''re called in the various programming languages, are blocking calls and I don''t want my parents process to wait until the children are done. Much rather I want my parent process to exit immediately after creating the children. I suppose I have to somehow ''disassociate'' the children from their parent for this to work cleanly. Anyone know a workaround? :confused:

Share this post


Link to post
Share on other sites
Advertisement
Wait only blocks if the child has not yet exited.

If you are interested in knowing when the child has died asynchronously, you can receive a signal which will be invoked when a child dies (SIGCHLD), wait is then guaranteed to return immediately.

Additionally, you can give wait() an option WNOHANG which will cause it to return immediately, even if there are no dead children around.

Zombies are not "because the process list has not been cleaned up properly", they exist for a reason - so that the parent process can get status information about their exit (exit code, etc).

If the parent dies before the child, then they become orphans, and get assigned a new parent (I''m not 100% sure how this is done, in some cases they get assigned init as their new parent).

Mark

Share this post


Link to post
Share on other sites
First off, thanks for helping! Now let me see if I got this straight.


quote:

Additionally, you can give wait() an option WNOHANG which will cause it to return immediately, even if there are no dead children around.



Hmmm, this sounds useful ...

quote:

If the parent dies before the child, then they become orphans, and get assigned a new parent (I''m not 100% sure how this is done, in some cases they get assigned init as their new parent).



So basically my parent process could exit immediately after creating the child process without having to worry about the child at all?


Share this post


Link to post
Share on other sites
Thanks Shadowdancer, that's really interesting stuff! I'm still not quite sure if I actually need to make my child processes daemons, though.

In regards to what markr said about processes which have become orphans, I ran a test with my start.pl script (I'm writing in Perl here, but I figure the principle is the same regardless of the programming language used) which doesn't do anything more than fork() a child process and then exit immediately:

on the tc-shell:
perl start.pl;ps -f

This revealed that after start.pl had exited, the program started by it had a parent PID of 1 (which I'm assuming is the init process), so markr would be correct.

Maybe I should explain a little more about what I'm actually trying to achieve. Basically I have a central host (which I call the 'issuing' host) and a bunch of remote hosts (which I call the 'executing' hosts). The issuing host is supposed to establish a secure shell connection to each of the executing hosts and start a number of big processes that aren't going to finish any time soon. However, since I'm not sure just how many ssh-connections the issuing host can maintain at any one time, I don't wanna put too high of a strain on the issuing hosts resources and thus want the ssh-connection to be closed at the soonests possible point in time. That's why I want to simply say 'perl start.pl', have that script fork() and start a bunch of new processes in a hurry (these processes then do the actual work) and exit, so the ssh connection can then be discarded.
Is it necessary that I do the whole detaching the child process from the tty dance for this to work without problems?

P.S. Looks like it is. The way I'm doing it now, the ssh connection still remains open. Hrmpf ...

[edited by - Red Ant on June 4, 2004 5:27:49 AM]

Share this post


Link to post
Share on other sites
Of course you need to detach from the console, otherwise the program would be killed when the session ends. You might want to look at how the "screen" tool handles things, that might give you some good pointers.

Share this post


Link to post
Share on other sites
Yep, that worked! I''m still somewhat confused in regards to fork() and waitpid(). All snippets I''ve seen suggested that I have to waitpid or I''ll accumulate zombies. However ... apparently I''m not, regardless of whether I waitpid or not. =|

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!