On every new client connection forked server process
different processes(other children of server i.e. exec) couldn\'t identify that client with same fd in used in
The file descriptor number is unique only within the process it exists in, and as soon as it's closed, it can be reused (for example the next time you call accept
). It is not a good choice of "connection identifier".
In your case where you have a new process for each connection, the most natural identifier for a connection would be the process id - but you've thrown it away by not saving the return value of fork
. In the parent process, fork
returns the pid of the child process it created. You want to save this and use it later. In particular you can use it to kill the child process or identify when the child process exits (wait
-family functions will tell you which child exited).
Of course if your whole model is to use a separate process for each connection, I'm not sure why you need to identify connections at all in the parent process. If each connection is not a completely independent process, you might do a lot better using threads instead of processes.