in reply to Re^2: Call Intercepts
in thread Call Intercepts

it does not work as desired when it reaches the ELSE statement (else { print "$testFile1) when the lck file indeed exists. (...) the script just gets stuck and does not continue

You're still not using exit in the child! :)  The problem with this is that the child process continues to run code outside of the if ( $pid == 0 ) {...}, i.e. you have two processes (parent and child) running the same code, which is not what you want (in this particular case, each child forks a new process, while its parent hangs in waitpid() waiting for it to terminate, which it doesn't because it in turn is waiting for its own child...)  Add

... print "doing waitpid($pid,0)...\n"; # <--- waitpid($pid,0);

and you'll see what I mean — i.e. that it hangs after "doing waitpid(0,0)..." after you have pressed CTRL-C (in which case you stop forking new children due to $wantToQuit now being true), and that you're accumulating more and more processes otherwise — run ps axf from another terminal after a while, and you'll see something like this

17001 pts/16 S+ 0:00 \_ /usr/bin/perl ./810377.pl 17002 pts/16 S+ 0:00 \_ /usr/bin/perl ./810377.pl 17004 pts/16 S+ 0:00 \_ /usr/bin/perl ./810377. +pl 17005 pts/16 S+ 0:00 \_ /usr/bin/perl ./810 +377.pl 17007 pts/16 S+ 0:00 \_ /usr/bin/perl . +/810377.pl 17008 pts/16 S+ 0:00 \_ /usr/bin/pe +rl ./810377.pl 17009 pts/16 S+ 0:00 \_ /usr/bi +n/perl ./810377.pl 17018 pts/16 S+ 0:00 \_ /us +r/bin/perl ./810377.pl 17021 pts/16 S+ 0:00 \_ + /usr/bin/perl ./810377.pl 17054 pts/16 R+ 0:03 + \_ /usr/bin/perl ./810377.pl

with all those processes hanging in waitpid() because their children haven't yet exited.  This problem only manifests in case of the else branch being run, because otherwise you are in fact exiting with the exit 0; at the end of test2().

Add an exit here

if ( $pid == 0 ) { if ( ! -e $lck ) { ... } else { ... } $SIG{INT} = 'DEFAULT'; exit; # <--- }

and the problem is gone.  And if you want to execute the test*() routines in the else case, too (which I'm not quite sure as you're saying "...only exit if all code has been run through its entirety"), you'd have to take them out of the if branch...

Replies are listed 'Best First'.
Re^4: Call Intercepts
by perlnewbie9292 (Novice) on Dec 01, 2009 at 16:55 UTC
    AHHHHHH, lol...That was the problem now everything works like expected. Thanks for pointing that out, still trying to learn/grasp where to put commands. Guess I just need to keep on practicing. Thanks again for all your help everyone.