in reply to Executing a shell script with progress indicator

[In reply to updated question, where the OP attempts to use fork.]

Close. You'll never reach to waitpid. Also, system(...); exit(); is the same as exec(...);

I believe the code below is a robust (allows for missed polls or missed twitches), reusable (a user-provided function indicates when to stop polling) and fancy (seperate timers for polling and twitching) solution. (Untested)

use POSIX qw( WNOHANG ); use Time::HiRes qw( time sleep ); sub min { $_[0] < $_[1] ? $_[0] : $_[1] } sub max { $_[0] > $_[1] ? $_[0] : $_[1] } sub twirling_baton { my ($twitch_interval, $poll_interval, $poll_func) = @_; my $af = ($| = 1); my $tcount = 0; my @baton = qw( | / - \ ); print($baton[0]); my $time = time; my $twitch_time = $time + $twitch_interval; my $poll_time = $time + $poll_interval; for (;;) { sleep(max(min($twitch_time, $poll_time) - $time, 0.250)); # Sleep is not precise. $time = time; if ($time >= $poll_time) { last if &$poll_func(); $time = time; # In case the call is slow. $poll_time = $time + $poll_interval; } if ($time >= $twict_time) { $tcount = ($tcount + 1) % @baton; print("\b", $baton[$tcount]); $twitch_time = $time + $twitch_interval; } } $| = $af; } $pid = fork; if (not defined $pid) { die("Unable to create child process: $!\n"); } if (not $pid) { exec("$program < $temp_file"); die("Unable to launch application: $!\n"); } twirling_baton( 0.250, # Twitch every 500ms 0.500, # Poll every 500ms sub { waitpid($pid, WNOHANG) != 0 }, );

Update: Oops, I used : instead of ? in min and max. Fixed.

Replies are listed 'Best First'.
Re^2: Executing a shell script with progress indicator
by axl163 (Scribe) on Oct 11, 2005 at 21:15 UTC
    Wow! The code looks very impressive. I will give it a shot as it is beyond my comprehension. Can you recommend a good book that will cover this material?