How is socket connection being handled in a forked process

后端 未结 2 687
闹比i
闹比i 2020-12-23 12:01

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 ser

2条回答
  •  囚心锁ツ
    2020-12-23 12:06

    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.

提交回复
热议问题