Additional to dynamic pool of process C
Changed code:
int main(int argc, char *argv[])
{
...//settings
listensock = socket(AF_INET, SOCK_STREAM,IPPROTO_TCP);
result = bind(listensock, (struct sockaddr *) &sAddr, sizeof(sAddr));
result = listen(listensock, 1);
...//skip errors checking
while(1){
newsock = accept(listensock, NULL,NULL);
pid=fork();
if (pid == 0) {
send(newsock, buffer, nread, 0);
}
close(newsock);
}
wait(NULL);
}
This creates childs only if someone try to connect. It is not pre-fork model. N processes should be run after server starts and wait for connects. If I try to invoke fork()
3 times in loop, it immediately terminate. How to start, wait until someone connect and then send data (like in my code).