The following snippet of code creates 4 processes, all sharing the same listening socket.
Is there any danger in doing this? Should I always have one listening process and fork after connections are accepted, in the conventional manner?
for (p = 0; p < 3; p++) {
pid = fork();
if (pid == 0) break;
}
while (1) {
unsigned int clientlen = sizeof(echoclient);
/* Wait for client connection */
if ((clientsock =
accept(serversock, (struct sockaddr *) &echoclient,
&clientlen)) < 0) {
die("Failed to accept client connection");
}
fprintf(stdout, "Process No. %d - Client connected: %s\n",
p,
inet_ntoa(echoclient.sin_addr));
handle_client(clientsock);
}
(I understand that forking after accepting allows a programme to make a process per connection. I'm playing around with proto-threads and various async stuff, so I'm just looking at having one process per core.)