venki has asked for the wisdom of the Perl Monks concerning the following question:


Hi Perl Monks!

I am running a perl program on unix as a process using fork. I use lot of sub-prgrams within the program.

If one of the sub-program hangs and did not return to the main program i would like to proceed further.

Here is the example

when i connect to the database and for some reason if the database has issue and the connection just hangs with out returning anything, my program does not go further.

I would like to calculate total time taken by the sub-program and if it takes more than certain amount of time i would like to proceed for the next step of the program Is it possible to do this way. Or else let know the best way of handling this type of issues.

Replies are listed 'Best First'.
Re: calculate run time
by Tanalis (Curate) on Nov 04, 2002 at 19:58 UTC
    The easiest way to handle this sort of thing is to set an alarm before you call the database. That way, you can detect the alarm, and perform some action should the database action fail.

    One way to achieve this would be as follows:

    local $SIG{ALRM} = sub { die "Database error!\n"; }; alarm 60; # wait 60 seconds before interrupting # call to database

    Hope that helps ..
    --Foxcub

Re: calculate run time
by zigdon (Deacon) on Nov 04, 2002 at 19:57 UTC

    I think you want to look at the alarm function:

    alarm SECONDS alarm Arranges to have a SIGALRM delivered to this process after the specified number of wallclock seconds have elapsed. If SECONDS is not specified, the value stored in $_ is used.

    -- Dan