In my current application, I have a need to use threading to handle multiple instances of data that needs to be parsed in relative real time. Right now, I have everything working correctly to a point. Now with in the application I also need to run the application as a daemon.

The issue that I am seeing is that when the application iterates through the first time everything works fine. But as the second iteration starts the daemon dies. I have a small idea why it is happening, but not sure if it is correct.

So I come to you wise monks, humble as can be. Code below

Daemon.pm
package Lib::Daemon; use base qw(Exporter); our @EXPORT = qw(); use POSIX qw(setsid); use strict; sub daemonize { defined(my $pid = fork) or die "Can't fork: $!"; exit if $pid; print $pid . "PID WAS \n\n"; setsid() or die "Can't start a new session: $!"; } 1; __END__


Main.pl
Lib::Daemon::daemonize(); print "Starting Daemon.... \n\n"; my @thr = (); while(1){ main(); sleep(60); } sub threadMain{ print "Child Thread $_[0] \n"; print "*********************************************************** +*********************\n"; parseMain($_[0],$_[1]); return 1; } sub main{ my @instData = Lib::PLSInstance::getInstances(); for(my $i = 0; $i < @instData; $i++){ $thr[$i] = threads->new(\&threadMain, $instData[$i][2],$instDa +ta[$i][1] ) or die "Thread Died: $!"; } # start rejoining the threads for (my $i = 0; $i < @instData; $i++) { print "Joining thread $i\n"; $thr[$i]->join() or die "Thread die on join: $!"; print "\tThread $i joined back with the parent\n"; } }

In reply to Threading + Daemons troubles by rottmanja

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.