in reply to Forking and loops: A plea for understanding.
It creates a copy of the script and executes the copy.
That may be what's bothering you. The trick is that both the parent and child go on from the same point, that where fork is called. The child gets a private copy of all the parent's environment, stack, memory and everything. It is said that fork returns twice, once in the parent and once in the child. The return value of fork is the only difference between the two, besides the dynamical process table variables like $$.
Following the parent only, your server loop looks like
A child ignores the next line and goes on to do the client handling and exit. That ends the program as far as the child is concerned.while(my $client = $server->accept()) { next; }
Two points which you may not need to be reminded of. The fork call can fail, returning undef, leaving you in the parent but executing child code. When you hit exit then, your server quits.
Second, you may be spinning out lots of zombies. In a persistent server, it is probably easiest to set $SIG{CHLD}='IGNORE'; if your OS supports that.
Parallel::ForkManager is a good wrapper for this sort of thing.
After Compline,
Zaxo
|
|---|