In linux, I have written a standard tcp server(using internet sockets) with a small difference. This is the skeleton of the server
fd=socket(...);
bind(...);
listen(...);
//now do a fork
fork();
//this will create two processes bound to the same server listening on the same port !!
clientfd=accept(...);
What will happen when a client connects to the server on the listening port. Which process will accept the connection ?
From the practical runs of the program, always the parent(the process which forked) was getting the client request. I want to know the theory behind this. Was it just accidental that the parent process was getting the request ?
Now I killed the parent process. So there is only child process running. When a client tried to connect to the server on the same port number, the child(or the lone survivor) process got the connection. How is this behavior explained ?