http://www.perlmonks.org?node_id=276411

djbiv has asked for the wisdom of the Perl Monks concerning the following question:

I have a server application written to accept data on a socket and spawn a child process using fork(); This applications is meant to accept online traffice to write data to a file for other applications use. I am wondering if there are any known limitations or issues with this type of processing? for example if the 'client' application is sending x amount of data to my listening port are there limitations to the number of child processess that can be kicked off? any comments or insight would be very helpfull!
This is the snippet of code that is handling the incoming connections and parent / child processing.
###################################################################### +########################## # M A I N ###################################################################### +########################## # Wait for a connect from the remote client my $acceptSock; my ($pid, $buf, $bufhdr, $req); while (1) { printf ("%sListening on port $args{p}\n", prHdr()); if (!($acceptSock = $listenSock->accept())) { print "bkpt 1: Accept failed: $!\n" if ($args{d} > 2); next; } # Spawn off a child to handle this connection my $pid = fork(); die "Cannot fork: $!" unless defined($pid); # PID is zero when we're the child if($pid) { print "Parent continues\n" if ($args{d} > 2); next; } printf ("%sAccepted connection from: %s\n", prHdr(), $acceptSock->p +eerhost()); srand(time); # Wait indefinitely for a message while (1) { open (FH, ">>$file"); print "Error reading 2-byte header: $!\n", last if (sysread($acceptSock, $bufhdr, 2) != 2); printf ("%sReceiving request\n", prHdr()); # We got the 2-byte (data length) header my $length = unpack("n", $bufhdr); print "$$: Length from request header = $length\n" if($args{d} > 2); print "Error length ($length) is < e-Header size ($EHDR_SIZE)\n", +last if ($length < $EHDR_SIZE); print "Error reading $length bytes of data: $!\n", last if (sysread($acceptSock, $buf, $length-2) != $length-2); my $msg = join('', $bufhdr, $buf); if($args{d} > 1) { # Got the data, display it &hexDump($bufhdr . $buf); # Got the data, display it &hexDump($bufhdr . $buf); print "\n"; } # Parse the message, creating hashes for the e-Header and ISO mess +age my ($hdr, $req) = &parse8583($bufhdr . $buf); # Dump the e-Header in readable form &printEHdr($hdr,1); if($args{d} && length($req)) { # Dump the individual ISO fields in the request print "ISO Request fields:\n"; for (sort numerically keys %$req) { if ($_ eq "48") { print " $_ =>\n"; &printDE48($req->{$_}); } elsif($_ eq "52") { my ($p1) = unpack("H16", $req->{52}); printf (" $_ => 0x%s\n",$p1); } # ADD processing of DE 127 if ($_ eq "127") { print " $_ =>\n"; &parse127($req->{$_}); } else { print " $_ => '", $req->{$_}, "'\n"; } } print "\n"; } printf("%sSending response\n", prHdr()); syswrite($acceptSock, $msg, length($msg)); close (FH); } printf ("%sChild done, closing socket\n", prHdr()); # Remote connection closed or error occured shutdown($acceptSock, 2); close($acceptSock); exit(0); } # Shouldn't be able to break out of the while(1) close($listenSock); die "Unexpected main loop termination";

Replies are listed 'Best First'.
Re: fork processing limitations
by blue_cowdawg (Monsignor) on Jul 21, 2003 at 19:26 UTC

    The number of processes you can fork off (and I am going to assume Un*x here) depends on some kernel tuning parameters. One of which is the total number of processes system wide that may be created as well as a second that determines how many each user may create. The total max processes system wide is a product of a calculation that takes into consideration the maximum users the system is configured to handle, the max processes per user allowed and is constrained by the physical memory of the system.

    All of this differs between flavors of Un*x as well as the whims of the Unix System Administrator administering the system.


    Peter L. BergholdBrewer of Belgian Ales
    Peter@Berghold.Netwww.berghold.net
    Unix Professional
      Thanks for your insight on my question, the actual implementation will be running on Linux(kernel 2.4.2.x). I would assume that your answer covers this as well.
      Noting your comment then, My application should be able to (theoretically handle), accept thousounds of connections per hour while processing the data and writing the data to file(s)... where might I find the processing limitations? writing my data to file(s)? processing the data within my code?

          My application should be able to (theoretically handle), accept thousounds of connections per hour while processing the data and writing the data to file(s)

        Theoretically. YMMV depending on hardware and physical memory.

          where might I find the processing limitations?

        You've caught me slightly off guard here. I don't have my Linux internals book handy. Just for grins I tried poking around with adb and had no success finding the right symbol to examine.

        Maybe another PM has the info at their fingertips? I'm curious now...


        Peter L. BergholdBrewer of Belgian Ales
        Peter@Berghold.Netwww.berghold.net
        Unix Professional