Well, in short, what's causing it is that *nix won't clean up a process until its parent has collected its status. That is, the process manager is holding onto such information as the exit status of the process, so that the parent can call waitpid on it.

Anyway, it's really easy to deal with this in perl... you just add this line to the top of your script:

$SIG{CHLD} = 'IGNORE';

Now, as for writing your own web server... to do this as a learning exercise is cool and all, but I think you'd really be much better off using an already existing one (such as apache) for real use. It's easier to work around the problems of not being able to run the web server as root (which you really shouldn't be doing! there's a good reason apache wants you to setuid to an unpriveleged user!) than to work out all of the stuff that apache can do for you.

That all being said, though, here's a simple web server that I once wrote (as a learning exercise)... just to give you some more example code. I'm not claiming it to be super good or secure or anything... but it is a good example of forking in practice. I think it borrows pretty heavily from an example in the Perl Cookbook (which you might want to get a copy of).

use IO::Socket; use Sys::Hostname; use strict; my $docroot = shift || die "Usage: $0 (document root path) [server por +t (default 80)]\n"; my $port = shift || 80; my $serverhandle = IO::Socket::INET->new( Proto => 'tcp', Listen => SOMAXCONN, LocalAddr => Sys::Hostname::hostname(), LocalPort => $port) or die "Could not listen on port $port... $!\n +"; my $CRLF = "\015\012"; my $error404 = "<html><head><title>404 Not Found</title><body><h1>Erro +r 404 File Not Found</h1> The requested URL could not be located on this server. +</body></html>"; my $error501 = "<html><head><title>501 Not Implemented</title><body><h +1>Error 501 Method Not Implemented</h1> The http method in your request is not implemented by +this server.</body></html>"; my $otherheaders = "Server: Steve's 60-line web server${CRLF}Connectio +n: close${CRLF}"; my $htmlheader = "Content-Type: text/html$CRLF"; my $textheader = "Content-Type: text/plain$CRLF"; $/ = "$CRLF$CRLF"; $SIG{CHLD} = 'IGNORE'; while (my $clienthandle = $serverhandle->accept) { my $inpid = fork; if (!defined $inpid) { die "Could not fork: $!"; } elsif ($inpid) { $clienthandle->close; next; } my $name = $clienthandle->peerhost; my $request = <$clienthandle>; my ($method,$url,$httpversion,$headers) = ($request =~ /(\S+)\s+(\ +S+)\s+HTTP\/(\S+)$CRLF(.*)/s); print localtime()." - $name - \"$method $url HTTP/$httpversion\" - +"; if (uc $method eq 'GET') { my $nofile = 0; open GETFILE,$docroot.$url or $nofile = 1; if ($nofile) { print $clienthandle "HTTP/1.1 404 Not Found$CRLF$otherhead +ers$htmlheader" ."Content-Length: ".length($error404).$CRLF.$CRLF.$err +or404; print " 404\n"; } else { my $buffer = ''; while (<GETFILE>) { $buffer .= $_; } close GETFILE; my $contenttype = ($url =~ /\.html?$/) ? $htmlheader : $te +xtheader; print $clienthandle "HTTP/1.1 200 OK$CRLF$otherheaders" ."Content-Length: ".length($buffer).$CRLF.$CRLF.$buffe +r; print " 200\n"; } } else { print $clienthandle "HTTP/1.1 501 Not Implemented$CRLF$otherhe +aders$htmlheader" ."Content-Length: ".length($error501).$CRLF.$CRLF.$error50 +1; print " 501\n"; } close $serverhandle; close $clienthandle; exit; } close $serverhandle;

Enjoy!

------------ :Wq Not an editor command: Wq

In reply to Re^3: Forking by etcshadow
in thread Forking by andy7t

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.