Thread: when parent process close, how to close the child?

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    29

    Exclamation when parent process close, how to close the child?

    can someone provide an example, where if the parent process quits for any reason, then the child process will also close?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    On what OS?

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I'm assuming this is Linux or some *NIX. IIRC from something I read here, Windows doesn't keep a process hierarchy, everything is a "child" of the master process (the equivalent of init in Linux). In Linux, if the parent process dies, the OS will reap all the children, and their new parent will be init. They will be allowed to finish normally. Generally the parent wants to wait for it's children, using the wait/waitpid functions.

    The way around this is to track all your children and kill them by sending SIGKILL to their process numbers. That means you will probably need a special signal handler in the child processes that catches the SIGKILL signal, cleans up accordingly and sends a SIGKILL to any child processes it may have, etc.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    29
    yeah its linux, and i want to end the child because of an infinite loop, so it would really help if you can provide an example of how to do the signal code.

    As well if you do know, i am trying to combine a select code (using socket fd's) with a pipe fd, but its not working, if you can also show an example of that, it would be very helpful

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by omega666 View Post
    yeah its linux, and i want to end the child because of an infinite loop, so it would really help if you can provide an example of how to do the signal code.
    I don't feel like writing you a tutorial. From your console/terminal, type "man 2 signal", "man 2 sigaction" and "man 2 kill". That should give you a start into sending and handling signals. Google around for "C signal handling in Linux", and you should get some decent results.

    As well if you do know, i am trying to combine a select code (using socket fd's) with a pipe fd, but its not working, if you can also show an example of that, it would be very helpful
    That's really vague. I'm not sure how you plan to use the two together, so I couldn't write you a useful example even if I wanted to. Again, Google around, and make sure you fully understand both select and pipes before combining them.

    If you have specific questions about either of these, post back with some code you've tried, and the exact problem, and we'll gladly help you out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble understanding parent and child processes
    By cardinals03 in forum C++ Programming
    Replies: 11
    Last Post: 10-04-2009, 05:51 PM
  2. Understanding fork()
    By NuNn in forum C Programming
    Replies: 8
    Last Post: 02-27-2009, 12:09 PM
  3. child classes calling parent constructors (with arguments)
    By reanimated in forum C++ Programming
    Replies: 3
    Last Post: 05-01-2006, 10:52 AM
  4. pipe
    By smart girl in forum C Programming
    Replies: 4
    Last Post: 04-30-2006, 09:17 AM
  5. Ghost in the CD Drive
    By Natase in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 10-12-2001, 05:38 PM