Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is socket connection being handled in a forked process

I have a program that serves as a TCP server that listens to port 5000, for example.

Now I want to run another command-line process to run in parallel with this server. I know the right way to do it is to fork the server, than call "exec" ....

My question is, is the child process also "owns" this port 5000? If I want to release this port 5000, do I need to kill both the parent process and the child process?

I am just very curious how this socket connection is being handled in the forking.

like image 714
CuriousMind Avatar asked May 16 '11 06:05

CuriousMind


2 Answers

  1. First, accept() the incoming connection. The accepting process now has a handle to the listening socket, and the newly accepted socket.
  2. Fork and:
    • In the child:
      1. Close the listening socket.
      2. Do stuff with the accepted socket.
    • In the parent:
      1. Close the accepted socket.
      2. Resume the accept loop.

The various socket resources will be reclaimed when all references to the handle are closed. If a process terminates, all its handles are closed implicitly. Therefore if a child closes the handle it inherits to the listening socket, the only handle remaining to that socket exists in the parent. Then the listening socket will be reclaimed when the parent terminates, or closes this handle explicitly.

like image 143
Matt Joiner Avatar answered Oct 09 '22 11:10

Matt Joiner


Yes, it does
Whenever a child process is created, it gets a copy of the file descriptor table from the parent process. And there is a reference count corresponding to each file descriptor, that is the number of processes currently accessing the file/socket. So, if a socket is open in master process and a child process is created, the reference count increments, as it is now open in child process as well, and when it is closed in any of the processes, it decrements. A socket is finally closed when the reference count reaches zero.

like image 39
mandavi Avatar answered Oct 09 '22 13:10

mandavi