Greetings Monks....

First time caller, long time lurker.

Here is my dilema.... I am starting with a simple client/server app. I have a forking server. It first creates itself as a daemon and waits for incoming socket connections. when it sees one, it forks a copy of itself, sends data to the client and exits. The problem is when the client dies, so does the parent. I would very much appricate some pointers. I am listing the server/client code below....

Server code:

#!/usr/local/bin/perl use strict; use IO::Socket qw(:DEFAULT :crlf); use IO::File; use POSIX 'WNOHANG'; use POSIX 'setsid'; use POSIX 'sys_wait_h'; my $port = 1426; my $socket = IO::Socket::INET->new( Proto => 'tcp', LocalPort => $port, Listen => 5, Reuse => SO_REUSEADDR, Type => SOCK_STREAM ) or die "Can't create listen socket: $!"; my $pid = become_daemon(); my $children = 0; $SIG{CHLD} = \&reaper1; while (my $conn = $socket->accept) { print "Received a request from a client....\n"; defined (my $child = fork()) or die "Can't fork: $!"; $children++; print "POST FORK: me = $$ child= $child\n"; if ($child == 0) { #my $x = become_daemon(); #print "my child is $x\n"; print "I am the child $$\n"; do_child($conn); exit(0); } else { print "I am the parent $$... waiting for next request\n"; } } sub become_daemon { print "Forking Daemon\n"; print "My pid is $$\n"; die "Can't fork" unless defined (my $child = fork); print "Forked... my pid is $$\n"; print "child pid is $child\n"; exit 0 if $child; # setsid(); open(STDIN, "</dev/null"); open(STDOUT, ">/dev/null"); open(STDERR, ">&STDOUT"); chdir("/"); umask(0); return $$; } sub reaper1 { #my $sig=shift; my $kid = waitpid(-1,&WNOHANG); while (my $kid > 0) { warn "REAPER: Reaped child with PID $kid\n"; } $SIG{CHLD} = \&reaper1; } sub do_child { my $connection = shift; my $resp = <$connection>; print "FromClient: $resp"; my $parent = getppid(); for (1.. 10) { print $connection "pid $$ parent=$parent Sending number $_\n"; sleep 1 } print $connection "All done\n"; print "All done\n"; #$connection->close; return; }

client code:

#!/usr/local/bin/perl use IO::Socket; my $port = 1426; my $host = myhost; our $socket = IO::Socket::INET->new( Proto => 'tcp', PeerAddr => "$host", PeerPort => "$port" ) or die "Can't create listen socket: $!"; $socket->send("begin\n"); while(<$socket>) { chomp; print $_ . "\n"; } close $socket;

READMORE tags added by Arunbear


In reply to Fork parent process dies unexpectedly by iang

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.