Hi monks,

in a very small prefork proxy server script, see below, I want to found which forked child (PID) process the current URI request.
In my tests I get only PID 0 (Ex: child 0 accept request URI: http://web.com/)

Best regards
Josef
#!/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); }

In reply to How to find the PID for executed URL request? by josef

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.