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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

fork problem in perl

Status
Not open for further replies.

mrahman2008

Programmer
Feb 7, 2009
1
BD
Hello everyone,
I have a perl program that does the following thing:
1. Continuously looking in a mysql database table for a row that has status '2'.
2. If a status 2 row is found it first checks whether that particular row has been previously visited or not.
3. If not previously visited it will create a child process.
4. The parent again enters the loop and look for any new status '2'.
5. In the mean time the child process waits for 10 sec and updates the row status to 7 if the status of that row is still remaining as '2'.

This program works fine for sometime but after some time it simply crashes. Can you please help me out!!!! Here is my code:

#!/usr/bin/perl
use DBI;
use Time::Local;
use POSIX ":sys_wait_h";

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

#$num = "10";
my @childs;
#my $m = "mishu";

#pipe (B_IN, B_OUT);
#my $pid2 = fork();


###################### DATABASE CONNECTION #########################
my $dbName = "abc";
my $userName = "root";
my $password = "";
my $dbpath = "dbi:mysql:database=$dbName;host=localhost";
my $dbh = DBI->connect($dbpath, $userName, $password) or die "Can't open database: $DBI::errstr";
#####################################################################

my %alreadyVisited;
my $messageID;

while(1)
{
my $query = "SELECT * FROM `transaction_log` WHERE status = '2'";
my $qh = $dbh->prepare($query);
$qh->execute();

while(my $row = $qh->fetchrow_hashref())
{
$messageID = $row->{message_id};

if(exists $alreadyVisited{$messageID})
{
#print $messageID." already exists in HASH \n";
next;
}
else
{
#print $messageID." is a new one \n";
}

my $pid = fork();

if($pid)
{
#parent
#print "Child created with PID : ".$pid."\n";
push(@childs, $pid);
}
elsif($pid == 0)
{
#child
print $messageID." is in child\n";
sleep(10);
updateStatus($messageID);
#print "%%%%%%%%%%%%%%%%%%%%%%%%%% \n";
exit(0);
}
else
{
print "Could not fork \n";
}
$alreadyVisited{$messageID} = $row->{name};
}
sleep(1);

foreach (@childs)
{
waitpid($_, WNOHANG);
}
}

print "LAST MSG ID is : ".$messageID."\n";

if($dbh->disconnect())
{
print "hello \n";
}
print "going to terminate child one \n";
#exit(0);

sub updateStatus
{
###################### DATABASE CONNECTION #########################
my $dbName = "abc";
my $userName = "root";
my $password = "";
my $dbpath = "dbi:mysql:database=$dbName;host=localhost";
my $dbh2 = DBI->connect($dbpath, $userName, $password) or die "Can't open database: $DBI::errstr";
#####################################################################

my $ID = $_[0];

print $ID."****\n";
my $query2 = "UPDATE `transaction_log` SET status = '7' WHERE status = '2' AND message_id = '".$ID."'";
my $qh2 = $dbh2->prepare($query2);
if($qh2->execute() eq 1)
{
print $ID." has been updated to 7\n";
}
else
{
print "Failed to update ID is : ".$ID."\n";
}


$dbh2->disconnect();
}

Actually there is no error message. you can see that i am running an infinite while loop. So it is not supposed to stop. But it stops after some time. Before stopping a windows prompt appears saying "perl.exe Application error. The instruction at '0x.........' referenced memory at '0x........' The memory could not be read."

There is also another error saying "DESTROY ignored because DBI sth handle (DBD::mysql::st) is owned by thread 2
33fac not current thread 45ad004" But this error msg does not come all the time

I am definitely doing something wrong in my code.


Please help me out guys
 
+1 for Parallel Forkmanager..

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top