T
titetluc
Hi all
My problem: I have a library managing Perl objects
An object may be composed of several processes
Here is an example of my library (be careful: the sleep call are used
only for debug purpose !!)
#!/usr/bin/perl -w
use strict;
package obj;
sub new
{
my $class = shift;
my %param = @_;
my %obj;
$obj{name} = $param{name};
my @pid;
$pid[0] = fork();
if (!$pid[0]){
sleep(5);
# the treatment to be declared here
exit(1);
}
print "PID: $pid[0]";
$pid[1] = fork();
if (!$pid[1]){
sleep(10000);
# the treatment to be declared here
}
print "PID: $pid[1]";
bless \%obj, $class;
}
sub action
{
my $obj = shift;
sleep(10000);
}
__END__
I would like my library to raise an exception when one of the process
dies for any reasons.
A code, implementing the process death detection and the exception
raise, could be:
sub watch
{
my @pid = @_;
my $p = waitpid(-1,0);
my $to_kill = $pid[0];
if ($p == $to_kill){
$to_kill = $pid[1];
}
kill 15, $to_kill;
die "goodbye cruel world\n";
}
I could then use the library as follows:
package main;
sub main
{
my $o1 = obj->new(name=> 'o1');
my $o2 = obj->new(name=> 'o2');
eval {$o1->action();};
if ($@){
print "o1 killed\n";
}
eval {$o2->action();};
if ($@){
print "o2 killed\n";
}
}
main();
1;
My problem is that I don't know how to call the 'watch' function
.. I could call it in a CHLD sig handler, but in this case I do not
know which object "generates" the CHLD signal. In addtion, the method
is very intrusive because it requires the signal handler declaration
in the main module.
.. I could declare a thread supervision per object (adding the line
threads->new(\&watch, @pid)->detach(); in the obj::new constructor)
but this does not run (message 'thread failed to start: goodbye cruel
world')
I didn't find any information in this group nor in perldoc
Any idea ?
Thanks
Gaetan
My problem: I have a library managing Perl objects
An object may be composed of several processes
Here is an example of my library (be careful: the sleep call are used
only for debug purpose !!)
#!/usr/bin/perl -w
use strict;
package obj;
sub new
{
my $class = shift;
my %param = @_;
my %obj;
$obj{name} = $param{name};
my @pid;
$pid[0] = fork();
if (!$pid[0]){
sleep(5);
# the treatment to be declared here
exit(1);
}
print "PID: $pid[0]";
$pid[1] = fork();
if (!$pid[1]){
sleep(10000);
# the treatment to be declared here
}
print "PID: $pid[1]";
bless \%obj, $class;
}
sub action
{
my $obj = shift;
sleep(10000);
}
__END__
I would like my library to raise an exception when one of the process
dies for any reasons.
A code, implementing the process death detection and the exception
raise, could be:
sub watch
{
my @pid = @_;
my $p = waitpid(-1,0);
my $to_kill = $pid[0];
if ($p == $to_kill){
$to_kill = $pid[1];
}
kill 15, $to_kill;
die "goodbye cruel world\n";
}
I could then use the library as follows:
package main;
sub main
{
my $o1 = obj->new(name=> 'o1');
my $o2 = obj->new(name=> 'o2');
eval {$o1->action();};
if ($@){
print "o1 killed\n";
}
eval {$o2->action();};
if ($@){
print "o2 killed\n";
}
}
main();
1;
My problem is that I don't know how to call the 'watch' function
.. I could call it in a CHLD sig handler, but in this case I do not
know which object "generates" the CHLD signal. In addtion, the method
is very intrusive because it requires the signal handler declaration
in the main module.
.. I could declare a thread supervision per object (adding the line
threads->new(\&watch, @pid)->detach(); in the obj::new constructor)
but this does not run (message 'thread failed to start: goodbye cruel
world')
I didn't find any information in this group nor in perldoc
Any idea ?
Thanks
Gaetan