N
Nitzan Shaked
Hi all
I need to do a timed operation: ie give up after some time if it does
not finish. From what I can gather the "canon" way of doing this is:
eval {
local $SIG{ALRM} = sub { die "alarm\n"; };
alarm 10; ## for example
$result = my_operation();
alarm 0;
};
if ( $@ ) {
die unless $@ eq "alarm\n";
## CODE IF TIMED-OUT
} else {
## CODE IF DID NOT TIME OUT
}
However, it strikes me as if there's a race here, and I can't find
proof to the contrary in the documentation:
==> What happens if my_operation() is performed, finishes on time, even
$result is set, ... BUT the alarm expires (is set) before running
"alarm 0", that is: "between" the lines "$result=..." and "alarm 0;".
In that case the operation was performed but I will not know that, or
think that it was not.
Specifically, I want to spawn child processes and wait for them, but
not more than "x" seconds. Is there a better way to do this other than
fork() and then:
1) Have the parent wait for it's children?
2) Have the children call exec()?
Of course I realize the race in my case is not terrible: worst case the
parent will try to kill a PID which was already "reaped". If that
doesn't happen before pid's overflow and start back from 1 (and it
won't in reality...) I am safe.
Still, is there no safe way of doing this?
I need to do a timed operation: ie give up after some time if it does
not finish. From what I can gather the "canon" way of doing this is:
eval {
local $SIG{ALRM} = sub { die "alarm\n"; };
alarm 10; ## for example
$result = my_operation();
alarm 0;
};
if ( $@ ) {
die unless $@ eq "alarm\n";
## CODE IF TIMED-OUT
} else {
## CODE IF DID NOT TIME OUT
}
However, it strikes me as if there's a race here, and I can't find
proof to the contrary in the documentation:
==> What happens if my_operation() is performed, finishes on time, even
$result is set, ... BUT the alarm expires (is set) before running
"alarm 0", that is: "between" the lines "$result=..." and "alarm 0;".
In that case the operation was performed but I will not know that, or
think that it was not.
Specifically, I want to spawn child processes and wait for them, but
not more than "x" seconds. Is there a better way to do this other than
fork() and then:
1) Have the parent wait for it's children?
2) Have the children call exec()?
Of course I realize the race in my case is not terrible: worst case the
parent will try to kill a PID which was already "reaped". If that
doesn't happen before pid's overflow and start back from 1 (and it
won't in reality...) I am safe.
Still, is there no safe way of doing this?