#!/usr/bin/perl use strict; use warnings; use DBI; use Data::Dumper; if( $#ARGV < 0 ) { die "no sufficient argument.\n"; } my $fork_count = shift; #print STDERR $local_time."\n"; #print STDERR $execute_time."\n"; my $user="zivah"; my $password="zivah"; my $database="zivah"; my $schema="public"; my $server="192.168.1.40"; my $port="5432"; my $logfile="Log.txt"; my $LOG_FILE; open ($LOG_FILE, ">$logfile") or die "Can't open the file, $!!\n"; $SIG{"ALRM"} = \&handler; sub handler() { my $handle=$_[0]; my $time=localtime; if($handle eq "ALRM") { print $LOG_FILE "SIGNAL SIG$handle is generated on $time -- $$\n"; &Fork_Execute(); } } my $Database_handler; my $sth; sub Database_Connect { # connect with database. #print "Connecting Database\n"; unless( $Database_handler = DBI->connect("dbi:Pg:database=".$database.";host=$server;port=$port", $user, $password, { RaiseError => 0, AutoCommit => 1, PrintError => 1, InactiveDestroy => 1, pg_server_prepare => 0}) ) { # Print an error message for unsuccessfull connection, print "EMERG: connect_database : connecting database failed, $DBI::errstr" ; # Return -1. return -1 ; } # Return the database handler. return $Database_handler; } sub Fork_Prepare { my ($result, $query, $return_value, $delete_time, @tables); # Prepare sql queries for finding unwanted tables. #$query="INSERT INTO $schema.a(name) values('santhosh');"; my $id=shift; $query="Update $schema.a SET name='single',mtime=now() where no=$id;"; unless($sth = $Database_handler->prepare($query)){ # print an error message. print "ERROR:can't prepare SQL statement:[ $query ]:[$DBI::errstr]"; # return -1. return -1; } } sub Fork_Execute { my ($result); unless($result = $sth->execute()){ # Print an error message print "ERROR:can't execute SQL statement::[$DBI::errstr]"; return -1; } } &Database_Connect(); my $fc=1; my $cnt=1; my @pids; system("date"); while( $fork_count >= $fc ) { my $pid = fork(); if (not defined $pid) { print "resources not avilable.\n"; } elsif ($pid == 0) { &Fork_Prepare($cnt); #&Database_Connect(); sleep(1000); exit; } else { $fc++; $cnt++; next; } } system("date");