[sf-perl] not understanding program behavior with alarm()

Francisco Obispo fobispo at isc.org
Sun Feb 5 06:27:24 PST 2012


Or avoid the fork() altogether..

Try using: 

my $result=qx{/path/to/my/script.sh};

regards,




On Feb 5, 2012, at 12:17 AM, Greg Lindahl wrote:

> Yes, you need to fork first.
> 
> sub system_with_timeout
> {
>    my ( $timeout, @argv ) = @_;
>    my $status = 1;
>    return 1 unless defined $timeout and $timeout > 0;
>    return 1 unless @argv and "@argv";
> 
>    my $pid = fork;
>    if ( ! defined $pid ) { die "fork failed: $!"; }
>    if ( $pid ) # parent                                                                                                                     
>    {
>        local $SIG{ALRM} = sub { die "Alarm fired\n"; };
>        alarm $timeout;
>        eval {
>            waitpid $pid, 0;
>            $status = $?;
>        };
>        if ( $@ ) # alarm fired                                                                                                              
>        {
>            kill 9, $pid;
>            return $status || 1; # force it to non-zero                                                                                      
>        }
>        alarm 0;
>    }
>    else
>    {
>        exec @argv;
>        die "exec failed: $!";
>    }
>    return $status;
> }
> 
> 
> On Fri, Feb 03, 2012 at 04:08:36PM -0800, David Alban wrote:
>> hmmm...  then i should probably fork() first... ?
>> 
>> On Fri, Feb 3, 2012 at 2:57 PM, Francisco Obispo <fobispo at isc.org> wrote:
>>> Perl's system() call performs a fork() on the specified process,
>>> 
>>> you might want to try exec() instead..
>> 
>> -- 
>> Live in a world of your own, but always welcome visitors.
>> ***
>> Rule of law is for the little people.
>> http://www.amazon.com/Liberty-Justice-Some-Equality-Powerful/dp/0805092056
>> _______________________________________________
>> SanFrancisco-pm mailing list
>> SanFrancisco-pm at pm.org
>> http://mail.pm.org/mailman/listinfo/sanfrancisco-pm
> _______________________________________________
> SanFrancisco-pm mailing list
> SanFrancisco-pm at pm.org
> http://mail.pm.org/mailman/listinfo/sanfrancisco-pm



More information about the SanFrancisco-pm mailing list