Thread: sockets and child processes

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445

    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,981
    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
    3,445
    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, 10:34 AM
  4. Unix sockets
    By karas in forum Linux Programming
    Replies: 8
    Last Post: 10-13-2007, 12:20 AM
  5. fork() and sockets
    By biosx in forum Linux Programming
    Replies: 2
    Last Post: 02-12-2002, 09:00 AM