in reply to Re: Re: Threads Problem!
in thread Threads Problem!

I've tried forks and it works but it doesnt exit the sub the same way as threads do.

Could you elaborate on this? It should behave the same as threads. And I have not been able to reproduce it with a simple case. Are you sure they are zombies? Could it not be that something is keeping the forked processes alive in e.g. a DESTROY subroutine?

Liz

Replies are listed 'Best First'.
Re: Re: Re: Re: Threads Problem!
by Phantazm (Initiate) on Nov 03, 2003 at 15:02 UTC
    Sorry but i it's real hard to explain.

    Now i'm trying forks. It's actually working real good the only problem is that it doesnt exits the sub routines.

    Say my script is test.pl

    When someone does !help on irc in channel it starts my help sub

    then a ps -x shows another test.pl that confirms that the sub is running. When the sub is done it doesnt kill the newly created test.pl

    So after 300 ppl have done !help there are 300 test.pl running :)

    If i end the sub with exit; the entire script dies.

    Using threads with same script it starts a thread (a new test.pl shows in ps -x) after its done the extra test.pl dissapears just as i want it, but with this code i got the severe memory leak

    So i'm pretty close now i think :) just need it to exit the sub correctly so it closes.

    I start the sub with this syntax.

    $helpthr = threads->new(\&help, "$ircnick"); $helpthr->detach;
    I have tried to end the help sub with exit; and return; all it does is kill the entire bot,
    It only works if i restart the irc loop when the sub ends with
    $irc->start();


    Thank you yet again for taking time to help me.
      If you think exiting is the problem, you might want to have a look at Thread::Exit. However, I doubt whether that will make any diifference.

      Please try to re-create the problem with as little source as possible. I cannot reproduce the behaviour on my systems, so it must be something specific to your situation that is causing the zombies.

      What OS are you using anyway? Maybe the problem is OS related.

      Liz

        Ok i will try to write a little source that have the same problem as the one i use now.

        i'm running Linux merlin 2.4.20-gentoo-r7 #5 SMP Wed Oct 22 01:11:47 CEST 2003 i686 Pentium III (Coppermine) GenuineIntel GNU/Linux

        I try thread exit and then write a little sample for you :)
        Thanx for effort liz :)

        This is a sample of code that exists. i've left all the use module::module; for you to see what i use in the entire bot.

        This bot just sends TEST TEST to a user who does .help

        With threaded perl it works good. starts a process then it exits it when done but loads alot of memory. With forks it works but it doesnt end the process, the extra bot.pl is still there when ps -aux | grep bot.pl

        bot.pl
        #!/usr/bin/perl -w use Net::IRC; use strict; use HTTP::Response; use HTML::LinkExtor; use URI::URL; use DBI; use Data::Dumper; use File::Basename; use File::Path; use vars qw($search $text $ftp); use Net::FTP; use Config::IniFiles; use Number::Format; use LWP::UserAgent; use HTTP::Request; #use Thread; use FileHandle; use Time::HiRes qw(usleep ualarm gettimeofday tv_interval); use Date::Calc qw(:all); use Net::Cmd; use forks; my $ircserver = '127.0.0.1'; + my $ircport = '7777'; + our $ircchannel = '#test'; + my $ircbotname = 'labrat'; + my $joingreet = 'hello fellas'; + my $nickgreet = 'welcome '; my $ircname = 'labrat'; + my $username = 'labrat'; + my $localip = '192.168.0.1'; + my $pid = $$; our $bold = chr(2); print "Preparing to load required files!!\n"; ### MODULES ### require "help_module.pl"; print "Required Files loaded OK!!\n"; sub ircinfo { our $irc = new Net::IRC; our $conn = $irc->newconn( Server => $ircserver, Port => $ircport, Nick => $ircbotname, Ircname => $ircname, LocalAddr => $localip, Password => 'fucknuts.', Username => $username ); $conn->{channel} = $ircchannel; $conn->add_handler('join', \&on_join); $conn->add_handler('part', \&on_part); $conn->add_handler('public', \&on_public); $conn->add_handler('376', \&on_connect); $conn->add_global_handler('disconnect', \&reconnect); $conn->add_global_handler(433, \&on_nick_taken); $irc->start(); } sub reconnect { system("./bot.pl &"); system("kill -9 $pid"); } sub default { my ($conn, $event) = @_; print Dumper($event); } sub on_connect { my $conn = shift; $conn->join($conn->{channel}); $conn->privmsg($conn->{channel}, $joingreet); $conn->{connected} = 1; $conn->add_global_handler(433, \&on_nick_taken); } sub on_nick_taken { my ($self) = shift; $self->nick(substr($self->nick, -1) . substr($self->nick, 0, 8)); } sub on_join { my ($conn, $event) = @_; my $nick = $event->{nick}; my $host = $event->{host}; $conn->privmsg($conn->{channel}, "$nickgreet," . $bold . " $nick" +. $bold ); } sub on_part { my ($conn, $event) = @_; my $nick = $event->{nick}; $conn->privmsg($conn->{channel}, "Goodbye, $nick!"); } sub on_public { my ($conn, $event) = @_; my $text = $event->{args}[0]; if (lc substr($text,0,5) eq ".help") { my $info = substr($text,6); my $helpthr = threads->new(\&help, "$event->{nick}"); $helpthr->detach; } } print "Starting Bot!!\n"; ircinfo();
        This is the help_module.pl
        sub help { my $nick = shift; $conn->privmsg($nick, $bold . "TEST " . $bold . "TEST"); last(); # Here i've tried with last; exit(); return; } 1;
        I'm really stuck on this :/ I dont have a singel clue. Might be my irc module thas poorly written to. Hope you get something out of it :)

        /Chris