josef has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl -w # Description: preforker proxy use 5.16.0; use HTTP::Daemon; use LWP::UserAgent; use Fcntl ':flock'; # global variables my $ip = '127.0.0.1'; my $port = '3128'; my $MIN_CHILDREN = 5; # number of children to maintain my $MAX_REQ_PER_CHILD = 30; # number of req processed per child my %children = (); # keys are current child process IDs my $children = 0; # current number of children $| = 1; my $agent = LWP::UserAgent->new; $agent->agent("perl proxy"); # establish SERVER socket, bind and listen. my $master = HTTP::Daemon->new( LocalPort => $port, LocalAddr => $ip ) or die "Cannot create master socket: $!\n"; # create initial children for (1..$MIN_CHILDREN) { &newChild($master) } # keep children - a never ending loop for the parent process which # just monitors dying children and generates new ones while (1) { sleep; for (my $i = $children; $i < $MIN_CHILDREN; $i++ ) { &newChild($master) } } exit (0); ################################################################### ### Subs ################################################################### # newChild - a forked child process that actually does some work sub newChild { my $socket = shift; my $pid; defined ($pid = fork) or die "Cannot fork child: $@\n"; if ($pid) { # parent code $children{$pid} = 1; # child is using this pid $children++; # Increase the child counter print "forked new child with PID $pid, we have now $children c +hildren(s)\n"; } &child_execute($socket) unless $pid; } sub child_execute { my $master = shift; my $i = 0; while ($i < $MAX_REQ_PER_CHILD) { # Loop for $childLifetime reques +ts $i++; flock($master, 2); # LOCK_EX my $slave = $master->accept or die "accept: $!"; flock($master, 8); # LOCK_UN $slave->autoflush(1); my $request = $slave->get_request; my $url = $request->url; # which PID execute this request??? print "child accept request URI: $url\n"; my $response = $agent->simple_request($request); if ($response->is_success) { my $content = $response->content} $slave->send_response($response); close $slave; } exit (0); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to find the PID for executed URL request?
by locked_user sundialsvc4 (Abbot) on Dec 10, 2013 at 15:46 UTC | |
by josef (Acolyte) on Dec 10, 2013 at 16:22 UTC | |
|
Re: How to find the PID for executed URL request?
by oiskuu (Hermit) on Dec 10, 2013 at 20:20 UTC | |
by josef (Acolyte) on Dec 11, 2013 at 06:30 UTC |