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

I've read a lot of sections on fork but can't seem to find what I am looking for. I created a program in a pTk. What I am trying to do is basically get the parameters that I need to pass to a shell script, kick off the shell script and still be able to use the window. The Function that RUN's the shell script is set up like:
sub run_program { my $inputfile; my $cmdline; my @running; my $tot_running; my $tot_files; my $tot_done; my $segment; my $locdir; $step++; # step 1 run input command print STDOUT "Running input command $in_cmnd\n"; system ("$in_cmnd"); print STDOUT "Running Script\n"; $step++; # Run as child process so can still use window????? unless(fork) { JOBS: for(;;) { @running=glob("$base/running.$pid.???"); $tot_running=@running+0; $tot_files=0; $tot_done=0; JOB: foreach $inputfile (glob("$in_dsn.???")) { $tot_files++; $segment=substr($inputfile,-3); # it's done if (open CHECK, "$base/done.$pid.$segment") { unlink "$base/running.$pid.$segment"; unlink "$base/done.$pid.$segment"; unlink "$inputfile"; $tot_done++; close CHECK; next JOB; } if (open ERROR, "$base/error.$pid.$segment") { system("rm $base/running.$pid.$segment 2> /dev/null"); print STDOUT "There was an error running segment $segme +nt\n"; error_message("There was an error running segment $segm +ent\n"); $tot_done++; close ERROR; next JOB; } if (open RUNNING, "$base/running.$pid.$segment") { next JOB; close RUNNING; } # launch the process if ($tot_running < $num_process) { $locdir = "/\U$mnem/$pid/$segment"; system("mkdir -p $locdir"); $cmdline="cd $locdir ;nohup $script $pid.$segment $pid. +$segment \L$filter\E 01 $no_mnths N >stdout 2>stderr &"; print STDOUT "Running\n$cmdline\n"; system("$cmdline"); system("echo xxx > $base/running.$pid.$segment"); $tot_running++; } } # for each file # break out of the loop when all files are processed last JOBS if $tot_done == $tot_files; sleep(10); } $step++; # step 3, run output command print STDOUT "Running $out_cmnd\n"; system("$out_cmnd"); $step++; # step 4, merge reports print STDOUT "Mergeing reports\n"; &merge_forms; print STDOUT "\nProcess complete\n\n"; } # end of fork }
It prints out Process complete, and the script runs fine but I keep getting this error:
Process complete X Error of failed request: BadIDChoice (invalid resource ID chosen fo +r this connection) Major opcode of failed request: 53 (X_CreatePixmap) Resource id in failed request: 0x100004f Serial number of failed request: 25739 Current serial number in output stream: 24474
I believe it has something to do with the fork but not quite sure, any ideas!!!! Thanks, Tom

Replies are listed 'Best First'.
Re: Running multiple scripts using fork
by chromatic (Archbishop) on Aug 29, 2001 at 21:47 UTC
    Don't forget to exit in your child. You don't want it to return and try to use resources still active in the parent.