Thread: Fork multiple processes()??

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

    Fork multiple processes()??

    How do you kill all the processes of a fork()??

    Having some massive problems with not being able to close a program due to other processes of a fork still being open.

    (For source code, check out http://cboard.cprogramming.com/showt...405#post806405 )

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, you could just send a SIGKILL or SIGINTR to them...

    Or just wait for them to die by themselves using wait() or waitpid().

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    167
    Quote Originally Posted by matsp View Post
    Well, you could just send a SIGKILL or SIGINTR to them...

    Or just wait for them to die by themselves using wait() or waitpid().

    --
    Mats
    Which one's better?

    Will those actually close the program cleanly? (sockets in this case)

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Killing the process will close sockets.

    Obviously, it's better to let the parent process wait for the processes to exit by themselves, but if the process has for some reason locked up, then you can't really wait for it...

    --
    Mats

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Paul22000 View Post
    Having some massive problems with not being able to close a program due to other processes of a fork still being open.
    Strange problem to be having. A process should be able to exit even while children are still running. Unless you're on some weirdo platform -- more details?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    167
    Quote Originally Posted by brewbuck View Post
    Strange problem to be having. A process should be able to exit even while children are still running. Unless you're on some weirdo platform -- more details?
    I'm using Linux, and gcc.

    All my source code and a makefile can be found at http://cboard.cprogramming.com/showt...405#post806405

    Any ideas on how to close the processes?

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Paul22000 View Post
    I'm using Linux, and gcc.

    All my source code and a makefile can be found at http://cboard.cprogramming.com/showt...405#post806405

    Any ideas on how to close the processes?
    That was already answered, but anyway...

    1. Terminate them with kill()
    2. Wait for them to exit normally with wait()
    3. Use some other mechanism of your own devising to tell the subprocesses to exit cleanly.

    But on Linux, there is no reason why a parent process cannot exit before its children do. What makes you think it cannot?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Is the problem this:
    Code:
    			while( 1 )
    			{
    				int pid = fork();
    Basically, every time you enter or receive something, you fork another process. That's a tad excessive I think.

    That can be a bit difficult to deal with, but I think both branches of the fork will block on waiting for input, so if you start another shell, you should be able to kill the first fork, and then go down the tree of forked processes.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    167
    Quote Originally Posted by brewbuck View Post
    That was already answered, but anyway...

    1. Terminate them with kill()
    2. Wait for them to exit normally with wait()
    3. Use some other mechanism of your own devising to tell the subprocesses to exit cleanly.

    But on Linux, there is no reason why a parent process cannot exit before its children do. What makes you think it cannot?
    It doesn't allow the client to disconnect even if I put close(sock) all over the place lol

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Same pipe to multiple processes?
    By Ironic in forum C Programming
    Replies: 7
    Last Post: 10-25-2008, 10:10 AM
  2. Fork Zombie Processes
    By valaris in forum Linux Programming
    Replies: 4
    Last Post: 09-05-2008, 08:16 AM
  3. shared libraries, datasegment and multiple processes
    By ashim_k1 in forum Linux Programming
    Replies: 1
    Last Post: 02-28-2008, 02:23 PM
  4. Multiple processes
    By cpsh007 in forum C Programming
    Replies: 10
    Last Post: 11-22-2007, 05:30 AM
  5. Using fork() to accept multiple clients
    By Lone in forum Networking/Device Communication
    Replies: 3
    Last Post: 03-17-2005, 10:07 AM