#!/usr/local/bin/perl5 -w use strict qw(refs); require 5.001; use Getopt::Std; use Sybase::DBlib; use POSIX "sys_wait_h"; # Open database connections my $x = db_login( ... ) || die( "Could not open ..." ); my $xx = db_login( ... ) || die( "Could not open ..." ); my $xxx = db_login( ... ) || die( "Could not open ..." ); my $xxxx = db_login( ... ) || die( "Could not open ..." ); my %j = my %jj = ; my %jjjj = ; my $pid1 = &executeChild( ....... ); my $pid2 = &executeChild( ....... ); my $pid3 = &executeChild( ....... ); my $pid4 = &executeChild( ....... ); my $pid5 = &executeChild( ....... ); my $pid6 = &executeChild( ....... ); print "Waiting for forked processes to finish...\n"; waitpid($pid1,0); waitpid($pid2,0); waitpid($pid3,0); waitpid($pid4,0); waitpid($pid5,0); waitpid($pid6,0); print "Done waiting, all forked processes have finished...\n"; exit 0; sub executeChild { my $fork_pid=fork(); if( $fork_pid != 0 ) { return $fork_pid; } # open 4 more db connections to get some data $gg = db_login( .........) || die( "Could not open ........." ); $ggg = db_login( ......... ) || die( "Could not open ........." ); $gggg = db_login( ......... ) || die( "Could not open ........." ); $ggggg = db_login( ......... ) || die( "Could not open ........." ); # open 3 files here - child specific open(Some_File_Handler1, ">some_file_name1") || die ("Failed"); open(Some_File_Handler2, ">some_file_name2") || die ("Failed"); open(Some_File_Handler3, ">some_file_name3") || die ("Failed"); #TILL This point no error, error occurs after this point at some random place for ( over 270k items) { do some processing, involving hashmap manipulation, and writing out data into 3 files } do some more processing - involving array/hashmap xx. call three external programs [system(...)]to work on the written out files. read in files generated in xx step. do some more processing open 3 final output files write out 3 final output files } #### if ($!) { print "Error occured: $!"; # reset the error so that we can catch next genuine error condition $! = 0; }