sockets and child processes

This is a discussion on sockets and child processes within the Linux Programming forums, part of the Platform Specific Boards category; if I accept a socket, and then fork and handoff the connection to the child, then the child terminates, and ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    2,588

    sockets and child processes

    if I accept a socket, and then fork and handoff the connection to the child, then the child terminates, and closes the socket, is the socket still open in the parent? I think this may be the source of my problems with not being able to accept a socket after the server has been running for a while.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,701
    Yes. If the socket descriptor is created prior to fork(), then both the parent and child must call close() to prevent the descriptor from being leaked.

    If you are running out of file descriptors, you can check "/proc/[id]/fd" to see if open socket descriptors are listed there. If they are, you should see them growing

    gg

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    2,588
    Quote Originally Posted by Codeplug View Post
    Yes. If the socket descriptor is created prior to fork(), then both the parent and child must call close() to prevent the descriptor from being leaked.

    If you are running out of file descriptors, you can check "/proc/[id]/fd" to see if open socket descriptors are listed there. If they are, you should see them growing

    gg
    I found that on my production servers, they are chewing up FDs left and right, and on my dev server, where I am now running a thread to reclaim the FDs, the number of FDs in use with no clients connected is 7. I create several connections, and close them, and the number goes back to 7. so I think I got it fixed.

    thanks for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Concurrent chat, Sockets
    By jakemott in forum Linux Programming
    Replies: 6
    Last Post: 11-29-2008, 05:41 PM
  2. Question about forking with sockets listening
    By Phoenix_Rebirth in forum C Programming
    Replies: 2
    Last Post: 11-10-2008, 04:05 AM
  3. TCP Sockets: multiple clients - one server
    By Printisor in forum C Programming
    Replies: 4
    Last Post: 11-01-2007, 11:34 AM
  4. Unix sockets
    By karas in forum Linux Programming
    Replies: 8
    Last Post: 10-13-2007, 01:20 AM
  5. fork() and sockets
    By biosx in forum Linux Programming
    Replies: 2
    Last Post: 02-12-2002, 09:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21