Thread: How do I stop child processes from becoming defunct?

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    40

    How do I stop child processes from becoming defunct?

    Hello!

    I'm writing a shell and by now it works pretty well, even though I still haven't managed to incorporate ncurses/termios yet. But when I run a program from my shell using "&", and shutdown the program, the child process becomes defunct.

    As I understand it I need to handle the child process ending in some way, but how do I handle it without waiting for it? Or do I need two child processes, where the first starts the second and then waits for it, or would that just make the first child process defunct instead of the second?

    //Zarniwoop

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Zarniwoop View Post
    As I understand it I need to handle the child process ending in some way, but how do I handle it without waiting for it?
    The child should handle it's own ending just like a normal program. The parent does not have to wait for it -- that is the purpose of fork, &.

    They still use the same display, remember. If the child fails to do something (like exec), it should execute error handling code itself, including alerting the user. This could appear to interrupt output of a foreground or other backgrounded process, but that is what happens with bash.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Are you talking about zombies? If you are, you can waitpid() with WNOHANG to keep from getting zombies, but at the same time you'll not pause your main thread either.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by Kennedy View Post
    Are you talking about zombies? If you are, you can waitpid() with WNOHANG to keep from getting zombies, but at the same time you'll not pause your main thread either.
    To add to Kennedy's suggestion, I think if you call waitpid() after detecting a SIGCHLD, you can avoid calling waitpid() when there's no child ending.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    40
    Thank you for all the responses.

    itsme86: How do I detect "SIGCHLD" if I don't wait? Or can I check for incoming signals regularly? I assume that would be useful for ignoring Ctrl+C also?

  6. #6
    Registered User
    Join Date
    Dec 2009
    Posts
    3
    The old-time standard procedure for creating proper daemons is to first fork. Let the parent die. The child will then have init (pid=1) as its parent. Use setsid() to put the child in a process group of its own. Then close all file descriptors (including 0, 1 and 2) and then open 0, 1 and 2 to some harmless device such as /dev/null. Then set up signal handlers (or simply ignore them) and you should be all set.

    Most *nix daemons use this method and this is also why you tend to see the pid increased by two each time you start a daemon.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    40
    I don't understand how you mean?

    What I want to do is the following. I have created a shell, and when the user start e.g. emacs using e.g. "emacs test.c &", he/she can continue using the shell. When the user shuts down emacs, I want to avoid making the process defunct which it becomes now. Actually thanks to running waitpid with WNOHANG every time the user presses enter in the shell I avoid this now, but how would I do what you and itsme86 suggest?

    If I create a parent process, which then forks and runs the program, wouldn't that process become defunct in place of the child process?

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    It is totally normal for a process to appear <defunct> for a short duration after exit.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why isn't the execlp() function doing anything?
    By jsrig88 in forum C Programming
    Replies: 5
    Last Post: 10-12-2009, 10:09 AM
  2. Replies: 3
    Last Post: 10-15-2008, 09:24 AM
  3. forks and child processes
    By kpax in forum C Programming
    Replies: 1
    Last Post: 05-28-2008, 04:47 AM
  4. process programming
    By St0rM-MaN in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2007, 07:53 AM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM