Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl daemon with Threads

Status
Not open for further replies.

rottmanja

Programmer
Aug 5, 2008
17
US
I have since updated my threading code since my last post. Currently everything works great when I test my code with out trying initialize the code as a daemon.

The issue that I run into when I try to initialize the code with a perl daemon is. The first iteration of the code works perfectly fine. However when the next iteration starts, it fails/stops right after the first thread is created.

Here is the code I am currently testing with. Any help with this is greatly appreciated.

plsMain.pl:
Code:
use Lib::PLSInstance;
use Lib::PLSParse qw(parseMain);
use Lib::Daemon;
use Data::Dumper;
use strict;
use threads;


Lib::Daemon::daemonize();

	my @thr = ();

	while(1){
		main();
		sleep(10);
	}




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],$instData[$i][1] );
	}


	# start rejoining the threads
	for (my $i = 0; $i < @instData; $i++) {
	   print "Joining thread $i\n";
	   $thr[$i]->join();
	   print "\tThread $i joined back with the parent\n";
	}
	
}

Daemon.pm:
Code:
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;
	    setsid() or die "Can't start a new session: $!";
	}

1;

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top