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

hi monks can any one tell me weather the child process will be killed after executing client process completely.
#!/usr/bin/perl use Socket; use Carp; use IO::Handle; my $port = 4573; $|=1; # Setup some variables my %AGI; my $tests = 0; my $fail = 0; my $pass = 0; my $capture = 0; sub checkresult { my ($res) = @_; my $retval; $tests++; chomp $res; if ($res =~ /^200/) { $res =~ /result=(-?\d+)/; if (!length($1)) { print STDERR "FAIL ($res)\n"; $fail++; } else { print STDERR "PASS ($1)\n"; $capture = $1; print STDERR "capture is $capture"; $pass++; } } else { print STDERR "FAIL (unexpected result '$res')\n"; $fail++; } } socket(SERVER, PF_INET, SOCK_STREAM, 0); setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, pack("l", 1)); bind(SERVER, sockaddr_in($port, INADDR_ANY)) || die("can't bind\n"); listen(SERVER, SOMAXCONN); for(;;) { my $raddr = accept(CLIENT, SERVER); my ($s, $p) = sockaddr_in($raddr); $child = fork(); if ( ! $child ) { CLIENT->autoflush(1); while(<CLIENT>) { chomp; last unless length($_); if (/^agi_(\w+)\:\s+(.*)$/) { $AGI{$1} = $2; } } print STDERR "AGI Environment Dump from $s:$p --\n"; foreach my $i (sort keys %AGI) { print STDERR " -- $i = $AGI{$i}\n"; } print CLIENT "GET DATA /demo-congrats \"\"\n"; my $result = <CLIENT>; print STDERR "result is $result"; &checkresult($result); if($capture==1){ print STDERR "Going to ask to play tt-monkeys"; print CLIENT "GET DATA tt-monkeys \"\"\n"; my $result = <CLIENT>; &checkresult($result); } elsif($capture==2){ print STDERR "Going to ask to play demo-congrats"; print CLIENT "GET DATA demo-instruct \"\"\n"; my $result = <CLIENT>; &checkresult($result); } else{ print STDERR "In else condition"; } print STDERR "================== Complete ==================== +==\n"; print STDERR "$tests tests completed, $pass passed, $fail fail +ed\n"; print STDERR "================================================ +==\n"; close(CLIENT); exit(); #kill (9, $child); } close(SERVER); }
Actually i have created a socket program with one socket and more than one client by using fork function and i would like to know weather the child process will be terminated after executing it completely. So, can any one give me suggestions on this code. Thanks in advance.

Replies are listed 'Best First'.
Re: Reg socket
by targetsmart (Curate) on Mar 10, 2009 at 05:47 UTC
    Give us more clarity on your problem.
    Take a look at IO::Socket::INET and IO::Select
    This example also may help you Multiprocessing with IO::Select
    UPDATE
    ideally the server has to listen and accept connections, the client has to connect to server, but your two programs doesn't look like client & server.

    Vivek
    -- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.
Re: Reg socket
by ikegami (Patriarch) on Mar 10, 2009 at 05:57 UTC

    For starters, you never create a socket in the child?! use strict;!!

    By the way,

    $SIG{CHLD} = sub {wait ()};

    is better written as

    $SIG{CHLD} = 'IGNORE';
Re: Reg socket
by ikegami (Patriarch) on Mar 10, 2009 at 05:13 UTC

    [ At the time of my reply, the entire parent post was "hi monks i would like to know one thing is it possible to create more than one socket by using perl programming. If any one have some idea on it please give me suggestions about it." ]

    Yes. Take the code for making one socket, and execute it twice. Given the vagueness of your question and the total lack of description of your problem, there's really nothing else I can say.

Re: Reg socket
by irah (Pilgrim) on Mar 10, 2009 at 05:50 UTC

    When you create or use a same socket more than once, use setsockopt function. Please note that, the socket address, port number will not be repeat. Further more reading see, perldoc -f setsockopt

Re: Reg socket
by leslie (Pilgrim) on Mar 10, 2009 at 09:09 UTC

    You can create more than one socket by executing your socket program again and again. Before doing this you have to make sure that your port number and address should be unique.

    If you want to reuse the address you can use setsockopt. Instead of using fork() you can use select(), it is better way compare to fork().

Re: Reg socket
by Beechbone (Friar) on Mar 10, 2009 at 10:02 UTC
    Go a CPAN mirror and fetch one of the countless modules that implement a ((pre-)forking) server for you. This is just too easy to do wrong without much deeper knowledge of Sockets, Perl and Forking...

    Search, Ask, Know