in reply to Re: Call Intercepts
in thread Call Intercepts

Thanks for the replies. Sorry I didn't explain myself very well. What I am trying to accomplish is the program to run continuously and also only exit if all code has been run through its entirety, so in my example it should only exit once it has reached the end of &test2 sub since that's where the exit 0 is being called.

The code below works but when I added the IF to check if a file exits it does not work as desired when it reaches the ELSE statement (else { print "$testFile1) when the lck file indeed exists. While that section of code loops until the lck file is removed if a ctrl-c is sent while that section of code is running then the script just gets stuck and does not continue to wait until the lck file is removed. So as long as the ctrl-c is hit outside that else statement section it works as desired.

With the code below which I removed the check of file existence the code work as desired. If I launch the script and hit ctrl-c then it only exits once it has reached the end of the test2 subroutine.

To answer you other questions: testFile1 will always exits in that location. lck file gets created by the script although right now I have no added code to have it removed as I am trying to get this one part working first. Thanks again for all the help.
#!/usr/bin/perl use strict; use warnings; use Fcntl qw(:flock); my $testFile1 = "/tmp/test.txt"; my $lck = "/tmp/l.lck"; my $wantToQuit = 0; while (1) { while ( !$wantToQuit ) { $SIG{INT} = 'waitThenExit'; my $pid = fork(); if ( !defined( $pid ) ) { die("can't fork sub proc\n"); } if ( $pid == 0 ) { print "Currently in pid==0 section, sleeping for 5 seconds +\n"; countDown(5); &test1; &test2; $SIG{INT} = 'DEFAULT'; } waitpid($pid,0); } exit 0 if $wantToQuit; } sub waitThenExit { print "\n\n\n===============WAIT SUB=================\n"; print "One moment, I just gotta finish this bit...\n"; $wantToQuit = 1; print "\n\n\n===============END WAIT SUB=================\n"; } sub test1 { print "\n\n\n===============TEST1 SUB=================\n"; print "Sleeping for 3 seconds In sub test1\n"; countDown(3); print "===============END TEST1 SUB=================\n"; } sub test2 { print "\n\n\n===============TEST2 SUB=================\n"; print "Sleeping for 3 secs In sub Test2\n"; sleep 3; print "Not done yet gotta sleep for another 15 seconds then we can + quit\n"; countDown(15); print "DONE WITH ALL CODE PROGRAM SHOULD NOW EXIT\n"; print "===============END TEST2 SUB=================\n"; exit 0; } sub countDown { my $delay = shift; my $count = 0; while ( $count < $delay ) { print "$count\n"; sleep 1; $count++; } }
Output when script is run with no interuption
Currently in pid==0 section, sleeping for 5 seconds 0 1 2 3 4 ===============TEST1 SUB================= Sleeping for 3 seconds In sub test1 0 1 2 ===============END TEST1 SUB================= ===============TEST2 SUB================= Sleeping for 3 secs In sub Test2 Not done yet gotta sleep for another 15 seconds then we can quit 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 DONE WITH ALL CODE PROGRAM SHOULD NOW EXIT

OUTPUT WHEN CTRL-C is hit right after the script has been launched
Currently in pid==0 section, sleeping for 5 seconds 0 1 ===============WAIT SUB================= One moment, I just gotta finish this bit... ===============END WAIT SUB================= 2 ===============WAIT SUB================= One moment, I just gotta finish this bit... ===============END WAIT SUB================= 3 4 ===============TEST1 SUB================= Sleeping for 3 seconds In sub test1 0 1 2 ===============END TEST1 SUB================= ===============TEST2 SUB================= Sleeping for 3 secs In sub Test2 Not done yet gotta sleep for another 15 seconds then we can quit 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 DONE WITH ALL CODE PROGRAM SHOULD NOW EXIT ===============END TEST2 SUB=================

Replies are listed 'Best First'.
Re^3: Call Intercepts
by almut (Canon) on Dec 01, 2009 at 16:34 UTC
    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...

      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.