I try to write a program that monitors several oracle instances and when the database creates an archived redo logfile then the program should fork a child and backup the archived log to tape.

Everthing works fine till I opened a DBI db-connection. If I do that the the parent doesn't receive a signal that the child died, nor it's return value. I don't want to you the db-connection in the child I only want to keep a persistent connection in the parent.

Does any one see what I am doing wrong or if there is a work around?

Tanks in advance,

Kris.
kris.lemaire@pandora.be

This code works fine (no db-connection)
# hash with the status of the childern
# at startup -> started 
# when child dies -> return code
my %h;

$| = 1;
# Trap sig send from child to parent
# when child dies -> then change the status to return value
$SIG{CHLD}= sub {$pid = wait();$h{$pid} = $?;};

# start forking 4 childern
for (my $i=1; $i < 5; $i++) {
  print "I'll fork a new child\n";
  if( !defined($child_pid = fork() ) ) {
    die "Can't fork : $!";
  } elsif ($child_pid) {
    # child started -> set status
    $h{$child_pid} = 'started';
  } else {
    child_action(3*$i, $i%2);
  }
}

# the parent sleeps 1 sec and then print the 
# status for all childeren
while (1) {
  foreach $key (sort keys %h) {
    print "$key->$h{$key} ";
  }
  print "\n";
  sleep 2;
}

# The child just sleep a few sec 
# prints to stdout when it stops
sub child_action {
  my ($sleeptime,$retcode) = @_;
  sleep 1; # always start sleeping 1 sec to allow initialisation
	print "I'm the child $$ and will sleep for $sleeptime s and return $retcode\n";
  sleep($sleeptime);
	print "I'm the child $$ : $retcode, I'm awake\n";
  exit $retcode;
}

# output :
#
#$ perl -w test5.pl
#I'll fork a new child
#I'll fork a new child
#I'll fork a new child
#I'll fork a new child
#15259->started 15260->started 15261->started 15262->started 
#I'm the child 15260 and will sleep for 6 s and return 0
#I'm the child 15259 and will sleep for 3 s and return 1
#I'm the child 15261 and will sleep for 9 s and return 1
#I'm the child 15262 and will sleep for 12 s and return 0
#15259->started 15260->started 15261->started 15262->started 
#I'm the child 15259 : 1, I'm awake
#15259->256 15260->started 15261->started 15262->started 
#15259->256 15260->started 15261->started 15262->started 
#I'm the child 15260 : 0, I'm awake
#15259->256 15260->0 15261->started 15262->started 
#15259->256 15260->0 15261->started 15262->started 
#I'm the child 15261 : 1, I'm awake
#15259->256 15260->0 15261->256 15262->started 
#15259->256 15260->0 15261->256 15262->started 
#I'm the child 15262 : 0, I'm awake
#15259->256 15260->0 15261->256 15262->0 
#15259->256 15260->0 15261->256 15262->0 
#15259->256 15260->0 15261->256 15262->0 
#15259->256 15260->0 15261->256 15262->0 
#^C
This code doesn't work (with db-connection)
use DBI;
# hash with the status of the childern
# at startup -> started 
# when child dies -> return code
my %h;

$| = 1;
# Trap sig send from child to parent
# when child dies -> then change the status to return value
$SIG{CHLD}= sub {$pid = wait();$h{$pid} = $?;};

# Open connection : this is when it goes wrong
my $dbh = DBI->connect("dbi:Oracle:", "/", "",
                       {PrintError=>1, RaiseError=>0, ora_session_mode=>2});
# Try without this line, with this line at 1 and at 0, but it doesn't work;
#$dbh->{InactiveDestroy} = 0;

# start forking 4 childern
for (my $i=1; $i < 5; $i++) {
  print "I'll fork a new child\n";
  if( !defined($child_pid = fork() ) ) {
    die "Can't fork : $!";
  } elsif ($child_pid) {
    # child started -> set status
    $h{$child_pid} = 'started';
  } else {
    child_action(3*$i, $i%2);
  }
}

# the parent sleeps 1 sec and then print the 
# status for all childeren
while (1) {
  foreach $key (sort keys %h) {
    print "$key->$h{$key} ";
  }
  print "\n";
  sleep 2;
}

# The child just sleep a few sec 
# prints to stdout when it stops
sub child_action {
  my ($sleeptime,$retcode) = @_;
  sleep 1; # always start sleeping 1 sec to allow initialisation
	print "I'm the child $$ and will sleep for $sleeptime s and return $retcode\n";
  sleep($sleeptime);
	print "I'm the child $$ : $retcode, I'm awake\n";
  exit $retcode;
}

# output :
#
#$ perl -w test6.pl
#I'll fork a new child
#I'll fork a new child
#I'll fork a new child
#I'll fork a new child
#15362->started 15363->started 15364->started 15365->started 
#I'm the child 15362 and will sleep for 3 s and return 1
#I'm the child 15363 and will sleep for 6 s and return 0
#I'm the child 15364 and will sleep for 9 s and return 1
#I'm the child 15365 and will sleep for 12 s and return 0
#15362->started 15363->started 15364->started 15365->started 
#I'm the child 15362 : 1, I'm awake
#15362->started 15363->started 15364->started 15365->started 
#15362->started 15363->started 15364->started 15365->started 
#I'm the child 15363 : 0, I'm awake
#15362->started 15363->started 15364->started 15365->started 
#I'm the child 15364 : 1, I'm awake
#15362->started 15363->started 15364->started 15365->started 
#15362->started 15363->started 15364->started 15365->started 
#I'm the child 15365 : 0, I'm awake
#15362->started 15363->started 15364->started 15365->started 
#15362->started 15363->started 15364->started 15365->started 
#15362->started 15363->started 15364->started 15365->started 
#15362->started 15363->started 15364->started 15365->started 
#^C15362->started 15363->started 15364->started 15365->started 

In reply to Is DBI trapping CHLD sigs, is there a workaround? by Anonymous Monk

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.