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...


In reply to Re^3: Call Intercepts by almut
in thread Call Intercepts by perlnewbie9292

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.